Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js_2048_game #1036

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ <h1>2048</h1>
</p>
</div>
</div>
<script src="scripts/main.js"></script>
<script
type="module"
src="scripts/main.js"
></script>
</body>
</html>
288 changes: 276 additions & 12 deletions src/modules/Game.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,79 @@ class Game {
* 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],
],
) {
if (
initialState.length === 4 &&
initialState.every((row) => row.length === 4)
) {
this.initialState = initialState;
this.restart();
} else {
throw new Error('Initial state is not valid!');
}
}

moveLeft() {}
moveRight() {}
moveUp() {}
moveDown() {}
moveLeft() {
if (this.status === 'playing') {
const result = this.moveTiles(true, false);

if (this.isStateDifferent(result.state)) {
this.updateGame(result);
}
}
}

moveRight() {
if (this.status === 'playing') {
const result = this.moveTiles(true, true);

if (this.isStateDifferent(result.state)) {
this.updateGame(result);
}
}
}

moveUp() {
if (this.status === 'playing') {
const result = this.moveTiles(false, false);

if (this.isStateDifferent(result.state)) {
this.updateGame(result);
}
}
}

moveDown() {
if (this.status === 'playing') {
const result = this.moveTiles(false, true);

if (this.isStateDifferent(result.state)) {
this.updateGame(result);
}
}
}

/**
* @returns {number}
*/
getScore() {}
getScore() {
return this.score;
}

/**
* @returns {number[][]}
*/
getState() {}
getState() {
return this.state;
}

/**
* Returns the current game status.
Expand All @@ -50,19 +104,229 @@ class Game {
* `win` - the game is won;
* `lose` - the game is lost
*/
getStatus() {}
getStatus() {
return this.status;
}

/**
* Starts the game.
*/
start() {}
start() {
this.restart();
this.status = 'playing';

this.putNewNumber();
this.putNewNumber();
}

/**
* Resets the game.
*/
restart() {}
restart() {
this.state = this.cloneState(this.initialState);
this.score = 0;
this.status = 'idle';
this.firstMoveMade = false;
}

// Add your own methods here
cloneState(state) {
const newState = [];

for (let row = 0; row < state.length; row++) {
newState.push([...state[row]]);
}

return newState;
}

isStateDifferent(newState) {
for (let row = 0; row < this.state.length; row++) {
for (let column = 0; column < this.state[row].length; column++) {
if (this.state[row][column] !== newState[row][column]) {
return true;
}
}
}

return false;
}

updateGame(result) {
this.state = result.state;
this.score += result.score;
this.firstMoveMade = true;
this.putNewNumber();

if (this.isGameOver()) {
this.status = 'lose';
} else if (this.isGameWon()) {
this.status = 'win';
}
}

getFirstMoveMade() {
return this.firstMoveMade;
}

getAvailableCell() {
const cell = [];

this.state.forEach((row, y) => {
row.forEach((number, x) => {
if (!number) {
cell.push({ x, y });
}
});
});

return cell;
}

getRandomElement(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);

return arr[randomIndex];
}

generateNumber() {
return Math.floor(Math.random() * 10) === 0 ? 4 : 2;
}

putNewNumber() {
const availableCell = this.getAvailableCell();
const randomCell = this.getRandomElement(availableCell);

this.state[randomCell.y][randomCell.x] = this.generateNumber();
}

isGameOver() {
if (this.getAvailableCell().length === 0) {
const directions = [
{ horizontal: false, forward: false },
{ horizontal: true, forward: true },
{ horizontal: false, forward: true },
{ horizontal: true, forward: false },
];

return directions.every(({ horizontal, forward }) => {
const newState = this.moveTiles(horizontal, forward).state;

return this.isStateDifferent(newState);
});
}

return false;
}

isGameWon() {
return this.state.flat().includes(2048);
}

getCellCoords(horizontal, firstCoord, position) {
return horizontal
? { row: firstCoord, column: position }
: { row: position, column: firstCoord };
}

moveTiles(horizontal, forward, addScore) {
const state = this.cloneState(this.state);
let score = 0;

const rowLength = state[0].length;
const columnLength = state.length;
let firstCoordLength;
let secondCoordLength;

if (horizontal) {
firstCoordLength = columnLength;
secondCoordLength = rowLength;
} else {
firstCoordLength = rowLength;
secondCoordLength = columnLength;
}

let initialSecondCoord;
let startOfStripe;
let endOfStirpe;
let step;

if (forward) {
initialSecondCoord = secondCoordLength - 2;
startOfStripe = -1;
endOfStirpe = secondCoordLength;
step = 1;
} else {
initialSecondCoord = 1;
startOfStripe = secondCoordLength;
endOfStirpe = -1;
step = -1;
}

for (let firstCoord = 0; firstCoord < firstCoordLength; firstCoord++) {
const mergedInStripe = Array(secondCoordLength).fill(false);

for (
let secondCoord = initialSecondCoord;
secondCoord !== startOfStripe;
secondCoord -= step
) {
let merged = false;
let position = secondCoord;

let currentCellCoords = this.getCellCoords(
horizontal,
firstCoord,
position,
);
let nextCellCoords = this.getCellCoords(
horizontal,
firstCoord,
position + step,
);

while (
position + step !== endOfStirpe &&
(state[nextCellCoords.row][nextCellCoords.column] === 0 ||
(state[nextCellCoords.row][nextCellCoords.column] ===
state[currentCellCoords.row][currentCellCoords.column] &&
!mergedInStripe[position + step] &&
!merged))
) {
if (state[nextCellCoords.row][nextCellCoords.column] > 0) {
merged = true;
score += state[nextCellCoords.row][nextCellCoords.column] * 2;
}

state[nextCellCoords.row][nextCellCoords.column] +=
state[currentCellCoords.row][currentCellCoords.column];
state[currentCellCoords.row][currentCellCoords.column] = 0;
position += step;

currentCellCoords = this.getCellCoords(
horizontal,
firstCoord,
position,
);

nextCellCoords = this.getCellCoords(
horizontal,
firstCoord,
position + step,
);
}

if (merged) {
mergedInStripe[position] = true;
}
}
}

return {
state: state,
score: score,
};
}
}

module.exports = Game;
Loading
Loading