Spaces:
Running
Running
File size: 6,278 Bytes
1e917c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
class Game {
constructor() {
this.grid = Array(4).fill().map(() => Array(4).fill(0));
this.score = 0;
this.init();
}
init() {
this.addNewTile();
this.addNewTile();
this.updateDisplay();
}
addNewTile() {
const emptyCells = [];
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (this.grid[i][j] === 0) {
emptyCells.push({x: i, y: j});
}
}
}
if (emptyCells.length > 0) {
const randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)];
this.grid[randomCell.x][randomCell.y] = Math.random() < 0.9 ? 2 : 4;
}
}
updateDisplay() {
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
const cell = document.getElementById(`cell-${i}-${j}`);
const value = this.grid[i][j];
cell.className = 'grid-cell' + (value ? ` tile tile-${value}` : '');
cell.textContent = value || '';
}
}
document.getElementById('score').textContent = this.score;
}
move(direction) {
let moved = false;
const oldGrid = JSON.stringify(this.grid);
switch(direction) {
case 'left':
moved = this.moveLeft();
break;
case 'right':
moved = this.moveRight();
break;
case 'up':
moved = this.moveUp();
break;
case 'down':
moved = this.moveDown();
break;
}
if (moved) {
this.addNewTile();
this.updateDisplay();
if (this.isGameOver()) {
alert('Game Over!');
}
}
}
moveLeft() {
let moved = false;
for (let i = 0; i < 4; i++) {
let row = this.grid[i].filter(cell => cell !== 0);
for (let j = 0; j < row.length - 1; j++) {
if (row[j] === row[j + 1]) {
row[j] *= 2;
this.score += row[j];
row.splice(j + 1, 1);
moved = true;
}
}
while (row.length < 4) {
row.push(0);
}
if (row.join(',') !== this.grid[i].join(',')) {
moved = true;
}
this.grid[i] = row;
}
return moved;
}
moveRight() {
let moved = false;
for (let i = 0; i < 4; i++) {
let row = this.grid[i].filter(cell => cell !== 0);
for (let j = row.length - 1; j > 0; j--) {
if (row[j] === row[j - 1]) {
row[j] *= 2;
this.score += row[j];
row.splice(j - 1, 1);
row.unshift(0);
moved = true;
}
}
while (row.length < 4) {
row.unshift(0);
}
if (row.join(',') !== this.grid[i].join(',')) {
moved = true;
}
this.grid[i] = row;
}
return moved;
}
moveUp() {
let moved = false;
for (let j = 0; j < 4; j++) {
let column = [];
for (let i = 0; i < 4; i++) {
column.push(this.grid[i][j]);
}
column = column.filter(cell => cell !== 0);
for (let i = 0; i < column.length - 1; i++) {
if (column[i] === column[i + 1]) {
column[i] *= 2;
this.score += column[i];
column.splice(i + 1, 1);
moved = true;
}
}
while (column.length < 4) {
column.push(0);
}
for (let i = 0; i < 4; i++) {
if (this.grid[i][j] !== column[i]) {
moved = true;
}
this.grid[i][j] = column[i];
}
}
return moved;
}
moveDown() {
let moved = false;
for (let j = 0; j < 4; j++) {
let column = [];
for (let i = 0; i < 4; i++) {
column.push(this.grid[i][j]);
}
column = column.filter(cell => cell !== 0);
for (let i = column.length - 1; i > 0; i--) {
if (column[i] === column[i - 1]) {
column[i] *= 2;
this.score += column[i];
column.splice(i - 1, 1);
column.unshift(0);
moved = true;
}
}
while (column.length < 4) {
column.unshift(0);
}
for (let i = 0; i < 4; i++) {
if (this.grid[i][j] !== column[i]) {
moved = true;
}
this.grid[i][j] = column[i];
}
}
return moved;
}
isGameOver() {
// Check for empty cells
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (this.grid[i][j] === 0) {
return false;
}
}
}
// Check for possible merges
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
if (j < 3 && this.grid[i][j] === this.grid[i][j + 1]) return false;
if (i < 3 && this.grid[i][j] === this.grid[i + 1][j]) return false;
}
}
return true;
}
}
// Initialize game
let game = new Game();
// Event listeners
document.addEventListener('keydown', (event) => {
switch(event.key) {
case 'ArrowLeft':
game.move('left');
break;
case 'ArrowRight':
game.move('right');
break;
case 'ArrowUp':
game.move('up');
break;
case 'ArrowDown':
game.move('down');
break;
}
});
document.getElementById('new-game').addEventListener('click', () => {
game = new Game();
}); |