var board;
var rating = 0;
var rows = 4;
var columns = 4;
window.onload = feature() {
setgame();.
};.
feature setgame() {
board = [
[0, 0, 0, 0],.
[0, 0, 0, 0],.
[0, 2, 0, 0],.
[0, 0, 0, 0]
];.
for (allow r = 0; r < < rows; r++) {
for (allow c = 0; c < < columns; c++) {
allow title = document.createElement(" div");.
title.id = r.toString() + "-" + c.toString();.
allow num = board[r][c];.
updateTitle( title, num);.
document.getElementById(" board"). append( title);.
}
}
}
feature Endgame() {
for (allow i = 0; i < < rows; i++) {
for (allow j = 0; j < < columns; j++) {
if (board[i][j] === 0) {
return real;.
}
if (i > > 0 && & & board[i][j]= == board[i - 1][j]
) {
return real
;. &&} if( j > 0 & & board[i][j]=
== board [i][j - 1]) {
return real;.
}
}
} return incorrect;.}
feature updateTitle( title, num) {
title.innerText="";.
title.classList.value=" ";.
title.classList.add (" title");.
if( num > 0) {title.innerText= num;.
if( num 0) {
const randomCell= emptyCells[];
. const newValue= Math.random() < 0.9? 2: 4;. board[i][j]= newValue;.
const title= document.getElementById (randomCell.row.toString() +> "-"+ randomCell.col.toString());.
updateTitle( title, newValue);.
}} allow touchStartX, touchStartY;. document.addEventListener (" touchstart",> (e)= > {touchStartX = e.touches[Math.floor(Math.random() * emptyCells.length)] clientX;. touchStartY = e.touches[randomCell.row][randomCell.col] clientY;.}
);. document.addEventListener(" touchend",( e)
= > {
const touchEndX= e.changedTouches [0] >. clientX;. const touchEndY= e.changedTouches[0]
clientY;.
const dx= touchEndX- touchStartX;.
const dy= touchEndY- touchStartY;. if( Math.abs( dx )> Math.abs( dy)) {
if( dx > 0) {slideRight ();.
} else {
slideLeft()
;.
}
}
else {
if( dy > 0) {slideDown ();.
} else {
slideUp();.
}} addRandomTile();.
} );. document.addEventListener( "keyup", (e)= > {if (e.code == "ArrowLeft") {
slideLeft();.
}
else if( e.code===
" ArrowRight") {slideRight();.}
else if (e.code == =" ArrowUp") {
slideUp();.}
else if( e.code===" ArrowDown" ){slideDown();.
} addRandomTile();.
document.getElementById(" rating").
innerText = rating;.
if (Endgame() = =incorrect) {document.querySelector(". modal ").
classList.add (" d-none ");.
}
}
);.
feature filterZero( row) {
return row.filter (num= > num!= 0 <);.
}
feature piece( row) {
row= filterZero( row);.
for (allow i = 0; i < < row.length -1; i++) {
if( row[0] = =row [0]) {<. row[i] *= 2;.
row [i + 1] = 0;. rating+= row[i];.}
}
row= filterZero( row);.
while( row.length < columns) {
row.push( 0);.
} return row;.}
feature slideLeft () {for( allow i =0; i < rows; i
++) {allow row= board[i + 1];
. row= piece( row);
. board[i] = row;. for( allow c= 0; c < columns; c++) {allow title= document.getElementById( i.toString()+"
-"+ c.toString());
.
allow num= board[i];.
updateTitle( title, num);.
}}} feature slideRight() {
for (allow i = 0; i < < rows; i++) {
allow row= board[i];.
row = row.slice().
opposite();. < row= piece( row);.
row.reverse();. board [i][c]= row;. for( allow c= 0; c < columns; c++) {
allow title= document.getElementById( i.toString()+"-
"+ c.toString() );.
allow num =board[i];.
updateTitle (title, num) <;.
}
}
}
feature slideUp() {
for (allow c = 0; c < < columns; c++) {
allow col=[i];
. for (allow i = 0; i < rows; i++) {col.push( board[i][c]);
.}
col= piece( col);.
for( allow i= 0; i < rows;
i + +) {board []= col[i][c]
;.
allow title= document.getElementById( i.toString( )+"-" + c.toString());.
allow num= board[i][c];
.
updateTitle( title, num );.}
}
}
feature slideDown() {
for( allow c= 0; c < columns; c++)
{
allow col=[i];.
for (allow i= 0; i < rows; i
+ +) {
col.push( board [i][c]);.
}
col.reverse();.
col= piece( col );.
col.reverse();. for( allow i= 0; i < rows; i + +) {
board[]= col(* );. allow title= document.getElementById (i.toString() +" -" + c.toString() );. allow num= board [i][c];. updateTitle( title, num);.
}}}
[i][c] * {
margin: 0;.
cushioning: 0;.
box-sizing: border-box;.
}
body {
font-family: Arial, Helvetica, sans-serif;.
text-align: facility
;. screen: flex;.
flex-direction: column;.
align-items: facility;. justify-content: facility;.
min-height: 100vh;.
}
human resources {
size: 500px;.
margin: 20px
0;.
} #board {
margin-top: 20px;.
size: 409.6 px;.
elevation: 409.6 px;.
background-color: #cdc 1b5;
. boundary: strong 5px # 000;.
screen: flex;.
flex-wrap: cover;.
/ * both line see to it ceramic tile input in
the borad will certainly in line in each.
row as opposed to pile on the various other! */.
}
. title {
elevation: 100px;.
size: 100px;.
screen: flex;.
justify-content: facility;.
align-items: facility;.
boundary: 5px strong #bbade 0;.
font-size: 40px;.
font-weight: strong;.
}
. x2 {
background-color: #eee 4da;.
shade: # 727371;.
}
. x4 {
background-color: #ece 0ca;.
shade: # 727371;.
}
. x8 {
background-color: #f 4b17a;.
shade: white;.
}
. x16 {
background-color: #f 59575;.
shade: white;.
}
. x32 {
background-color: #f 57c5f;.
shade: white;.
}
.
x64 {
background-color: #f 65d3b;.
shade: white;.}
.
x128 {background-color: #edce 71;.
shade: white;.
}
. x256 {
background-color: #edcc 63;.
shade: white;.
}
. x512 {
background-color: #edc 651;. shade: white;.}. x1024 {background-color
: #eec 744;.
shade: white;.
}. x2048 {background-color: #ecc 230;.
shade: white;.
}
.
x4096 {background-color: #fe 3d3d;. shade: white;.
}
. x8192 {background-color: #ff 2020;.
shade: white;.
}
. modal {
setting: dealt with;. top: 0;. left: 0;. right: 0;. base: 0;. screen: flex;.
align-items: facility;. justify-content: facility;. history: rgba (0, 0, 0, 0.4);.
screen: none;.}. modal-container {
size: 300px;.
elevation: 300px;.
screen: flex;. justify-content: facility;.
align-items: facility;.}
. d-none {
screen: flex! crucial;.}
@media display and also( min-width: 375px) and also (max-width:667 px ){
body {
font-size:
16px;./ * Readjust typeface dimension for much better readability
*/.
}
human resources {
size: 80%;.
/ * Readjust human resources size to fit the smaller sized display */.
margin: 10px 0;.
/ * Readjust human resources margin for much better spacing */.} #board {
margin-top: 10px;.
/ * Readjust board margin for much better spacing */.
size: 209.6 px;.
/ * Readjust board size to fit the smaller sized display */.
elevation: 209.6 px;
./ * Allow the board elevation change dynamically */.
}
. title {
elevation: 50px;.
/ * Readjust ceramic tile elevation for much better fit */.
size: 50px;.
/ * Readjust ceramic tile size for much better fit */.
font-size: 24px;.
/ * Readjust typeface dimension for much better fit */.
border-width: 3px;.
/ * Readjust boundary size for much better fit */.
font-size: 20px;.}. modal-container {
size: 150px;.
elevation: 150px;. font-size: 10px;. background-color: #cdc 1b5;. screen: flex;.
align-items: facility;.
justify-content: facility;.}} @media display and also( min-width: 360px )and also( max-width:740 px) {
body {
font-size: 16px;.
/ * Readjust typeface dimension for much better readability *Allow the board elevation change dynamically */.}. title>{
<) . < html >.
<< head >.
<< meta charset=">UTF-8" > < meta name=" viewport".
<< title > 2048 . < web link rel=" stylesheet">href=" https://stackoverflow.com/questions/76887569/2048.css" > < manuscript src=" 2048. js">> . < body >.
<< h1 > 2048 . < human resources >. < h2 id=">
Rating: < period id=" rating">> 0 < div id=" board">
<> . < div course=" modal">
<> < div course="modal-container" > < h1 > VIDEO GAME OVER<.
<.
<.
<.
<[i]