-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
resolution game_2048 #1072
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') { | ||
return; | ||
} | ||
|
||
const updatedState = this.processMoveLeft(); | ||
|
||
this.updatedGameState(updatedState); | ||
|
||
return this.initialState; | ||
} | ||
moveRight() { | ||
if (this.state === 'idle') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition should be |
||
return; | ||
} | ||
|
||
const updatedState = this.processMoveRight(); | ||
|
||
this.updatedGameState(updatedState); | ||
|
||
return this.initialState; | ||
} | ||
moveUp() { | ||
if (this.state === 'idle') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition should be |
||
return; | ||
} | ||
|
||
const updatedState = this.processMoveUp(); | ||
|
||
this.updatedGameState(updatedState); | ||
|
||
return this.initialState; | ||
} | ||
moveDown() { | ||
if (this.state === 'idle') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition should be |
||
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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect usage of |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect usage of |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect usage of |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// verifica se perdeu ou ganhou | ||
loseWinState() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// verifca a pontuação | ||
calculateScore() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// mescla para a esquerda | ||
mergeToTheLeft() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// mescla para a direita | ||
mergeToTheRight() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// mescla para cima | ||
mergeToTheUp() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
// Add your own methods here | ||
// mescla para baixo | ||
mergeToTheDown() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
} | ||
|
||
module.exports = Game; |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uncomment this line to import the |
||
|
||
// const game = new Game(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uncomment this line to create an instance of the |
||
|
||
// Write your code here |
There was a problem hiding this comment.
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.