This commit is contained in:
Jacob Signorovitch
2025-04-10 11:43:35 -04:00
parent c367b201d4
commit d6cd6812c4

View File

@@ -71,7 +71,10 @@ class Game extends World {
this.scene = new WorldScene(this.grid.w, this.grid.h);
}
void launchGame() { this.bigBang(this.w, this.h, 0.01); }
void launchGame() {
this.bigBang(this.w, this.h, 0.01);
this.grid.set(0, 0, 2).set(0, 2, 2);
}
public WorldScene makeScene() {
this.scene = this.getEmptyScene();
@@ -84,7 +87,16 @@ class Game extends World {
else if (key.equals("down") || key.equals("j")) this.grid.mv(1);
else if (key.equals("up") || key.equals("k")) this.grid.mv(2);
else if (key.equals("right") || key.equals("l")) this.grid.mv(3);
else return; // Don't draw if bad key pressed.
else return; // Don't draw or add tiles if bad key pressed.
this.draw();
List<Integer> frees = this.grid.freeCellIdxs();
// Add tiles
this.grid.buf.set(
frees.get(0), 2 // this.rand.nextInt() % (frees.size() - 1)), 2
);
this.draw();
}
@@ -173,13 +185,46 @@ class Grid {
void combine(int d) {
// Assume left.
if (d == 0) {
for (int i = 0; i < this.h; i++) {
int prv = this.get(0, i);
for (int j = 1; j < this.w; j++) {
int cur = this.get(j, i);
for (int y = 0; y < this.h; y++) {
int prv = this.get(0, y);
for (int x = 1; x < this.w; x++) {
int cur = this.get(x, y);
if (cur == prv) {
this.set(j - 1, i, cur * 2);
this.set(j, i, 0);
this.set(x - 1, y, cur * 2);
this.set(x, y, 0);
}
}
}
} else if (d == 1) {
for (int x = 0; x < this.w; x++) {
int prv = this.get(x, this.h - 1);
for (int y = this.h - 2; y > 0; y--) {
int cur = this.get(x, y);
if (cur == prv) {
this.set(x, y + 1, cur * 2);
this.set(x, y, 0);
}
}
}
} else if (d == 2) {
for (int x = 0; x < this.w; x++) {
int prv = this.get(x, 0);
for (int y = 1; y < this.h; y++) {
int cur = this.get(x, y);
if (cur == prv) {
this.set(x, y - 1, cur * 2);
this.set(x, y, 0);
}
}
}
} else if (d == 3) {
for (int y = 0; y < this.h; y++) {
int prv = this.get(this.w - 1, y);
for (int x = this.w - 2; x > 0; x--) {
int cur = this.get(x, y);
if (cur == prv) {
this.set(x + 1, y, cur * 2);
this.set(x, y, 0);
}
}
}
@@ -226,6 +271,7 @@ class Grid {
void mv(int d) {
while (this.partMv(d)); // I love side effects :).
this.combine(d);
// while (this.partMv(d)); // Complete move.
}
// Render the grid.
@@ -294,7 +340,6 @@ class Examples {
void testGame(Tester t) {
game = new Game();
game.grid.set(0, 0, 2).set(2, 0, 2);
game.launchGame();
}