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

resolution game_2048 #1072

Open
wants to merge 2 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
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://14lucas-mendes.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
8 changes: 4 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
190 changes: 177 additions & 13 deletions src/modules/Game.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,78 @@ 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],
],
) {
this.state = 'idle';
this.score = 0;
this.initialState = initialState;
}

moveLeft() {}
moveRight() {}
moveUp() {}
moveDown() {}
// eslint-disable-next-line no-console

moveLeft() {
if (this.state === 'idle') {

Choose a reason for hiding this comment

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

The condition should be this.state === 'playing' to ensure moves are only allowed when the game is actively being played.

return;
}

const updatedState = this.processMoveLeft();

this.updatedGameState(updatedState);

return this.initialState;
}
moveRight() {
if (this.state === 'idle') {

Choose a reason for hiding this comment

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

The condition should be this.state === 'playing' to ensure moves are only allowed when the game is actively being played.

return;
}

const updatedState = this.processMoveRight();

this.updatedGameState(updatedState);

return this.initialState;
}
moveUp() {
if (this.state === 'idle') {

Choose a reason for hiding this comment

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

The condition should be this.state === 'playing' to ensure moves are only allowed when the game is actively being played.

return;
}

const updatedState = this.processMoveUp();

this.updatedGameState(updatedState);

return this.initialState;
}
moveDown() {
if (this.state === 'idle') {

Choose a reason for hiding this comment

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

The condition should be this.state === 'playing' to ensure moves are only allowed when the game is actively being played.

return;
}

const updatedState = this.processMoveDown();

this.updatedGameState(updatedState);

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

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

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

/**
* Starts the game.
*/
start() {}
start() {
const initialStateEmpty = this.isInitialStateAllZeros;

const newInitialState = this.generateInitialState(initialStateEmpty);

this.resetGamesState(newInitialState);

return this.initialState;
}

/**
* Resets the game.
*/
restart() {}
restart() {
this.initialState = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];

this.score = 0;
this.state = 'idle';

return this.initialState;
}

// add metodo de inicialização do jogo
isInitialStateAllZeros() {
return this.initialState.flat().every((value) => value === 0);
}

generateInitialState(isEmpty) {
const numberOfCellsToAdd = isEmpty ? 2 : 1;

return this.addNewCellRandomCell(this.initialState, numberOfCellsToAdd);
}

resetGamesState(newState) {
this.initialState = newState;
this.score = 0;
this.state = 'playing';
}

// add metodo de atualização do tabuleiro quando se move para a esquerda
processMoveLeft() {
const moveRows = this.initialState.map(this.mergeToTheLeft);
const withNewCell = this.addNewCellRandomCell(moveRows, 1);

return withNewCell;
}

// atualiza os movimentos independente da direção
updatedGameState(newState) {
this.state = this.loseWinState(newState);
this.score += this.calculateScore(this.initialState, newState);
this.initialState = newState;
}

// add metodo de atualização do tabuleiro quando se move para a direta
processMoveRight() {
const moveRows = this.initialState(this.mergeToTheRight);

Choose a reason for hiding this comment

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

Incorrect usage of initialState as a function. It should be this.initialState.map(this.mergeToTheRight).

const withNewCell = this.addNewCellRandomCell(moveRows, 1);

return withNewCell;
}

// add metodo de atualização do tabuleiro quando se move para cima
processMoveUp() {
const moveRows = this.initialState(this.mergeToTheUp);

Choose a reason for hiding this comment

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

Incorrect usage of initialState as a function. It should be this.initialState.map(this.mergeToTheUp).

const withNewCell = this.addNewCellRandomCell(moveRows, 1);

return withNewCell;
}

// add metodo de atualização do tabuleiro quando se move para baixo
processMoveDown() {
const moveRows = this.initialState(this.mergeToTheDown);

Choose a reason for hiding this comment

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

Incorrect usage of initialState as a function. It should be this.initialState.map(this.mergeToTheDown).

const withNewCell = this.addNewCellRandomCell(moveRows, 1);

return withNewCell;
}

// add novas celulas ao tabuleiro
addNewCellRandomCell(initialState, qtdCellAdd) {
const matrixInRow = initialState.flat();

const indexEvaluation = matrixInRow.forEach((value, index) => {
if (value === 0 && qtdCellAdd > 0) {
matrixInRow[index] = 1;
} else {
matrixInRow[index] = null;
}
});

if (indexEvaluation.length === 0) {
return initialState;
}
}
Comment on lines +197 to +211

Choose a reason for hiding this comment

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

The method addNewCellRandomCell does not correctly add new cells. It should randomly add a new cell with a value of 2 or 4 to an empty spot.


// verifica se perdeu ou ganhou
loseWinState() {}

Choose a reason for hiding this comment

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

The method loseWinState is not implemented. It should determine if the game is won or lost.


// verifca a pontuação
calculateScore() {}

Choose a reason for hiding this comment

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

The method calculateScore is not implemented. It should calculate the score based on the changes in the game state.


// mescla para a esquerda
mergeToTheLeft() {}

Choose a reason for hiding this comment

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

The method mergeToTheLeft is not implemented. It should handle merging tiles to the left.


// mescla para a direita
mergeToTheRight() {}

Choose a reason for hiding this comment

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

The method mergeToTheRight is not implemented. It should handle merging tiles to the right.


// mescla para cima
mergeToTheUp() {}

Choose a reason for hiding this comment

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

The method mergeToTheUp is not implemented. It should handle merging tiles upwards.


// Add your own methods here
// mescla para baixo
mergeToTheDown() {}

Choose a reason for hiding this comment

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

The method mergeToTheDown is not implemented. It should handle merging tiles downwards.

}

module.exports = Game;
3 changes: 2 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

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

Choose a reason for hiding this comment

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

Uncomment this line to import the Game class from the specified module.


// const game = new Game();

Choose a reason for hiding this comment

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

Uncomment this line to create an instance of the Game class.


// Write your code here
Loading