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

2048 the game #422

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

misha-shevchuk
Copy link

DEMO LINK


I've decided to use "keyup" event instead of "keydown" just in case to avoid continuous moves if press and hold any arrow key. Also, I`ve changed UI a little and added some sound effects for a better experience.

p.s. During development I have never won the game, maybe you will have more luck ;)

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Nice work 👍
Let's improve your code

return Math.random() < 0.9 ? 2 : 4;
}

function newCell() {

Choose a reason for hiding this comment

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

The condition 'clearCells.length === undefined' can be simplified to '!clearCells.length' which checks if the array is empty or not.


switch (direction) {
case 'ArrowUp':
for (const column of columns) {

Choose a reason for hiding this comment

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

Avoid using a for...of loop to iterate over an array. Instead, use forEach or map.

});
}

document.addEventListener('keyup', (evnt) => {

Choose a reason for hiding this comment

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

It's more efficient to listen to the keyup event only on the window or document object, not on the entire document. So you can change 'document.addEventListener' to 'window.addEventListener' or 'document.body.addEventListener'.

Copy link
Author

Choose a reason for hiding this comment

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

I've decided to change it back to 'keydown'. It feels more natural and more responsive while playing.

function canBeMarged() {
const wholeField = [...cellsInRow, ...columns];

for (const line of wholeField) {

Choose a reason for hiding this comment

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

Avoid using a for...of loop to iterate over an array. Instead, use forEach or map.

}

switch (direction) {
case 'ArrowUp':

Choose a reason for hiding this comment

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

Сreate a variable for these magic words

Copy link

@polosanya polosanya left a comment

Choose a reason for hiding this comment

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

It is possible to play after the loss. Make sure that you handle the losing correctly
P.S. Sounds feature is crazy 🔥 Well done!
loss

Comment on lines +184 to +188
case left:
cellsInRow.forEach((row) => {
flipLine([...row].reverse());
});
break;

Choose a reason for hiding this comment

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

Switch case should always have default case to handle possible errors

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Nice work 🔥

Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

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

Nice work 🔥

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.

3 participants