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

solution #1193

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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://CNegruzzi.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
Binary file added src/images/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
rel="stylesheet"
href="./styles/main.scss"
/>
<link
rel="shortcut icon"
href="/src/images/favicon.png"
type="image/x-icon"
/>
</head>
<body>
<div class="container">
Expand Down Expand Up @@ -65,6 +70,9 @@ <h1>2048</h1>
</p>
</div>
</div>
<script src="scripts/main.js"></script>
<script
type="module"
src="scripts/main.js"
></script>
</body>
</html>
298 changes: 253 additions & 45 deletions src/modules/Game.class.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,183 @@
'use strict';

/**
* This class represents the game.
* Now it has a basic structure, that is needed for testing.
* 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';
}

moveLeft() {}
moveRight() {}
moveUp() {}
moveDown() {}
moveLeft() {
const oldBoard = JSON.stringify(this.board);

/**
* @returns {number}
*/
getScore() {}
const shiftAndFilter = (array) => {
const filteredArray = array.filter((cell) => cell !== 0);

/**
* @returns {number[][]}
*/
getState() {}
while (filteredArray.length < 4) {
filteredArray.push(0);
}

return filteredArray;
};

for (let row = 0; row < 4; row++) {
this.board[row] = shiftAndFilter(this.board[row]);

for (let col = 0; col < 3; col++) {
if (this.board[row][col] === this.board[row][col + 1]) {
this.board[row][col] += this.board[row][col + 1];
this.board[row][col + 1] = 0;
this.score += this.board[row][col];

col++;
}
}

this.board[row] = shiftAndFilter(this.board[row]);
}

const newBoard = JSON.stringify(this.board);

return oldBoard !== newBoard;
}

moveRight() {
const oldBoard = JSON.stringify(this.board);

const shiftAndFilter = (array) => {
const filteredArray = array.filter((cell) => cell !== 0);

while (filteredArray.length < 4) {
filteredArray.unshift(0);
}

return filteredArray;
};

for (let row = 0; row < 4; row++) {
this.board[row] = shiftAndFilter(this.board[row]);

for (let col = 0; col < 3; col++) {
if (this.board[row][col] === this.board[row][col + 1]) {
this.board[row][col] += this.board[row][col + 1];
this.board[row][col + 1] = 0;
this.score += this.board[row][col];

col++;
}
}

this.board[row] = shiftAndFilter(this.board[row]);
}

const newBoard = JSON.stringify(this.board);

return oldBoard !== newBoard;
}

moveUp() {
const oldBoard = JSON.stringify(this.board);

const shiftAndFilter = (array) => {
const filteredArray = array.filter((cell) => cell !== 0);

while (filteredArray.length < 4) {
filteredArray.push(0);
}

return filteredArray;
};

for (let col = 0; col < 4; col++) {
let column = [];

for (let row = 0; row < 4; row++) {
column.push(this.board[row][col]);
}

column = shiftAndFilter(column);

for (let r = 0; r < 3; r++) {
if (column[r] === column[r + 1]) {
column[r] += column[r + 1];
column[r + 1] = 0;
this.score += column[r];

r++;
}
}

column = shiftAndFilter(column);

for (let rowIndex = 0; rowIndex < 4; rowIndex++) {
this.board[rowIndex][col] = column[rowIndex];
}
}

const newBoard = JSON.stringify(this.board);

return oldBoard !== newBoard;
}

moveDown() {
const oldBoard = JSON.stringify(this.board);

const shiftAndFilter = (array) => {
const filteredArray = array.filter((cell) => cell !== 0);

while (filteredArray.length < 4) {
filteredArray.push(0);
}

return filteredArray;
};

for (let col = 0; col < 4; col++) {
let column = [];

for (let row = 0; row < 4; row++) {
column.push(this.board[row][col]);
}

column.reverse();
column = shiftAndFilter(column);

for (let r = 0; r < 3; r++) {
if (column[r] === column[r + 1]) {
column[r] += column[r + 1];
column[r + 1] = 0;
this.score += column[r];

r++;
}
}

column = shiftAndFilter(column);
column.reverse();

for (let rowIndex = 0; rowIndex < 4; rowIndex++) {
this.board[rowIndex][col] = column[rowIndex];
}
}

const newBoard = JSON.stringify(this.board);

return oldBoard !== newBoard;
}

getScore() {
return this.score;
}

getState() {
return this.board;
}

/**
* Returns the current game status.
Expand All @@ -50,19 +189,88 @@ class Game {
* `win` - the game is won;
* `lose` - the game is lost
*/
getStatus() {}
getStatus() {
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 4; col++) {
if (this.board[row][col] === 2048) {
this.status = 'win';

/**
* Starts the game.
*/
start() {}
return this.status;
}
}
}

/**
* Resets the game.
*/
restart() {}
const hasEmptyTiles = this.board.some((row) => row.includes(0));

if (!hasEmptyTiles) {
const canMove = (board) => {
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 4; col++) {
if (
(col < 3 && board[row][col] === board[row][col + 1]) ||
(row < 3 && board[row][col] === board[row + 1][col])
) {
return true;
}
}
}

return false;
};

if (!canMove(this.board)) {
this.status = 'lose';

return this.status;
}
}

return this.status;
}

start() {
this.board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];

// Add your own methods here
this.score = 0;
this.status = 'playing';

this.addRandomTiles();
this.addRandomTiles();
}

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';
}

addRandomTiles() {
const emptyTiles = [];

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

const randomCell =
emptyTiles[Math.floor(Math.random() * emptyTiles.length)];

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

module.exports = Game;
Loading
Loading