-
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
Solution #7
base: master
Are you sure you want to change the base?
Solution #7
Conversation
src/scripts/main.js
Outdated
switch (allCell[k].innerText) { | ||
case '': | ||
allCell[k].className = 'field-cell'; | ||
break; | ||
case '2': | ||
allCell[k].classList.add('field-cell--2'); | ||
break; | ||
case '4': | ||
allCell[k].classList.add('field-cell--4'); | ||
break; | ||
case '8': | ||
allCell[k].classList.add('field-cell--8'); | ||
break; | ||
case '16': | ||
allCell[k].classList.add('field-cell--16'); | ||
break; | ||
case '32': | ||
allCell[k].classList.add('field-cell--32'); | ||
break; | ||
case '64': | ||
allCell[k].classList.add('field-cell--64'); | ||
break; | ||
case '128': | ||
allCell[k].classList.add('field-cell--128'); | ||
break; | ||
case '256': | ||
allCell[k].classList.add('field-cell--256'); | ||
break; | ||
case '512': | ||
allCell[k].classList.add('field-cell--512'); | ||
break; | ||
case '1024': | ||
allCell[k].classList.add('field-cell--1024'); | ||
break; | ||
case '2048': | ||
allCell[k].classList.add('field-cell--2048'); | ||
break; | ||
} |
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.
Автоматизуй. Робиться одним рядком
src/scripts/main.js
Outdated
} | ||
|
||
randomCell = generateRandomInteger(0, allEmptyCell.length - 1); | ||
newDiv.textContent = Math.random() >= 0.1 ? 2 : 4; |
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.
в такому разі шанс спавну 4 це 9.(9)%
src/scripts/main.js
Outdated
@@ -1,3 +1,447 @@ | |||
'use strict'; | |||
|
|||
// write your code here | |||
const tbody = document.querySelector('tbody'); |
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.
ти збираєшся textContent читати? Це не надійно, в такому разі можна просто підредагувати html і все.
тобі потрібен масив масивів (матриця)
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]
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.
на основі нього вже заповнювати твої клітинки на полі (метод render зробити який буде проеціювати твій масив на дошку)
Ну і далі всі операції робити з цим масивом, а не з html, html це просто для показу результатів і взаємодії зтвоєю підкапотною логікою через івенти
src/scripts/main.js
Outdated
const messStart = document.querySelector('.message-start'); | ||
let score = 0; | ||
const width = 4; | ||
let allEmptyCell = [...allCell].filter(item => item.classList.length < 2); |
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.
ні пусті клітинки це там де нулі в тебе в масиві
src/scripts/main.js
Outdated
} | ||
|
||
if (row[j].innerText === row[j - 1].innerText | ||
&& row[j].innerText !== '') { | ||
const totalValue = parseInt(row[j].innerText) | ||
+ parseInt(row[j - 1].innerText); | ||
|
||
score += totalValue; | ||
|
||
blockScore.innerText = score; | ||
|
||
isYouWin(); | ||
|
||
row[j].innerText = totalValue; | ||
row[j].className = 'field-cell'; | ||
row[j - 1].innerText = ''; | ||
row[j - 1].className = 'field-cell'; | ||
} else { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function combineRowLeft() { | ||
for (let i = 0; i < tbody.children.length; i++) { | ||
const row = tbody.children[i].children; | ||
|
||
for (let j = 0; j <= row.length; j++) { | ||
if (row[j + 1] === undefined) { | ||
break; | ||
} | ||
|
||
if (row[j].innerText === row[j + 1].innerText | ||
&& row[j].innerText !== '') { | ||
const totalValue = parseInt(row[j].innerText) | ||
+ parseInt(row[j + 1].innerText); | ||
|
||
score += totalValue; | ||
|
||
blockScore.innerText = score; | ||
|
||
isYouWin(); | ||
|
||
row[j].innerText = totalValue; | ||
row[j].className = 'field-cell'; | ||
row[j + 1].innerText = ''; | ||
row[j + 1].className = 'field-cell'; | ||
} else { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function combineColumnUp() { | ||
for (let i = 0; i < allCell.length - 4; i++) { | ||
if (allCell[i].innerText === allCell[i + width].innerText | ||
&& allCell[i].innerText !== '') { | ||
const totalValue = parseInt(allCell[i].innerText) | ||
+ parseInt(allCell[i + width].innerText); | ||
|
||
score += totalValue; | ||
|
||
blockScore.innerText = score; | ||
|
||
isYouWin(); | ||
|
||
allCell[i].innerText = totalValue; | ||
allCell[i].className = 'field-cell'; | ||
allCell[i + width].innerText = ''; | ||
allCell[i + width].className = 'field-cell'; | ||
} else { | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
function combineColumnDown() { | ||
for (let i = allCell.length - 1; i > 3; i--) { | ||
if (allCell[i].innerText === allCell[i - width].innerText | ||
&& allCell[i].innerText !== '') { | ||
const totalValue = parseInt(allCell[i].innerText) | ||
+ parseInt(allCell[i - width].innerText); | ||
|
||
score += totalValue; | ||
|
||
blockScore.innerText = score; | ||
|
||
isYouWin(); | ||
|
||
allCell[i].innerText = totalValue; | ||
allCell[i].className = 'field-cell'; | ||
allCell[i - width].innerText = ''; | ||
allCell[i - width].className = 'field-cell'; | ||
} else { | ||
continue; | ||
} | ||
} | ||
} |
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.
Підхід розвернути матрицю, в правильну сторону скласти і розвернути її назад більш стабільний і коду менше буде
Нажали вверх, розвернули нашу матрицю на 90 градусів, склали все методом combineRowRight і розвернули матрицю на 270 градусів назад,
Нажали вниз, розвернули на 270 градусів, склали все методом combineRowRight, розвернули на 90 і все.
Тобто можна все 2 методати зробити
метод rotateMatrix допишеш
src/scripts/main.js
Outdated
for (let i = 0; i <= tbody.children.length - 1;) { | ||
const row = tbody.children[i].children; | ||
|
||
for (let j = 0; j <= row.length - 1;) { | ||
if (tbody.children[i + 1] === undefined) { | ||
if (row[j].innerText === row[j + 1].innerText) { | ||
return false; | ||
} | ||
} | ||
|
||
if (i === 3) { | ||
if (row[j].innerText === row[j + 1].innerText | ||
|| row[j + 1].innerText === row[j + 2].innerText | ||
|| row[j + 2].innerText === row[j + 3].innerText) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (j === 3) { | ||
if (row[j].innerText === tbody.children[i + 1].children[j].innerText | ||
|| row[j].innerText === tbody.children[i + 1].children[j].innerText) { | ||
return false; | ||
} | ||
j++; | ||
continue; | ||
} | ||
|
||
if (row[j].innerText === row[j + 1].innerText | ||
|| row[j].innerText === tbody.children[i + 1].children[j].innerText) { | ||
return false; | ||
} | ||
|
||
j++; | ||
} | ||
i++; | ||
} | ||
|
||
return true; |
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.
певен можна простіше, коли перепишеш на матричне рішення задачі буде видно як тут можна спростити ситуацію
DEMO LINK