Skip to content

Commit

Permalink
add solve
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg19790807 committed Dec 16, 2024
1 parent cba0bbf commit d974264
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 55 deletions.
173 changes: 118 additions & 55 deletions src/modules/Game.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,126 @@
* Feel free to add more props and methods if needed.
*/
class Game {
/**
* Creates a new game instance.
*
* @param {number[][]} initialState
* The initial state of the board.
* @default
* [[0, 0, 0, 0],
* [0, 0, 0, 0],
* [0, 0, 0, 0],
* [0, 0, 0, 0]]
*
* If passed, the board will be initialized with the provided
* initial state.
*/
constructor(initialState) {
// eslint-disable-next-line no-console
console.log(initialState);
constructor(
initialState = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
],
) {
this.board = initialState;
this.score = 0;
this.status = 'idle'; // 'idle', 'playing', 'win', 'lose'
}

moveLeft() {}
moveRight() {}
moveUp() {}
moveDown() {}

/**
* @returns {number}
*/
getScore() {}

/**
* @returns {number[][]}
*/
getState() {}

/**
* Returns the current game status.
*
* @returns {string} One of: 'idle', 'playing', 'win', 'lose'
*
* `idle` - the game has not started yet (the initial state);
* `playing` - the game is in progress;
* `win` - the game is won;
* `lose` - the game is lost
*/
getStatus() {}

/**
* Starts the game.
*/
start() {}

/**
* Resets the game.
*/
restart() {}

// Add your own methods here
getState() {
return this.board;
}

getScore() {
return this.score;
}

getStatus() {
return this.status;
}

start() {
this.status = 'playing';
this.addNewTile();
this.addNewTile();
}

restart() {
this.board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];
this.score = 0;
this.status = 'idle';
this.addNewTile();
this.addNewTile();
}

addNewTile() {
const emptyCells = [];

this.board.forEach((row, rIndex) => {
row.forEach((cell, cIndex) => {
if (cell === 0) {
emptyCells.push([rIndex, cIndex]);
}
});
});

if (emptyCells.length > 0) {
const [rIndex, cIndex] =
emptyCells[Math.floor(Math.random() * emptyCells.length)];

this.board[rIndex][cIndex] = Math.random() < 0.9 ? 2 : 4;
}
}

moveLeft() {
let moved = false;

this.board.forEach((row) => {
const newRow = row.filter((cell) => cell !== 0);
const mergedRow = [];
let skip = false;

for (let i = 0; i < newRow.length; i++) {
if (skip) {
skip = false;
continue;
}

if (i + 1 < newRow.length && newRow[i] === newRow[i + 1]) {
mergedRow.push(newRow[i] * 2);
this.score += newRow[i] * 2;
skip = true;
} else {
mergedRow.push(newRow[i]);
}
}

while (mergedRow.length < 4) {
mergedRow.push(0);
}
row.splice(0, 4, ...mergedRow);
moved = true;
});

if (moved) {
this.addNewTile();
}
}

moveRight() {
this.board.forEach((row) => row.reverse());
this.moveLeft();
this.board.forEach((row) => row.reverse());
}

moveUp() {
this.board = this.transpose(this.board);
this.moveLeft();
this.board = this.transpose(this.board);
}

moveDown() {
this.board = this.transpose(this.board);
this.moveRight();
this.board = this.transpose(this.board);
}

transpose(board) {
return board[0].map((_, colIndex) => board.map((row) => row[colIndex]));
}
}

// Export Game class
module.exports = Game;
2 changes: 2 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
/* eslint-disable no-unused-vars */
/* eslint-disable prefer-const */
/* eslint-disable no-shadow */
Expand Down Expand Up @@ -78,3 +79,4 @@ function render() {




0 comments on commit d974264

Please sign in to comment.