User:Nullable22/common.js

class Game2048 { constructor(size, container) { this.container = document.getElementById(container); this.size = size; this.grid = Array.from({ length: size }, => Array(size).fill(0)); this.score = 0; this.init; }

init { this.addRandomCell; this.addRandomCell;

document.addEventListener("keydown", (e) => this.onKeyPress(e)); this.draw; }

getRandom2 { return Math.random < 0.9 ? 2 : 4; }

getRandom4 { return Math.floor(Math.random * 4); }

addRandomCell { var line = this.getRandom4; var column = this.getRandom4;

if (this.grid[line][column] === 0) { this.grid[line][column] = this.getRandom2; } else { this.addRandomCell; } }

moveLeft { var i,     j,      k,      canMove = false;

i = 0; while (i < this.grid.length) { if (this.grid[i] != [0, 0, 0, 0]) { k = 0; while (k < this.grid[i].length) { j = 0; while (j < this.grid[i].length - 1) { if (this.grid[i][j] === 0) { this.grid[i][j] = this.grid[i][j + 1]; this.grid[i][j + 1] = 0; if (this.grid[i][j] !== 0) { canMove = true; }           }            j += 1; }         k += 1; }     }      i += 1; }

return canMove; }

moveRight { var i,     j,      k,      canMove = false;

i = 0; while (i < this.grid.length) { if (this.grid[i] != [0, 0, 0, 0]) { k = 0; while (k < this.grid[i].length) { j = this.grid.length - 1; while (j > 0) { if (this.grid[i][j] === 0) { this.grid[i][j] = this.grid[i][j - 1]; this.grid[i][j - 1] = 0; if (this.grid[i][j] !== 0) { canMove = true; }           }            j -= 1; }         k += 1; }     }      i += 1; }

return canMove; }

moveUp { var i,     j,      k,      canMove = false;

k = 0; while (k < this.grid.length - 1) { i = 0; while (i < this.grid.length - 1) { j = 0; while (j < this.grid.length) { if (this.grid[i][j] === 0) { this.grid[i][j] = this.grid[i + 1][j]; this.grid[i + 1][j] = 0; if (this.grid[i][j] !== 0) { canMove = true; }         }          j += 1; }       i += 1; }     k += 1; }

return canMove; }

moveDown { var i,     j,      k,      canMove = false;

k = 0; while (k < this.grid.length - 1) { i = this.grid.length - 1; while (i > 0) { j = 0; while (j < this.grid.length) { if (this.grid[i][j] === 0) { this.grid[i][j] = this.grid[i - 1][j]; this.grid[i - 1][j] = 0; if (this.grid[i][j] !== 0) { canMove = true; }         }          j += 1; }       i -= 1; }     k += 1; }

return canMove; }

checkMatchesLeft { var i,     j,      canMerge = false;

i = 0; while (i < this.grid.length) { j = 0; while (j < this.grid[i].length - 1) { if (this.grid[i][j] !== 0 && this.grid[i][j] === this.grid[i][j + 1]) { this.grid[i][j] = this.grid[i][j] * 2; this.score += this.grid[i][j]; this.grid[i][j + 1] = 0; canMerge = true; }       j += 1; }     i += 1; }

this.moveLeft; return canMerge; }

checkMatchesRight { var i,     j,      canMerge = false;

i = 0; while (i < this.grid.length) { j = this.grid.length - 1; while (j > 0) { if (this.grid[i][j] !== 0 && this.grid[i][j] === this.grid[i][j - 1]) { this.grid[i][j] = this.grid[i][j] * 2; this.score += this.grid[i][j]; this.grid[i][j - 1] = 0; canMerge = true; }       j -= 1; }     i += 1; }

this.moveRight; return canMerge; }

checkMatchesUp { var i,     j,      canMerge = false;

i = 0; while (i < this.grid.length - 1) { j = 0; while (j < this.grid.length) { if (this.grid[i][j] !== 0 && this.grid[i][j] === this.grid[i + 1][j]) { this.grid[i][j] = this.grid[i][j] * 2; this.score += this.grid[i][j]; this.grid[i + 1][j] = 0; canMerge = true; }       j += 1; }     i += 1; }

this.moveUp; return canMerge; }

checkMatchesDown { var i,     j,      canMerge = false;

i = this.grid.length - 1; while (i > 0) { j = 0; while (j < this.grid.length) { if (this.grid[i][j] !== 0 && this.grid[i][j] === this.grid[i - 1][j]) { this.grid[i][j] = this.grid[i][j] * 2; this.score += this.grid[i][j]; this.grid[i - 1][j] = 0; canMerge = true; }       j += 1; }     i -= 1; }

this.moveDown; return canMerge; }

draw { const gridHtml = this.grid .map(       (row) =>          ` ${row            .map( (cell) => `${cell || ""} ` )           .join("")} `      ) .join(""); this.container.innerHTML = gridHtml; }

onKeyPress(e) { var move, check; switch (e.key) { case "ArrowUp": move = this.moveUp; check = this.checkMatchesUp; if (move || check) { this.addRandomCell; this.draw; }       break; case "ArrowDown": move = this.moveDown; check = this.checkMatchesDown; if (move || check) { this.addRandomCell; this.draw; }       break; case "ArrowLeft": move = this.moveLeft; check = this.checkMatchesLeft; if (move || check) { this.addRandomCell; this.draw; }       break; case "ArrowRight": move = this.moveRight; check = this.checkMatchesRight; if (move || check) { this.addRandomCell; this.draw; }       break; } } }

function initGame { var gameContainer = $(' '); $('#contentSub').prepend(gameContainer); const game = new Game2048(4, "game2048-container"); }

$(document).ready(function {	var button = $('Play 2048 (V 0.1) ');    $('#contentSub').prepend(button); });