class Mause { constructor(x, y) { this.x = x; this.y = y; this.life = 5; this.directions = []; } getNewcordinates(){ this.directions = [ [this.x - 1, this.y - 1], [this.x , this.y - 1], [this.x + 1, this.y - 1], [this.x - 1, this.y ], [this.x + 1, this.y ], [this.x - 1, this.y + 1], [this.x , this.y + 1], [this.x + 1, this.y + 1] ]; } chooseCell(character) { this.getNewcordinates(); var found = []; for (var i in this.directions) { var x = this.directions[i][0]; var y = this.directions[i][1]; if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) { if (matrix[y][x] == character) { found.push(this.directions[i]); } } } return found; } mul() { let emptyCells = this.chooseCell(0); let newCell = random(emptyCells); if (newCell) { let x = newCell[0]; let y = newCell[1]; // matrixi mej gru mem MEK -> matrix[y][x] = 2; let a = new Mause(x, y); fishArr.push(a); this.life = 0; } } eat() { let emptyCells = this.chooseCell(1); let newCell = random(emptyCells); if (newCell) { this.life++; let x = newCell[0]; let y = newCell[1]; matrix[y][x] = 2; matrix[this.y][this.x] = 0; for (let i in fishArr) { if (fishArr[i].x == x && fishArr[i].y == y) { fishArr.splice(i, 1) } } this.x = x; this.y = y; if (this.life >= 10) { this.mul(); } } else { this.move() } } move() { this.life--; let emptyCells = this.chooseCell(0); let newCell = random(emptyCells); if (newCell) { let x = newCell[0]; let y = newCell[1]; // matrixi mej gru mem MEK -> 1 matrix[y][x] = 2; matrix[this.y][this.x] = 0; this.y = y; this.x = x; } if (this.life < 0) { this.die(); } } die() { matrix[this.y][this.x] = 0; for (let i in fishArr) { if (fishArr[i].x == this.x && fishArr[i].y == this.y) { fishArr.splice(i, 1) } } } } 

  • Add a fully working example with html, js and css. - Stepan Kasyanenko

0