Aug 12, 2022
0
Level 1
2048 game - CheckforGameOver() need help to code
Hey guys, I've watched a tutorial to code the 2048 game with JS and in the CheckforLose method. There's a slight error, here the game is over when there are no cells that equals to 0.
function checkForLose(){
let nul = 0
for(let i = 0; i < squares.length; i++){
if(squares[i].innerHTML == 0){
nul++
}
}
if(nul === 0) {
document.removeEventListener('keyup', control)
popup()
popUpTitle.innerHTML = 'You Lose !'
}
}```
But in the real game , it's possible to still combine the rows and columns. I wanted to improve the code but I don't know how to code it:
I used Arrays to generate the game.
```let squares = [];
function generate() {
let randomNumber = Math.floor(Math.random()*squares.length);
const random = [2, 4];
if (squares[randomNumber].innerHTML == 0){
squares[randomNumber].innerHTML = random[Math.floor(Math.random() * random.length)];
checkForLose()
} else generate()
}```
I would like that the game stops when there are no moves available
So I would have to take the grid that exists and simulate the moves (left, right, up and down) and if the grid stays the same then the game ends.
Can someone help with this please ?
Thanks a lot
Please or to participate in this conversation.