generated from mate-academy/gulp-template
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
add task solution #983
Open
1luki9901
wants to merge
8
commits into
mate-academy:master
Choose a base branch
from
1luki9901: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
add task solution #983
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0827f68
add task solution
1luki9901 8e8b911
add task solution
1luki9901 ea00f5f
add task solution
1luki9901 1183e29
add task solution
1luki9901 f37c7d5
add task solution
1luki9901 550f6cb
add task solution
1luki9901 faeb265
add task solution
1luki9901 2743aaa
add task solution
1luki9901 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,146 @@ | ||
'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(initialState = null) { | ||
this.size = 4; | ||
this.board = initialState || this.createEmptyBoard(); | ||
this.score = 0; | ||
this.status = 'idle'; | ||
} | ||
|
||
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 | ||
createEmptyBoard() { | ||
return Array.from({ length: this.size }, () => Array(this.size).fill(0)); | ||
} | ||
|
||
start() { | ||
this.status = 'playing'; | ||
this.addRandomTile(); | ||
this.addRandomTile(); | ||
} | ||
|
||
restart() { | ||
this.board = this.createEmptyBoard(); | ||
this.score = 0; | ||
this.status = 'idle'; | ||
this.start(); | ||
} | ||
|
||
addRandomTile() { | ||
const emptyCells = []; | ||
|
||
// eslint-disable-next-line no-shadow | ||
for (let r = 0; r < this.size; r++) { | ||
// eslint-disable-next-line no-shadow | ||
for (let c = 0; c < this.size; c++) { | ||
if (this.board[r][c] === 0) { | ||
emptyCells.push({ r, c }); | ||
} | ||
} | ||
} | ||
|
||
if (emptyCells.length === 0) { | ||
return; | ||
} | ||
|
||
const { r, c } = emptyCells[Math.floor(Math.random() * emptyCells.length)]; | ||
|
||
this.board[r][c] = Math.random() < 0.9 ? 2 : 4; | ||
} | ||
|
||
moveLeft() { | ||
let moved = false; | ||
|
||
for (let r = 0; r < this.size; r++) { | ||
const row = this.board[r].filter((val) => val); | ||
|
||
for (let i = 0; i < row.length - 1; i++) { | ||
if (row[i] === row[i + 1]) { | ||
row[i] *= 2; | ||
this.score += row[i]; | ||
row.splice(i + 1, 1); | ||
row.push(0); | ||
} | ||
} | ||
|
||
while (row.length < this.size) { | ||
row.push(0); | ||
} | ||
|
||
if (this.board[r].toString() !== row.toString()) { | ||
moved = true; | ||
} | ||
this.board[r] = row; | ||
} | ||
|
||
if (moved) { | ||
this.addRandomTile(); | ||
} | ||
} | ||
|
||
moveRight() { | ||
this.board.forEach((row) => row.reverse()); | ||
this.moveLeft(); | ||
this.board.forEach((row) => row.reverse()); | ||
} | ||
|
||
moveUp() { | ||
this.transpose(); | ||
this.moveLeft(); | ||
this.transpose(); | ||
} | ||
|
||
moveDown() { | ||
this.transpose(); | ||
this.moveRight(); | ||
this.transpose(); | ||
} | ||
|
||
transpose() { | ||
this.board = this.board[0].map( | ||
(_, colIndex) => this.board.map((row) => row[colIndex]), | ||
// eslint-disable-next-line function-paren-newline | ||
); | ||
} | ||
|
||
getScore() { | ||
return this.score; | ||
} | ||
|
||
getState() { | ||
return this.board; | ||
} | ||
|
||
getStatus() { | ||
if (this.board.flat().includes(2048)) { | ||
return 'win'; | ||
} | ||
|
||
if (!this.canMove()) { | ||
return 'lose'; | ||
} | ||
|
||
return this.status; | ||
} | ||
|
||
canMove() { | ||
for (let r = 0; r < this.size; r++) { | ||
for (let c = 0; c < this.size; c++) { | ||
if (this.board[r][c] === 0) { | ||
return true; | ||
} | ||
|
||
if (c < this.size - 1 && this.board[r][c] === this.board[r][c + 1]) { | ||
return true; | ||
} | ||
|
||
if (r < this.size - 1 && this.board[r][c] === this.board[r + 1][c]) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
module.exports = Game; |
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 |
---|---|---|
@@ -1,7 +1,70 @@ | ||
'use strict'; | ||
// const Game = require('../modules/Game.class'); | ||
|
||
// Uncomment the next lines to use your game instance in the browser | ||
// const Game = require('../modules/Game.class'); | ||
// const game = new Game(); | ||
const Game = require('../modules/Game.class'); | ||
const game = new Game(); | ||
|
||
// Write your code here | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const startButton = document.getElementById('start-button'); | ||
const scoreElement = document.getElementById('score'); | ||
const statusElement = document.getElementById('status'); | ||
const gameBoard = document.getElementById('game-board'); | ||
const cells = gameBoard.getElementsByClassName('field-cell'); | ||
|
||
let previousState = []; | ||
|
||
function updateUI() { | ||
const state = game.getState(); | ||
|
||
for (let i = 0; i < cells.length; i++) { | ||
const cell = cells[i]; | ||
const value = state[Math.floor(i / 4)][i % 4]; | ||
|
||
if (previousState[i] !== value) { | ||
cell.className = 'field-cell'; | ||
|
||
if (value) { | ||
cell.classList.add(`field-cell--${value}`); | ||
cell.textContent = value; | ||
} else { | ||
cell.textContent = ''; | ||
} | ||
} | ||
} | ||
previousState = state.flat(); | ||
scoreElement.textContent = game.getScore(); | ||
statusElement.textContent = game.getStatus(); | ||
} | ||
|
||
startButton.addEventListener('click', () => { | ||
if (game.getStatus() === 'playing') { | ||
game.restart(); | ||
} else { | ||
game.start(); | ||
} | ||
updateUI(); | ||
|
||
startButton.textContent = | ||
game.getStatus() === 'playing' ? 'Restart' : 'Start'; | ||
}); | ||
|
||
const keyMap = { | ||
ArrowLeft: () => game.moveLeft(), | ||
ArrowRight: () => game.moveRight(), | ||
ArrowUp: () => game.moveUp(), | ||
ArrowDown: () => game.moveDown(), | ||
}; | ||
|
||
// eslint-disable-next-line no-shadow | ||
document.addEventListener('keydown', (event) => { | ||
const moveFunction = keyMap[event.key]; | ||
|
||
if (moveFunction && moveFunction()) { | ||
updateUI(); | ||
} | ||
}); | ||
|
||
updateUI(); | ||
}); |
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.
Where is that file?