generated from mate-academy/gulp-template
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
game #425
Open
diana-tuz
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
diana-tuz:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
game #425
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,201 @@ | ||
'use strict'; | ||
|
||
// write your code here | ||
const rows = [...document.querySelectorAll('.field-row')]; | ||
const columns = [[], [], [], []]; | ||
const cells = document.querySelectorAll('.field-cell'); | ||
const cellsInRow = rows.map((row) => [...row.querySelectorAll('.field-cell')]); | ||
const startCellVelues = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]; | ||
|
||
const startButton = document.querySelector('.start'); | ||
const messageStart = document.querySelector('.message-start'); | ||
const messageLose = document.querySelector('.message-lose'); | ||
const messageWin = document.querySelector('.message-win'); | ||
const score = document.querySelector('.game-score'); | ||
|
||
for (let i = 0; i < 4; i++) { | ||
for (let j = 0; j < 4; j++) { | ||
columns[j].push(rows[i].children[j]); | ||
} | ||
} | ||
|
||
let clearCells = [...cells]; | ||
let started = false; | ||
let rotated = false; | ||
|
||
const randomCellFill = () => { | ||
if (!clearCells.length) { | ||
return; | ||
} | ||
|
||
const randomIndex = (someArr) => Math.floor(Math.random() * someArr.length); | ||
const randomCell = clearCells[randomIndex(clearCells)]; | ||
const randomValue = startCellVelues[randomIndex(startCellVelues)]; | ||
|
||
randomCell.classList = (`field-cell field-cell--${randomValue}`); | ||
randomCell.innerText = randomValue; | ||
|
||
clearCells.splice(clearCells.indexOf(randomCell), 1); | ||
}; | ||
|
||
const start = () => { | ||
messageStart.style = 'display: none;'; | ||
startButton.classList = 'button restart'; | ||
startButton.innerText = 'Restart'; | ||
|
||
randomCellFill(); | ||
randomCellFill(); | ||
started = true; | ||
}; | ||
|
||
const restart = () => { | ||
messageLose.classList.add('hidden'); | ||
messageWin.classList.add('hidden'); | ||
clearCells = [...cells]; | ||
score.innerText = 0; | ||
|
||
cells.forEach(cell => { | ||
cell.innerText = ''; | ||
cell.className = 'field-cell'; | ||
}); | ||
|
||
randomCellFill(); | ||
randomCellFill(); | ||
}; | ||
|
||
const isMergePossible = () => { | ||
const wholeField = [...cellsInRow, ...columns]; | ||
|
||
for (const line of wholeField) { | ||
for (let i = 0; i < 3; i++) { | ||
if (line[i].innerText === line[i + 1].innerText) { | ||
return true; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const deleteCell = (element) => { | ||
element.innerText = ''; | ||
element.className = 'field-cell'; | ||
clearCells.push(element); | ||
}; | ||
|
||
const cellMerge = (curr, prev) => { | ||
const value = curr.innerText * 2; | ||
|
||
curr.innerText = value; | ||
curr.className = `field-cell field-cell--${value}`; | ||
score.innerText = +score.innerText + value; | ||
deleteCell(prev); | ||
|
||
curr.dataset.blocked = true; | ||
prev.dataset.blocked = true; | ||
|
||
if (value === 2048) { | ||
messageWin.classList.remove('hidden'); | ||
} | ||
}; | ||
|
||
const rotateCell = (curr, prev) => { | ||
curr.innerText = prev.innerText; | ||
curr.className = prev.className; | ||
clearCells.splice(clearCells.indexOf(curr), 1); | ||
deleteCell(prev); | ||
}; | ||
|
||
const arrowUp = () => { | ||
for (const column of columns) { | ||
rotateCells([...column].reverse()); | ||
} | ||
}; | ||
|
||
const arrowDown = () => { | ||
for (const column of columns) { | ||
rotateCells(column); | ||
} | ||
}; | ||
|
||
const arrowRight = () => { | ||
for (const row of cellsInRow) { | ||
rotateCells(row); | ||
} | ||
}; | ||
|
||
const arrowLeft = () => { | ||
for (const row of cellsInRow) { | ||
rotateCells([...row].reverse()); | ||
} | ||
}; | ||
|
||
const arrowMove = (direction) => { | ||
if (!clearCells.length && !isMergePossible()) { | ||
messageLose.classList.remove('hidden'); | ||
} | ||
|
||
switch (direction) { | ||
case 'ArrowUp': | ||
arrowUp(); | ||
break; | ||
case 'ArrowDown': | ||
arrowDown(); | ||
break; | ||
case 'ArrowRight': | ||
arrowRight(); | ||
break; | ||
case 'ArrowLeft': | ||
arrowLeft(); | ||
break; | ||
default: break; | ||
} | ||
|
||
cells.forEach(cell => { | ||
cell.removeAttribute('data-blocked'); | ||
}); | ||
}; | ||
|
||
const rotateCells = (line) => { | ||
for (let i = 3; i > 0; i--) { | ||
const isMoveAllowed = clearCells.includes(line[i]) | ||
&& !clearCells.includes(line[i - 1]); | ||
|
||
const mergeAllowed = line[i].innerText === line[i - 1].innerText | ||
&& line[i].innerText.length | ||
&& !line[i - 1].dataset.blocked; | ||
|
||
if ((mergeAllowed || isMoveAllowed) && !rotated) { | ||
rotated = true; | ||
} | ||
|
||
if (mergeAllowed) { | ||
cellMerge(line[i], line[i - 1]); | ||
rotateCells(line); | ||
} | ||
|
||
if (isMoveAllowed) { | ||
rotateCell(line[i], line[i - 1]); | ||
rotateCells(line); | ||
} | ||
} | ||
}; | ||
|
||
document.addEventListener('keydown', (evt) => { | ||
const arrowDirections = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight']; | ||
|
||
if (arrowDirections.includes(evt.key)) { | ||
evt.preventDefault(); | ||
arrowMove(evt.key); | ||
|
||
if (rotated) { | ||
randomCellFill(); | ||
rotated = false; | ||
} | ||
} | ||
}); | ||
|
||
startButton.addEventListener('click', () => { | ||
if (!started) { | ||
start(); | ||
} else { | ||
restart(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should always have a default case to handle possible errors