Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DmytroArkhypenko
Copy link

@vpolets
Copy link
Contributor

vpolets commented Oct 12, 2020

я був дуже близько )
image

Copy link
Contributor

@vpolets vpolets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

погано що в тебе поразка коли робиш спробу походити кудись, коли вже ясно що ти програв і видно що ходів немає
Що до спрощення, гру можна було написати на класах і не писати для кожного ходу свою функцію, а розвертати матрицю в правильноу напрямку, збирати до купи - і тоді розвертати як було до того

Comment on lines +216 to +360
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);
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я певен це все можна дуже спростити, але реалізація непогана, код достатньо чистий, мені подобається

Comment on lines +183 to +212
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();
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants