generated from mate-academy/gulp-template
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
first commit #1172
Open
SerhiiUnhurian
wants to merge
7
commits into
mate-academy:master
Choose a base branch
from
SerhiiUnhurian:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
first commit #1172
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74787cb
first commit
SerhiiUnhurian f3dc9bb
first commit1
SerhiiUnhurian 3f0fa25
solution
SerhiiUnhurian 1868ce7
solution
SerhiiUnhurian 1da7d27
solution
SerhiiUnhurian b5fda33
solution
SerhiiUnhurian 5b31c75
solution
SerhiiUnhurian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,25 +20,103 @@ class Game { | |
* If passed, the board will be initialized with the provided | ||
* initial state. | ||
*/ | ||
constructor(initialState) { | ||
constructor(initialState = null) { | ||
// eslint-disable-next-line no-console | ||
console.log(initialState); | ||
|
||
this.size = 4; | ||
this.score = 0; | ||
this.status = 'idle'; | ||
|
||
this.board = initialState | ||
? initialState.map((row) => [...row]) | ||
: this.createEmptyBoard(); | ||
} | ||
|
||
createEmptyBoard() { | ||
return Array.from({ length: this.size }, () => Array(this.size).fill(0)); | ||
} | ||
|
||
moveLeft() {} | ||
moveRight() {} | ||
moveUp() {} | ||
moveDown() {} | ||
moveLeft() { | ||
if (this.getStatus() === 'playing') { | ||
let moved = false; | ||
|
||
for (let row = 0; row < this.board.length; row++) { | ||
let newRow = this.board[row].filter((tile) => tile !== 0); | ||
|
||
for (let col = 0; col < newRow.length - 1; col++) { | ||
if (newRow[col] !== 0 && newRow[col] === newRow[col + 1]) { | ||
newRow[col] *= 2; | ||
newRow[col + 1] = 0; | ||
this.score += newRow[col]; | ||
moved = true; | ||
} | ||
} | ||
newRow = newRow.filter((tile) => tile !== 0); | ||
|
||
while (newRow.length < this.board[row].length) { | ||
newRow.push(0); | ||
} | ||
|
||
if (this.board[row].toString() !== newRow.toString()) { | ||
moved = true; | ||
} | ||
this.board[row] = newRow; | ||
} | ||
|
||
if (moved) { | ||
this.createRandomTile(); | ||
this.checkGameStatus(); | ||
// this.updateTable(); | ||
} | ||
|
||
return moved; | ||
} | ||
} | ||
|
||
moveRight() { | ||
if (this.getStatus() === 'playing') { | ||
this.board = this.board.map((row) => row.reverse()); | ||
this.moveLeft(); | ||
this.board = this.board.map((row) => row.reverse()); | ||
} | ||
} | ||
moveUp() { | ||
if (this.getStatus() === 'playing') { | ||
this.transposeBoard(); | ||
this.moveLeft(); | ||
this.transposeBoard(); | ||
} | ||
} | ||
moveDown() { | ||
if (this.getStatus() === 'playing') { | ||
this.transposeBoard(); | ||
this.moveRight(); | ||
this.transposeBoard(); | ||
} | ||
} | ||
|
||
transposeBoard() { | ||
this.board = this.board[0].map((_, colIndex) => | ||
// eslint-disable-next-line prettier/prettier | ||
this.board.map((row) => row[colIndex])); | ||
|
||
return this.board; | ||
} | ||
|
||
/** | ||
* @returns {number} | ||
*/ | ||
getScore() {} | ||
getScore() { | ||
return this.score; | ||
} | ||
|
||
/** | ||
* @returns {number[][]} | ||
*/ | ||
getState() {} | ||
getState() { | ||
return this.board.map((row) => [...row]); | ||
} | ||
|
||
/** | ||
* Returns the current game status. | ||
|
@@ -50,19 +128,163 @@ class Game { | |
* `win` - the game is won; | ||
* `lose` - the game is lost | ||
*/ | ||
getStatus() {} | ||
getStatus() { | ||
return this.status; | ||
} | ||
|
||
initGame() { | ||
this.board = this.createEmptyBoard(); | ||
this.createRandomTile(); | ||
this.createRandomTile(); | ||
this.status = 'playing'; | ||
// this.updateTable(); | ||
} | ||
|
||
/** | ||
* Starts the game. | ||
*/ | ||
start() {} | ||
start() { | ||
this.initGame(); | ||
} | ||
|
||
/** | ||
* Resets the game. | ||
*/ | ||
restart() {} | ||
restart() { | ||
this.initGame(); | ||
this.score = 0; | ||
} | ||
|
||
// Add your own methods here | ||
|
||
randomNumber(max) { | ||
return Math.floor(Math.random() * (max + 1)); | ||
} | ||
|
||
generateCellValue() { | ||
return Math.random() < 0.9 ? 2 : 4; | ||
} | ||
|
||
// updateTable() { | ||
// const cells = document.querySelectorAll('.field-cell'); | ||
// let index = 0; | ||
|
||
// this.board.forEach((row) => { | ||
// row.forEach((cellValue) => { | ||
// const cell = cells[index++]; | ||
|
||
// cell.textContent = cellValue !== 0 ? cellValue : ''; | ||
// eslint-disable-next-line max-len | ||
// cell.className = `field-cell ${cellValue ? 'field-cell--' + cellValue : ''}`; | ||
// }); | ||
// }); | ||
// } | ||
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. Remove all comments |
||
|
||
createRandomTile() { | ||
const emptyCells = []; | ||
|
||
// eslint-disable-next-line no-shadow | ||
this.board.forEach((row, rowIndex) => { | ||
// eslint-disable-next-line no-shadow | ||
row.forEach((cell, cellIndex) => { | ||
if (cell === 0) { | ||
emptyCells.push({ rowIndex, cellIndex }); | ||
} | ||
}); | ||
}); | ||
|
||
if (emptyCells.length === 0) { | ||
return; | ||
} | ||
|
||
const randomIndex = this.randomNumber(emptyCells.length - 1); | ||
const { rowIndex, cellIndex } = emptyCells[randomIndex]; | ||
|
||
const newBoard = this.board.map((row) => row.slice()); | ||
|
||
newBoard[rowIndex][cellIndex] = this.generateCellValue(); | ||
this.board = newBoard; | ||
// this.board[rowIndex][cellIndex] = this.generateCellValue(); | ||
// this.updateTable(); | ||
} | ||
|
||
checkGameStatus() { | ||
for (const row of this.board) { | ||
if (row.includes(2048)) { | ||
this.status = 'win'; | ||
|
||
return; | ||
} | ||
} | ||
|
||
for (const row of this.board) { | ||
if (row.includes(0)) { | ||
return; | ||
} | ||
} | ||
|
||
if (!this.canMakeMove()) { | ||
this.status = 'lose'; | ||
} | ||
} | ||
|
||
canMakeMove() { | ||
for (let row = 0; row < this.size; row++) { | ||
for (let col = 0; col < this.size; col++) { | ||
if (this.board[row][col] === 0) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
for (let row = 0; row < this.size; row++) { | ||
for (let col = 0; col < this.size - 1; col++) { | ||
if ( | ||
(this.board[row][col] === this.board[row][col + 1] && | ||
this.board[row][col] !== 0) || | ||
(this.board[row][col] !== 0 && this.hasMergeableTilesInRow(row)) | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
for (let col = 0; col < this.size; col++) { | ||
for (let row = 0; row < this.size - 1; row++) { | ||
if (this.board[row][col] === this.board[row + 1][col]) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
hasMergeableTilesInRow(row) { | ||
const filteredRow = this.board[row].filter((value) => value !== 0); | ||
|
||
for (let i = 0; i < filteredRow.length - 1; i++) { | ||
if (filteredRow[i] === filteredRow[i + 1]) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
hasMergeableTilesInColumn(col) { | ||
const filteredCol = this.board.map((row) => | ||
// eslint-disable-next-line prettier/prettier | ||
row[col].filter((val) => val !== 0)); | ||
|
||
for (let i = 0; i < filteredCol.length - 1; i++) { | ||
if (filteredCol[i] === filteredCol[i + 1]) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
module.exports = Game; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
after restart game board should be without any tiles.