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

Develop #1092

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

Develop #1092

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
29 changes: 29 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm start & sleep 5 && npm test
- name: Upload tests report(cypress mochaawesome merged HTML report)
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: report
path: reports
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://nex1994.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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@mate-academy/eslint-config": "latest",
"@mate-academy/jest-mochawesome-reporter": "^1.0.0",
"@mate-academy/linthtml-config": "latest",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/stylelint-config": "latest",
"@parcel/transformer-sass": "^2.12.0",
"cypress": "^13.13.0",
Expand Down
38 changes: 19 additions & 19 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<title>2048</title>
<link
rel="stylesheet"
href="./styles/main.scss"
href="./styles/styles.css"
/>
</head>
<body>
Expand All @@ -21,38 +21,38 @@ <h1>2048</h1>
Score:
<span class="game-score">0</span>
</p>
<button class="button start">Start</button>
<button class="button start button-start">Start</button>
</div>
</div>

<table class="game-field">
<tbody>
<tr class="field-row">
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell" data-position="0-0"></td>
<td class="field-cell" data-position="0-1"></td>
<td class="field-cell" data-position="0-2"></td>
<td class="field-cell" data-position="0-3"></td>
</tr>

<tr class="field-row">
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell" data-position="1-0"></td>
<td class="field-cell" data-position="1-1"></td>
<td class="field-cell" data-position="1-2"></td>
<td class="field-cell" data-position="1-3"></td>
</tr>

<tr class="field-row">
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell" data-position="2-0"></td>
<td class="field-cell" data-position="2-1"></td>
<td class="field-cell" data-position="2-2"></td>
<td class="field-cell" data-position="2-3"></td>
</tr>

<tr class="field-row">
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell"></td>
<td class="field-cell" data-position="3-0"></td>
<td class="field-cell" data-position="3-1"></td>
<td class="field-cell" data-position="3-2"></td>
<td class="field-cell" data-position="3-3"></td>
</tr>
</tbody>
</table>
Expand All @@ -65,6 +65,6 @@ <h1>2048</h1>
</p>
</div>
</div>
<script src="scripts/main.js"></script>
<script type="module" src="/src/scripts/main.js"></script>
</body>
</html>
217 changes: 157 additions & 60 deletions src/modules/Game.class.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,165 @@
'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(
cells = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
],
) {
this.cells = cells;
this.restart();
}

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
slideAndMerge(row) {
const filteredRow = row.filter((val) => val !== 0);
const newRow = [];

for (let i = 0; i < filteredRow.length; i++) {
if (filteredRow[i] === filteredRow[i + 1]) {
newRow.push(filteredRow[i] * 2);
this.score += filteredRow[i] * 2;
i++;
} else {
newRow.push(filteredRow[i]);
}
}

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

return newRow;
}

move(direction) {
if (this.status !== 'playing') {
return;
}

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

let moved = false;

if (direction === 'up' || direction === 'down') {
this.board = rotated(this.board);
}

for (let i = 0; i < 4; i++) {
const row =
direction === 'right' || direction === 'down'
? [...this.board[i]].reverse()
: [...this.board[i]];

const newRow = this.slideAndMerge(row);

if (direction === 'right' || direction === 'down') {
newRow.reverse();
}

if (this.board[i].toString() !== newRow.toString()) {
this.board[i] = newRow;
moved = true;
}
}

if (direction === 'up' || direction === 'down') {
this.board = rotated(this.board);
}

if (moved) {
this.addRandomTitle();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a typo in the method name addRandomTitle. It should likely be addRandomTile to reflect the action of adding a random tile to the board.

}
this.checkGameOver();
}

moveLeft() {
this.move('left');
}
moveRight() {
this.move('right');
}
moveUp() {
this.move('up');
}
moveDown() {
this.move('down');
}

checkGameOver() {
if (this.board.some((row) => row.includes(2048))) {
this.status = 'win';
} else if (!this.canMove()) {
this.status = 'lose';
}
}

canMove() {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (this.board[i][j] === 0) {
return true;
}

if (j < 3 && this.board[i][j] === this.board[i][j + 1]) {
return true;
}

if (i < 3 && this.board[i][j] === this.board[i + 1][j]) {
return true;
}
}
}

return false;
}

addRandomTitle() {
const emptyCells = [];

this.board.forEach((row, rowIndex) => {
row.forEach((value, colIndex) => {
if (value === 0) {
emptyCells.push({ rowIndex, colIndex });
}
});
});

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

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

getScore() {
return this.score;
}

getState() {
return this.board.map((row) => [...row]);
}

getStatus() {
return this.status;
}

start() {
this.status = 'playing';
this.addRandomTitle();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same typo as above: addRandomTitle should be addRandomTile.

this.addRandomTitle();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same typo as above: addRandomTitle should be addRandomTile.

}

restart() {
this.board = this.cells.map((row) => [...row]);
this.score = 0;
this.status = 'idle';
}
}

module.exports = Game;
Loading
Loading