Finished twentyfortyeight.

This commit is contained in:
Jacob Signorovitch
2025-05-08 12:38:54 -04:00
parent fd75827c7a
commit 0573ca312a

View File

@@ -1,6 +1,7 @@
package twentyfortyeight; package twentyfortyeight;
import java.awt.Color; import java.awt.Color;
import java.time.format.TextStyle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@@ -77,12 +78,15 @@ class Game extends World {
} }
public void onKeyEvent(String key) { public void onKeyEvent(String key) {
if (key.equals("left") || key.equals("h")) this.grid.mv(0); int mv;
else if (key.equals("down") || key.equals("j")) this.grid.mv(1); if (key.equals("left") || key.equals("h")) mv = 0;
else if (key.equals("up") || key.equals("k")) this.grid.mv(2); else if (key.equals("down") || key.equals("j")) mv = 1;
else if (key.equals("right") || key.equals("l")) this.grid.mv(3); else if (key.equals("up") || key.equals("k")) mv = 2;
else if (key.equals("right") || key.equals("l")) mv = 3;
else return; // Don't draw or add tiles if bad key pressed. else return; // Don't draw or add tiles if bad key pressed.
this.score += this.grid.mv(mv);
this.draw(); this.draw();
List<Integer> frees = this.grid.freeCellIdxs(); List<Integer> frees = this.grid.freeCellIdxs();
@@ -93,7 +97,16 @@ class Game extends World {
} }
// Draw current game state. // Draw current game state.
void draw() { this.scene = this.grid.render(); } void draw() {
this.scene = this.grid.render();
this.scene.placeImageXY(
new TextImage(
"Score: " + String.valueOf(this.score), 25, Color.red
),
100, 20
);
}
} }
// The grid in which the game is played. // The grid in which the game is played.
@@ -126,18 +139,6 @@ class Grid {
return free; return free;
} }
// Get the indexes of all "unfree" cells -- those whose value in buf is not
// 0, and represent a used tile.
/*
List<Integer> unfreeCellIdxs() {
List<Integer> unfree = new ArrayList<>();
for (int i = 0; i < this.sz; i++)
if (this.buf.get(i) != 0 && unfree.add(i)) continue;
return unfree;
}
*/
// Set the tile at the coords. // Set the tile at the coords.
Grid set(int x, int y, int v) { Grid set(int x, int y, int v) {
this.buf.set(this.where(x, y), v); this.buf.set(this.where(x, y), v);
@@ -156,13 +157,16 @@ class Grid {
// Get the y coord for an index. // Get the y coord for an index.
int iy(int i) { return i % this.w; } int iy(int i) { return i % this.w; }
void mv(int d) { int mv(int d) {
int add = 0;
// For each row / col. // For each row / col.
for (int i = 0; i < (d % 3 == 0 ? this.h : this.w); i++) { for (int i = 0; i < (d % 3 == 0 ? this.h : this.w); i++) {
while (this.lnMv(i, d)); while (this.lnMv(i, d));
this.squish(i, d); add += this.squish(i, d);
while (this.lnMv(i, d)); while (this.lnMv(i, d));
} }
return add;
} }
// Get the ith row. // Get the ith row.
@@ -222,14 +226,18 @@ class Grid {
} }
// Squish like tiles together. // Squish like tiles together.
void squish(int i, int d) { int squish(int i, int d) {
int add = 0;
List<Integer> ln = getLn(i, d); List<Integer> ln = getLn(i, d);
for (int j = 0; j < ln.size() - 1; j++) { for (int j = 0; j < ln.size() - 1; j++) {
if (ln.get(j) != 0 && ln.get(j) == ln.get(j + 1)) { if (ln.get(j) != 0 && ln.get(j) == ln.get(j + 1)) {
ln.set(j, 2 * ln.get(j)); ln.set(j, 2 * ln.get(j));
ln.set(j + 1, 0); ln.set(j + 1, 0);
add += ln.get(j);
} }
} }
return add;
} }
// Render the grid. // Render the grid.
@@ -297,7 +305,7 @@ class Examples {
} }
void testGame(Tester t) { void testGame(Tester t) {
game = new Game(2, 2); game = new Game(4, 4);
game.launchGame(); game.launchGame();
} }
} }