diff --git a/Web development/2048 Game/app.js b/Web development/2048 Game/app.js index c04b7ae..5f3ae0c 100644 --- a/Web development/2048 Game/app.js +++ b/Web development/2048 Game/app.js @@ -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 = ` +
+`; +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; - } -} \ No newline at end of file diff --git a/Web development/2048 Game/style.css b/Web development/2048 Game/style.css index 1b54b54..3204fa4 100644 --- a/Web development/2048 Game/style.css +++ b/Web development/2048 Game/style.css @@ -1,147 +1,47 @@ - -*{ - margin:0; - padding:0; -} - -body { - background-color: rgb(228, 225, 218); - font-family: clear sans,helvetica neue,Arial,sans-serif; - -} - -.gameBoard{ - - /* position: absolute; */ - align-items: center; - display: flex; - flex-wrap: wrap; - justify-content: space-around !important; - background: #bbada0; - border-radius: 6px; - width: 70vmin; - height: 70vmin; - margin: 5vmin auto ; - padding: 4px; - -} - -.gameBoard div{ - background: rgba(238,228,218,.35); - height: 16vmin; - width: 16vmin; - border-radius: 0.5vmin; - text-align: center; - font-weight: bolder; - font-size: 9vmin; - line-height: 17vmin; - -} - -.topbar -{ - display: flex; - justify-content: space-around; - margin:2vmin; - color:white; - font-size: medium ; -} - -.heading -{ - margin: 1.5vmin 8vmin; - color: #776e65; - font-size: 7vmin; -} - -.newPlay -{ - background: #8f7a66; - border-radius: 1vmin; +/* Existing styles remain unchanged */ + +/* Add animations for the game board cells */ +.gameBoard div { + transition: transform 0.2s ease, background-color 0.2s ease; +} + +/* New styles for the game over modal */ +.modal { + display: none; /* Hidden by default */ + position: fixed; /* Stay in place */ + z-index: 1; /* Sit on top */ + left: 0; + top: 0; + width: 100%; /* Full width */ + height: 100%; /* Full height */ + background-color: rgba(0,0,0,0.7); /* Black with transparency */ + color: white; /* White text */ + text-align: center; /* Centered text */ + padding-top: 20%; +} + +.modal-content { + background-color: #444; /* Darker background for the modal */ + padding: 20px; + border-radius: 10px; + display: inline-block; /* Inline-block to fit content */ +} + +/* Style the close button for the modal */ +.close-button { + background-color: #f44336; /* Red */ border: none; - color:white; - font-family: clear sans,helvetica neue,Arial,sans-serif; - font-weight: bolder; - font-size: 2.5vmin; - padding: 0 3vmin; - line-height: 1vmin; - height: 8vmin; -} - -.scoreBar -{ - background-color:#cfb69f; - font-weight: bold; - color: #ebe4d8; - padding: 0 3vmin; - line-height: 8vmin; - border-radius: 1vmin; - height: 8vmin; - margin-bottom: 2vmin; -} - -.best ,.points -{ - font-size: 4vmin; - font-weight: lighter; - color:white; -} - -/* //coloring the cell */ - -.value2 { - background: #eee4da !important; - color: #727371; -} - -.value4 { - background-color: #ede0c8 !important; - color: #727371; -} - -.value8 { - color: #f9f6f2; - background: #f2b179 !important; -} - -.value16{ - color: #f9f6f2; - background: #f59563 !important; -} - -.value32{ - color: #f9f6f2; - background: #f67c5f !important; -} - -.value64{ - color: #f9f6f2; - background: #f65e3b !important; -} - -.value128{ - color: #f9f6f2; - background: #edcf72 !important; -} - -.value256{ - color: #f9f6f2; - background: #edcc61 !important; -} - -.value512{ - - color:#f9f6f2; - background:#edc850 !important; - -} - -.value1024{ color: white; - background:#edc53f !important; + padding: 10px 15px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + margin-top: 10px; + border-radius: 5px; + cursor: pointer; } -.value2048{ -color:#f9f6f2; -background-color:#edc22e !important; -} \ No newline at end of file +.close-button:hover { + background-color: #d32f2f; /* Darker red */ +}