diff --git a/twentyfortyeight/src/twentyfortyeight/Main.java b/twentyfortyeight/src/twentyfortyeight/Main.java index 49bdbfc..bdc65fa 100644 --- a/twentyfortyeight/src/twentyfortyeight/Main.java +++ b/twentyfortyeight/src/twentyfortyeight/Main.java @@ -11,7 +11,7 @@ import tester.Tester; class Examples { void testRun(Tester t) { - Game game = new Game(2); + Game game = new Game(); game.run(); } } @@ -43,11 +43,11 @@ class Util { // A game of 2048. class Game extends World { - int score; // The current score. - int sz; // The size of the grid. - int width; // The width of the image in pixels. - Board board; // The game board. - boolean gameOver; // Whether the game is over + int score; // The current score. + int sz; // The size of the grid. + int width; // The width of the image in pixels. + Board board; // The game board. + boolean gameOver; Game(int score, int sz, int width, Board board) { this.score = score; @@ -85,18 +85,15 @@ class Game extends World { this.width / 2, 20 ); - // Display game over message if the game is over - 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) - ); + // Display loss message. + if (this.gameOver) scene.placeImageXY( - new OverlayImage(gameOverText, background), this.width / 2, - this.width / 2 + new TextImage( + "Game over. Score: " + this.score + ". [R] to restart.", 20, + Color.RED + ), + this.width / 2, this.width / 2 ); - } return scene; } @@ -104,9 +101,9 @@ class Game extends World { public void onKeyEvent(String key) { System.out.println("got key" + key); - // If game is over, ignore movement keys + // If game over, ignore movement keys. if (this.gameOver) { - // Optional: restart game with 'r' key + // Restart game with 'r'. if (key.equals("r")) { this.score = 0; this.board = new Board(this.sz); @@ -131,7 +128,7 @@ class Game extends World { this.score += score; - // Check if game is over after the move + // Check if game over after move. 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) { Cell sourceCell = this.get(source); 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) return this.tryCombine(current, next); - // Only generate new tile if changed. - if (boardChanged) this.randGen(); - return 0; } // Move in the given direction. Returns the generated score. int move(Move move) { - System.out.println("moving"); - // Reset flags for all tiles and change tracker. resetFlags(); + this.boardChanged = false; 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; } @@ -316,49 +311,42 @@ class Board { } } - // Checks if the game is over (no valid moves left) + // Check if game over. boolean isGameOver() { - // Check if there are any empty spaces - for (int x = 0; x < this.sz; x++) { - for (int y = 0; y < this.sz; y++) { - if (this.get(new Coord(x, y)).isReplaceable()) { - return false; // There's an empty space, game not over - } - } - } + for (int x = 0; x < this.sz; x++) + for (int y = 0; y < this.sz; y++) + if (this.get(new Coord(x, y)).isReplaceable()) + 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 y = 0; y < this.sz; y++) { Cell current = this.get(new Coord(x, y)); - // Skip if not a tile - if (!(current instanceof Tile)) { continue; } + // Skip if not tile. + if (!(current instanceof Tile)) continue; Tile currentTile = (Tile)current; - // Check right neighbor if (x < this.sz - 1) { Cell right = this.get(new Coord(x + 1, y)); if (right instanceof Tile && ((Tile)right).n == currentTile.n) { - return false; // Can combine horizontally + return false; // Can combine horizontally. } } - // Check bottom neighbor if (y < this.sz - 1) { Cell bottom = this.get(new Coord(x, y + 1)); if (bottom instanceof Tile && ((Tile)bottom).n == currentTile.n) { - return false; // Can combine vertically + return false; // Can combine vertically. } } } } - // No empty spaces and no possible combinations - return true; + return true; // Game over. } }