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

solve #1078

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

solve #1078

Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ You can change the HTML/CSS layout if you need it.
## Deploy and Pull Request

1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_2048_game/)
- [DEMO LINK](https://oleg1979080.github.io/js_2048_game/)
2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
Expand Down
158 changes: 157 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,163 @@
/* eslint-disable function-paren-newline */
'use strict';

// Uncomment the next lines to use your game instance in the browser
// const Game = require('../modules/Game.class');
// const game = new Game();

// Write your code here
export class Game {
constructor(initialState = null) {
this.size = 4; // 4x4 board
this.board = initialState || this.generateEmptyBoard();
this.score = 0;
this.status = 'waiting';
}

generateEmptyBoard() {
return Array.from({ length: this.size }, () => Array(this.size).fill(0));
}

getState() {
return this.board;
}

getScore() {
return this.score;
}

getStatus() {
return this.status;
}

spawnTile() {
const emptyCells = [];

for (let row = 0; row < this.size; row++) {
for (let col = 0; col < this.size; col++) {
if (this.board[row][col] === 0) {
emptyCells.push({ row, col });
}
}
}

if (emptyCells.length > 0) {
const { row, col } =
emptyCells[Math.floor(Math.random() * emptyCells.length)];

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

compressRow(row) {
const newRow = row.filter((num) => num !== 0);

while (newRow.length < this.size) {
newRow.push(0);
}

return newRow;
}

mergeRow(row) {
for (let i = 0; i < this.size - 1; i++) {
if (row[i] !== 0 && row[i] === row[i + 1]) {
row[i] *= 2;
this.score += row[i];
row[i + 1] = 0;
}
}

return row;
}

moveLeft() {
let moved = false;

this.board = this.board.map((row) => {
const compressed = this.compressRow(row);
const merged = this.mergeRow(compressed);
const newRow = this.compressRow(merged);

if (newRow.toString() !== row.toString()) {
moved = true;
}

return newRow;
});

if (moved) {
this.spawnTile();
this.checkGameStatus();
}
}

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

moveUp() {
this.transposeBoard();
this.moveLeft();
this.transposeBoard();
}

moveDown() {
this.transposeBoard();
this.moveRight();
this.transposeBoard();
}

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

checkGameStatus() {
if (this.board.some((row) => row.includes(2048))) {
this.status = 'won';
} else if (!this.canMove()) {
this.status = 'lost';
}
}

canMove() {
for (let row = 0; row < this.size; row++) {
for (let col = 0; col < this.size; col++) {
if (this.board[row][col] === 0) {
return true;
}

if (
col < this.size - 1 &&
this.board[row][col] === this.board[row][col + 1]
) {
return true;
}

if (
row < this.size - 1 &&
this.board[row][col] === this.board[row + 1][col]
) {
return true;
}
}
}

return false;
}

start() {
this.board = this.generateEmptyBoard();
this.score = 0;
this.status = 'playing';
this.spawnTile();
this.spawnTile();
}

restart() {
this.start();
}
}
Loading