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

I am having issues with the num generator not always seeing that a sq… #550

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_2048_game/)
- [DEMO LINK](https://spencerspe.github.io/js_2048_game/)
2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
Expand All @@ -10,7 +10,7 @@
Hey! Are you ready for a real hard check of your JavaScript skills, ninja?
If you are still here, let's do it.

In this task, you need to implement the 2048 game like in [this reference](https://play2048.co/)
In this task, you need to implement the 2048 game like in [this reference] )
Don't play for too long! We need you to write the code!

Okay, what do we have?
Expand Down
129 changes: 129 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,132 @@
'use strict';

// write your code here

const field = document.querySelectorAll('.field-cell');
const tableRows = document.querySelectorAll('tr');

function numGenerator(max) {
return Math.floor(Math.random() * max);
}

function startPosition() {
const newNumber = numGenerator(16);

const fieldToCheck = field;

if (fieldToCheck[newNumber].textContent !== '') {
startPosition();
};

return newNumber;
};

function startNumGenerator() {
let startNum = 2;
const isItAFour = numGenerator(10);

if (isItAFour === 4) {
startNum = 4;
}

return startNum;
}

function addNumber() {
const squareForNewNm = startPosition();

field[squareForNewNm].textContent = startNumGenerator();
}
addNumber();
addNumber();

function upDownCheck(direction, addOrCheck) {
for (let i = 0; i < 3; i++) {
let fieldToCheck = Array.from(field);

if (direction === 'up') {
fieldToCheck = fieldToCheck.slice(4).reverse();
}

if (direction === 'down') {
fieldToCheck = fieldToCheck.slice(0, -4);
};

const reverseIndex = fieldToCheck.map((item, index) => index).reverse();

fieldToCheck.forEach((item, index) => {
const inPutIndex = direction === 'up' ? reverseIndex[index] : index + 4;

if (field[inPutIndex].textContent === '') {
field[inPutIndex].textContent = item.textContent;
item.textContent = '';
}

if (direction === 'down'
&& field[inPutIndex].textContent === field[index].textContent
&& field[inPutIndex].textContent !== '') {
field[inPutIndex].textContent = 2 * field[index].textContent;
field[index].textContent = '';
}

if (direction === 'up'
&& field[inPutIndex].textContent === field[inPutIndex + 4].textContent
&& field[inPutIndex].textContent !== '') {
field[inPutIndex].textContent = 2 * field[inPutIndex].textContent;
field[inPutIndex + 4].textContent = '';
}
});
}
};

function leftRightHmm(reverse) {
for (let i = 0; i < 3; i++) {
tableRows.forEach(item => {
let oddNums = ([...item.childNodes].filter((quarter, index) => {
if (index % 2 !== 0) {
return quarter;
}
}));

if (reverse) {
oddNums = oddNums.reverse();
};

oddNums.forEach((square, index) => {
if (index <= 2 && oddNums[index + 1].textContent === '') {
oddNums[index + 1].textContent = square.textContent;
square.textContent = '';
}

if (index <= 2 && oddNums[index].textContent
=== oddNums[index + 1].textContent
&& oddNums[index].textContent !== '') {
oddNums[index + 1].textContent = 2 * oddNums[index + 1].textContent;
oddNums[index].textContent = '';
}
});
});
}
}

document.addEventListener('keydown', (evt) => {
if (evt.key === 'ArrowDown') {
upDownCheck('down');
}

if (evt.key === 'ArrowUp') {
upDownCheck('up');
}

if (evt.key === 'ArrowLeft') {
leftRightHmm('reverse');
}

if (evt.key === 'ArrowRight') {
leftRightHmm();
}

if (evt.key === ' ') {
addNumber();
}
});
Loading