-
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
Implementation #9
base: master
Are you sure you want to change the base?
Implementation #9
Conversation
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.
погано що в тебе поразка коли робиш спробу походити кудись, коли вже ясно що ти програв і видно що ходів немає
Що до спрощення, гру можна було написати на класах і не писати для кожного ходу свою функцію, а розвертати матрицю в правильноу напрямку, збирати до купи - і тоді розвертати як було до того
matrixModel[row - 1][column] = 0; | ||
isMoved = true; | ||
} | ||
} | ||
|
||
if (isMoved) { | ||
return moveDown(); | ||
} | ||
} | ||
} | ||
|
||
// combine cells if they are equal and then call move function | ||
function combineDown() { | ||
for (let column = 0; column < size; column++) { | ||
for (let row = size - 1; row >= 1; row--) { | ||
if (matrixModel[row - 1][column] === matrixModel[row][column] | ||
&& matrixModel[row][column] > 0) { | ||
matrixModel[row - 1][column] *= 2; | ||
matrixModel[row][column] = 0; | ||
currentScore += matrixModel[row - 1][column]; | ||
scoreContainer.textContent = currentScore; | ||
moveDown(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// cells of each row move left untill | ||
// all existed moves in current row be done | ||
function moveLeft() { | ||
for (let row = 0; row < size; row++) { | ||
let isMoved = false; | ||
|
||
for (let column = 1; column < size; column++) { | ||
if (matrixModel[row][column - 1] === 0 | ||
&& matrixModel[row][column] > 0) { | ||
matrixModel[row][column - 1] = matrixModel[row][column]; | ||
matrixModel[row][column] = 0; | ||
isMoved = true; | ||
} | ||
} | ||
|
||
if (isMoved) { | ||
return moveLeft(); | ||
} | ||
} | ||
} | ||
|
||
// combine cells if they are equal and then call move function | ||
function combineLeft() { | ||
for (let row = 0; row < size; row++) { | ||
for (let column = 1; column < size; column++) { | ||
if (matrixModel[row][column - 1] === matrixModel[row][column] | ||
&& matrixModel[row][column] > 0) { | ||
matrixModel[row][column - 1] *= 2; | ||
matrixModel[row][column] = 0; | ||
currentScore += matrixModel[row][column - 1]; | ||
scoreContainer.textContent = currentScore; | ||
moveLeft(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// cells of each row move right untill | ||
// all existed moves in current row be done | ||
function moveRight() { | ||
for (let row = 0; row < size; row++) { | ||
let isMoved = false; | ||
|
||
for (let column = size - 1; column >= 1; column--) { | ||
if (matrixModel[row][column] === 0 | ||
&& matrixModel[row][column - 1] > 0) { | ||
matrixModel[row][column] = matrixModel[row][column - 1]; | ||
matrixModel[row][column - 1] = 0; | ||
isMoved = true; | ||
} | ||
} | ||
|
||
if (isMoved) { | ||
return moveRight(); | ||
} | ||
} | ||
} | ||
|
||
// combine cells if they are equal and then call move functiongi | ||
function combineRight() { | ||
for (let row = 0; row < size; row++) { | ||
for (let column = size - 1; column >= 1; column--) { | ||
if (matrixModel[row][column - 1] === matrixModel[row][column] | ||
&& matrixModel[row][column] > 0) { | ||
matrixModel[row][column - 1] *= 2; | ||
matrixModel[row][column] = 0; | ||
currentScore += matrixModel[row][column - 1]; | ||
scoreContainer.textContent = currentScore; | ||
moveRight(row, column); | ||
} | ||
} | ||
} | ||
} |
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.
я певен це все можна дуже спростити, але реалізація непогана, код достатньо чистий, мені подобається
if (event.keyCode === 38) { | ||
if (canMoveU()) { | ||
moveUp(); | ||
combineUp(); | ||
generate(); | ||
render(); | ||
} | ||
} else if (event.keyCode === 40) { | ||
if (canMoveD()) { | ||
moveDown(); | ||
combineDown(); | ||
generate(); | ||
render(); | ||
} | ||
} else if (event.keyCode === 37) { | ||
if (canMoveL()) { | ||
moveLeft(); | ||
combineLeft(); | ||
generate(); | ||
render(); | ||
} | ||
} else if (event.keyCode === 39) { | ||
if (canMoveR()) { | ||
moveRight(); | ||
combineRight(); | ||
generate(); | ||
render(); | ||
} | ||
} | ||
} |
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.
switch case
DEMO LINK - https://DmytroArkhypenko.github.io/js_2048_game/