generated from mate-academy/gulp-template
-
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
add task solution #1118
Open
jonathanhora
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
jonathanhora: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 #1118
Changes from all commits
Commits
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
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,199 @@ | ||
'use strict'; | ||
|
||
// Uncomment the next lines to use your game instance in the browser | ||
// const Game = require('../modules/Game.class'); | ||
// const game = new Game(); | ||
class Game { | ||
static SIZE = 4; | ||
static TILE_PROBABILITY = 0.9; | ||
static WIN_TILE_VALUE = 2048; | ||
|
||
// Write your code here | ||
constructor(initialState = null) { | ||
this.score = 0; | ||
this.status = 'idle'; | ||
|
||
this.initialState = initialState | ||
? JSON.parse(JSON.stringify(initialState)) | ||
: null; | ||
|
||
this.state = initialState | ||
? JSON.parse(JSON.stringify(initialState)) | ||
: this.createEmptyBoard(); | ||
} | ||
|
||
createEmptyBoard() { | ||
return Array.from({ length: Game.SIZE }, () => Array(Game.SIZE).fill(0)); | ||
} | ||
|
||
addRandomTile() { | ||
const emptyTiles = []; | ||
|
||
for (let row = 0; row < Game.SIZE; row++) { | ||
for (let col = 0; col < Game.SIZE; col++) { | ||
if (this.state[row][col] === 0) { | ||
emptyTiles.push({ row, col }); | ||
} | ||
} | ||
} | ||
|
||
if (emptyTiles.length > 0) { | ||
const { row, col } = | ||
emptyTiles[Math.floor(Math.random() * emptyTiles.length)]; | ||
|
||
this.state[row][col] = Math.random() < Game.TILE_PROBABILITY ? 2 : 4; | ||
} | ||
} | ||
|
||
checkGameState() { | ||
if (this.state.some((row) => row.includes(Game.WIN_TILE_VALUE))) { | ||
this.status = 'win'; | ||
|
||
return; | ||
} | ||
|
||
if (!this.canMove()) { | ||
this.status = 'lose'; | ||
} | ||
} | ||
|
||
canMove() { | ||
for (let row = 0; row < Game.SIZE; row++) { | ||
for (let col = 0; col < Game.SIZE; col++) { | ||
if (this.state[row][col] === 0) { | ||
return true; | ||
} | ||
|
||
if ( | ||
(row < Game.SIZE - 1 && | ||
this.state[row][col] === this.state[row + 1][col]) || | ||
(col < Game.SIZE - 1 && | ||
this.state[row][col] === this.state[row][col + 1]) | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
move(direction) { | ||
if (this.status !== 'playing') { | ||
return; | ||
} | ||
|
||
const previousState = JSON.stringify(this.state); | ||
|
||
if (direction === 'left') { | ||
this.state = this.shiftLeft(this.state); | ||
} | ||
|
||
if (direction === 'right') { | ||
this.state = this.shiftRight(this.state); | ||
} | ||
|
||
if (direction === 'up') { | ||
this.state = this.shiftUp(this.state); | ||
} | ||
|
||
if (direction === 'down') { | ||
this.state = this.shiftDown(this.state); | ||
} | ||
|
||
if (JSON.stringify(this.state) !== previousState) { | ||
this.addRandomTile(); | ||
} | ||
|
||
this.checkGameState(); | ||
} | ||
|
||
shiftLeft(state) { | ||
return state.map((row) => this.compressRow(row)); | ||
} | ||
|
||
shiftRight(state) { | ||
return state.map((row) => this.compressRow([...row].reverse()).reverse()); | ||
} | ||
|
||
shiftUp(state) { | ||
const transposed = this.transpose(state); | ||
const shifted = this.shiftLeft(transposed); | ||
|
||
return this.transpose(shifted); | ||
} | ||
|
||
shiftDown(state) { | ||
const transposed = this.transpose(state); | ||
const shifted = this.shiftRight(transposed); | ||
|
||
return this.transpose(shifted); | ||
} | ||
|
||
compressRow(row) { | ||
const filtered = row.filter((value) => value !== 0); | ||
|
||
for (let i = 0; i < filtered.length - 1; i++) { | ||
if (filtered[i] === filtered[i + 1]) { | ||
filtered[i] *= 2; | ||
this.score += filtered[i]; | ||
filtered[i + 1] = 0; | ||
} | ||
} | ||
|
||
const filtered2 = filtered.filter((value) => value !== 0); | ||
|
||
return [...filtered2, ...Array(Game.SIZE - filtered2.length).fill(0)]; | ||
} | ||
|
||
transpose(matrix) { | ||
return matrix[0].map((_, colIndex) => matrix.map((row) => row[colIndex])); | ||
} | ||
|
||
moveLeft() { | ||
this.move('left'); | ||
} | ||
|
||
moveRight() { | ||
this.move('right'); | ||
} | ||
|
||
moveUp() { | ||
this.move('up'); | ||
} | ||
|
||
moveDown() { | ||
this.move('down'); | ||
} | ||
|
||
getState() { | ||
return this.state; | ||
} | ||
|
||
getScore() { | ||
return this.score; | ||
} | ||
|
||
getStatus() { | ||
return this.status; | ||
} | ||
|
||
start() { | ||
if (this.status === 'idle') { | ||
this.resetGame(); | ||
this.status = 'playing'; | ||
this.addRandomTile(); | ||
this.addRandomTile(); | ||
} | ||
} | ||
|
||
restart() { | ||
this.resetGame(); | ||
this.status = 'idle'; | ||
} | ||
|
||
resetGame() { | ||
this.state = this.initialState | ||
? JSON.parse(JSON.stringify(this.initialState)) | ||
: this.createEmptyBoard(); | ||
this.score = 0; | ||
} | ||
} | ||
|
||
module.exports = Game; | ||
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.
The use of
module.exports
suggests that this code is intended to be used with a module system like CommonJS, which is typically used in Node.js environments. If this script is intended to run in the browser, ensure that you are using a bundler like Webpack or a module loader that supports this syntax. Otherwise, consider using ES6 module syntax (export default Game;
) if your environment supports it.