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

Enhancements for 2048 Game Experience through Animation and Modal Int… #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
293 changes: 29 additions & 264 deletions Web development/2048 Game/app.js
Original file line number Diff line number Diff line change
@@ -1,270 +1,35 @@

const gameBoard =document.querySelector(".gameBoard")

const userScore= document.querySelector(".points")

let score ;
let filledCell;
let endGame ;
let highScore=0;

startGame();
newPosition();

document.querySelector(".newPlay").addEventListener("click",function()
{
gameBoard.innerHTML="";
startGame();
newPosition();

console.log(filledCell);
// Create and append the modal to the body
const modal = document.createElement('div');
modal.classList.add('modal');
modal.innerHTML = `
<div class="modal-content">
<h2>Game Over</h2>
<p>Your Score: <span class="points"></span></p>
<button class="close-button">Close</button>
</div>
`;
document.body.appendChild(modal);

// Show modal when the game is over
function showModal() {
modal.style.display = 'block';
document.querySelector('.points').innerHTML = score; // Display score in modal
}

// Add event listener to close button
modal.querySelector('.close-button').addEventListener('click', () => {
modal.style.display = 'none';
});

function startGame ()
{
score =0;
userScore.innerHTML=score;
filledCell=[[0 , 0 , 0 , 0] , [0 , 0 , 0 , 0]
,[0 , 0 , 0 , 0],[0 , 0 , 0 , 0]];
//array to mark cell with value


for(let j=0;j<4;j++)
{
for(let k=0;k<4;k++)
{
let cell=document.createElement("div");
cell.id=`index${j}-${k}`;
gameBoard.append(cell);

updateValue(j,k);
}
}

}

// function to genrate a new cell value i.e 2 or 4
function generate () {

let newValue =Math.floor(Math.random()*2);
if(newValue===0)
{ return 2;}
else
{ return 4;}
}


// function to get a new position for new cell
function newPosition (){

if(canPlay()===1)
{
let newi =Math.floor(Math.random()*4);
let newj =Math.floor(Math.random()*4);

if(filledCell[newi][newj]!=0)
{
newPosition();
}
else
filledCell[newi][newj]=generate();
updateValue(newi,newj);
}
else alert("Well played ! but you loose start new game by New Game button ");
}


document.querySelector("body").addEventListener('keyup',(e)=>{
// console.log(e.key);
switch(e.key)
{
case "ArrowUp":

moveUp();
newPosition();
updateHighScore ();
break

case "ArrowDown":

moveDown();
newPosition();
updateHighScore ();
break

case "ArrowRight":
moveRight();
newPosition();
updateHighScore ();
break

case "ArrowLeft":
moveLeft();
newPosition();
updateHighScore ();
break
}
});


function updateValue(i,j)
{
let cell=document.querySelector(`#index${i}-${j}`);
if(filledCell[i][j]>0)
{

cell.innerHTML=filledCell[i][j];
cell.classList.add(`value${filledCell[i][j]}`);
// console.log("value"+filledCell[i][j]);
}
else
{
document.querySelector(`#index${i}-${j}`).innerHTML="";
cell.className="";
}
}

function moveUp()
{
for(let j=0;j<4;j++)
{
// console.log("-- 555555555--");
for(let i=3;i>0;i--)
{
// console.log(filledCell[i][j]);
// console.log("----");
if(filledCell[i][j]===filledCell[i-1][j] && filledCell[i][j])
{
// merge
filledCell[i-1][j]=2*filledCell[i][j];
filledCell[i][j]=0;
score=score+filledCell[i-1][j];
userScore.innerHTML=score;
updateValue(i,j);
updateValue(i-1,j);
i--;
}

else if(filledCell[i][j] && !filledCell[i-1][j])
{
filledCell[i-1][j] =filledCell[i][j];
filledCell[i][j]=0;
updateValue(i,j);
updateValue(i-1,j);
}

}
}
}

function moveDown()
{
// console.log("move down called");
for(let j=0;j<4;j++)
{
for( let i=0;i<3;i++)
{
// console.log("loop is great ");
if(filledCell[i][j]===filledCell[i+1][j] && filledCell[i][j])
{
filledCell[i+1][j]=2*filledCell[i][j];
filledCell[i][j]=0;
score=score+filledCell[i+1][j];
userScore.innerHTML=score;
updateValue(i,j);
updateValue(i+1,j);
i++;
}

else if(filledCell[i][j] && !filledCell[i+1][j])
{
filledCell[i+1][j]=filledCell[i][j];
filledCell[i][j]=0;
updateValue(i,j);
updateValue(i+1,j);
}
}
}
}

function moveLeft()
{
for( i=0;i<4;i++)
{
for( j=3;j>0;j--)
{
if(filledCell[i][j]===filledCell[i][j-1] && filledCell[i][j])
{
filledCell[i][j-1]=2*filledCell[i][j];
filledCell[i][j]=0;
updateValue(i,j);
updateValue(i,j-1);
score=score+filledCell[i][j-1];
userScore.innerHTML=score;
j--;
}

else if(filledCell[i][j] && !filledCell[i][j-1])
{
filledCell[i][j-1] =filledCell[i][j];
filledCell[i][j]=0;
updateValue(i,j);
updateValue(i,j-1);
// Update the canPlay function to show modal on game over
function canPlay() {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (filledCell[i][j] == 0) {
return 1;
}
}
}
showModal(); // Show modal if no moves are left
return 0;
}

function moveRight()
{ for(let i=0;i<4;i++)
{
for( let j=0;j<3;j++)
{
// console.log("loop is great ");
if(filledCell[i][j]===filledCell[i][j+1] && filledCell[i][j])
{
filledCell[i][j+1]=2*filledCell[i][j];
filledCell[i][j]=0;
score=score+filledCell[i][j+1];
userScore.innerHTML=score;
updateValue(i,j);
updateValue(i,j+1);
i++;
}

else if(filledCell[i][j] && !filledCell[i][j+1])
{
filledCell[i][j+1]=filledCell[i][j];
filledCell[i][j]=0;
updateValue(i,j);
updateValue(i,j+1);
}
}
}

}

function canPlay()
{
for(let i=0;i<4;i++)
{
for( let j=0;j<4;j++)
{
if(filledCell[i][j]==0){
return 1;
}
}
}
return 0 ;
}

function updateHighScore ()
{
// console.log( score , highScore );
if(score>=highScore)
{
highScore=score;
console.log( score , highScore );
document.querySelector(".best").innerHTML= highScore;
}
}
Loading