-
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
Develop #1092
base: master
Are you sure you want to change the base?
Develop #1092
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 |
---|---|---|
@@ -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(); | ||
} | ||
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(); | ||
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. Same typo as above: |
||
this.addRandomTitle(); | ||
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. Same typo as above: |
||
} | ||
|
||
restart() { | ||
this.board = this.cells.map((row) => [...row]); | ||
this.score = 0; | ||
this.status = 'idle'; | ||
} | ||
} | ||
|
||
module.exports = Game; |
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.
There seems to be a typo in the method name
addRandomTitle
. It should likely beaddRandomTile
to reflect the action of adding a random tile to the board.