This commit is contained in:
Jacob Signorovitch
2025-05-28 08:17:46 -04:00
parent 5b8ce38723
commit 473a3f7cb8

View File

@@ -11,7 +11,7 @@ import tester.Tester;
class Examples { class Examples {
void testRun(Tester t) { void testRun(Tester t) {
Game game = new Game(2); Game game = new Game();
game.run(); game.run();
} }
} }
@@ -47,7 +47,7 @@ class Game extends World {
int sz; // The size of the grid. int sz; // The size of the grid.
int width; // The width of the image in pixels. int width; // The width of the image in pixels.
Board board; // The game board. Board board; // The game board.
boolean gameOver; // Whether the game is over boolean gameOver;
Game(int score, int sz, int width, Board board) { Game(int score, int sz, int width, Board board) {
this.score = score; this.score = score;
@@ -85,18 +85,15 @@ class Game extends World {
this.width / 2, 20 this.width / 2, 20
); );
// Display game over message if the game is over // Display loss message.
if (this.gameOver) { if (this.gameOver)
WorldImage gameOverText =
new TextImage("Game Over!", 40, Color.RED);
WorldImage background = new RectangleImage(
this.width / 2, 50, OutlineMode.SOLID, new Color(0, 0, 0, 150)
);
scene.placeImageXY( scene.placeImageXY(
new OverlayImage(gameOverText, background), this.width / 2, new TextImage(
this.width / 2 "Game over. Score: " + this.score + ". [R] to restart.", 20,
Color.RED
),
this.width / 2, this.width / 2
); );
}
return scene; return scene;
} }
@@ -104,9 +101,9 @@ class Game extends World {
public void onKeyEvent(String key) { public void onKeyEvent(String key) {
System.out.println("got key" + key); System.out.println("got key" + key);
// If game is over, ignore movement keys // If game over, ignore movement keys.
if (this.gameOver) { if (this.gameOver) {
// Optional: restart game with 'r' key // Restart game with 'r'.
if (key.equals("r")) { if (key.equals("r")) {
this.score = 0; this.score = 0;
this.board = new Board(this.sz); this.board = new Board(this.sz);
@@ -131,7 +128,7 @@ class Game extends World {
this.score += score; this.score += score;
// Check if game is over after the move // Check if game over after move.
this.gameOver = this.board.isGameOver(); this.gameOver = this.board.isGameOver();
} }
@@ -200,7 +197,7 @@ class Board {
} }
} }
// Try to combine two tiles if they have the same value // Try to combine two tiles.
int tryCombine(Coord source, Coord target) { int tryCombine(Coord source, Coord target) {
Cell sourceCell = this.get(source); Cell sourceCell = this.get(source);
Cell targetCell = this.get(target); Cell targetCell = this.get(target);
@@ -247,21 +244,19 @@ class Board {
if (next.x >= 0 && next.x < this.sz && next.y >= 0 && next.y < this.sz) if (next.x >= 0 && next.x < this.sz && next.y >= 0 && next.y < this.sz)
return this.tryCombine(current, next); return this.tryCombine(current, next);
// Only generate new tile if changed.
if (boardChanged) this.randGen();
return 0; return 0;
} }
// Move in the given direction. Returns the generated score. // Move in the given direction. Returns the generated score.
int move(Move move) { int move(Move move) {
System.out.println("moving");
// Reset flags for all tiles and change tracker. // Reset flags for all tiles and change tracker.
resetFlags(); resetFlags();
this.boardChanged = false;
int score = 0; int score = 0;
for (int i = 0; i < this.sz; i++) { score += this.moveLine(i, move); } for (int i = 0; i < this.sz; i++) score += this.moveLine(i, move);
if (this.boardChanged) this.randGen();
return score; return score;
} }
@@ -316,49 +311,42 @@ class Board {
} }
} }
// Checks if the game is over (no valid moves left) // Check if game over.
boolean isGameOver() { boolean isGameOver() {
// Check if there are any empty spaces for (int x = 0; x < this.sz; x++)
for (int x = 0; x < this.sz; x++) { for (int y = 0; y < this.sz; y++)
for (int y = 0; y < this.sz; y++) { if (this.get(new Coord(x, y)).isReplaceable())
if (this.get(new Coord(x, y)).isReplaceable()) { return false; // There's an empty space, game not over.
return false; // There's an empty space, game not over
}
}
}
// Check if any adjacent tiles can be combined // Check if any adjacent tiles can be combined.
for (int x = 0; x < this.sz; x++) { for (int x = 0; x < this.sz; x++) {
for (int y = 0; y < this.sz; y++) { for (int y = 0; y < this.sz; y++) {
Cell current = this.get(new Coord(x, y)); Cell current = this.get(new Coord(x, y));
// Skip if not a tile // Skip if not tile.
if (!(current instanceof Tile)) { continue; } if (!(current instanceof Tile)) continue;
Tile currentTile = (Tile)current; Tile currentTile = (Tile)current;
// Check right neighbor
if (x < this.sz - 1) { if (x < this.sz - 1) {
Cell right = this.get(new Coord(x + 1, y)); Cell right = this.get(new Coord(x + 1, y));
if (right instanceof Tile && if (right instanceof Tile &&
((Tile)right).n == currentTile.n) { ((Tile)right).n == currentTile.n) {
return false; // Can combine horizontally return false; // Can combine horizontally.
} }
} }
// Check bottom neighbor
if (y < this.sz - 1) { if (y < this.sz - 1) {
Cell bottom = this.get(new Coord(x, y + 1)); Cell bottom = this.get(new Coord(x, y + 1));
if (bottom instanceof Tile && if (bottom instanceof Tile &&
((Tile)bottom).n == currentTile.n) { ((Tile)bottom).n == currentTile.n) {
return false; // Can combine vertically return false; // Can combine vertically.
} }
} }
} }
} }
// No empty spaces and no possible combinations return true; // Game over.
return true;
} }
} }