From e453f377e0b2204660a449a9df99d59665815915 Mon Sep 17 00:00:00 2001 From: Mario Benov Date: Fri, 13 Oct 2023 15:10:09 +0300 Subject: [PATCH] 10a, 10b - candy crush example solution --- .../11b/2023-10-13-candy-crush/BombTile.java | 24 +++++ .../11b/2023-10-13-candy-crush/CandyGame.java | 91 +++++++++++++++++++ .../11b/2023-10-13-candy-crush/ShapeTile.java | 38 ++++++++ .../11b/2023-10-13-candy-crush/Tile.java | 27 ++++++ 4 files changed, 180 insertions(+) create mode 100644 materials/2023-2024/11b/2023-10-13-candy-crush/BombTile.java create mode 100644 materials/2023-2024/11b/2023-10-13-candy-crush/CandyGame.java create mode 100644 materials/2023-2024/11b/2023-10-13-candy-crush/ShapeTile.java create mode 100644 materials/2023-2024/11b/2023-10-13-candy-crush/Tile.java diff --git a/materials/2023-2024/11b/2023-10-13-candy-crush/BombTile.java b/materials/2023-2024/11b/2023-10-13-candy-crush/BombTile.java new file mode 100644 index 0000000..9952956 --- /dev/null +++ b/materials/2023-2024/11b/2023-10-13-candy-crush/BombTile.java @@ -0,0 +1,24 @@ +import java.util.Random; + +public class BombTile extends Tile { + public BombTile(Tile[][] field) { + super(field); + variant = 5; + } + + @Override + public void print() { + System.out.print("B"); + } + + // Clear all tiles in a 3x3 area + @Override + public void processSwap() { + System.out.println("BOOM"); + for(int x = Math.max(0, this.x - 1); x <= Math.min(6, this.x + 1); x++) { + for(int y = Math.max(0, this.y - 1); y <= Math.min(6, this.y + 1); y++) { + field[x][y] = null; + } + } + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2023-10-13-candy-crush/CandyGame.java b/materials/2023-2024/11b/2023-10-13-candy-crush/CandyGame.java new file mode 100644 index 0000000..1d03aa5 --- /dev/null +++ b/materials/2023-2024/11b/2023-10-13-candy-crush/CandyGame.java @@ -0,0 +1,91 @@ +public class CandyGame { + public static void main(String[] args) { + CandyGame game = new CandyGame(); + + game.print(); + + // Test swapping and removing tiles + // game.swapTiles(0, 0, 1, 0); + // game.print(); + + // Test the bomb + game.swapTiles(2, 2, 1, 2); + game.print(); + } + + private int fieldSize = 6; + private Tile[][] field; + + CandyGame() { + field = new Tile[fieldSize][fieldSize]; + for(int x = 0; x < fieldSize; x++) { + for(int y = 0; y < fieldSize; y++) { + setTile(x, y, new ShapeTile(field)); + } + } + + // Create test tiles + field[0][0].variant = 1; + field[1][0].variant = 0; + field[2][0].variant = 1; + field[3][0].variant = 1; + + field[0][1].variant = 0; + field[0][2].variant = 0; + + setTile(1, 2, new BombTile(field)); + } + + public void print() { + for(int y = 0; y < fieldSize; y++) { + for(int x = 0; x < fieldSize; x++) { + if(field[x][y] != null) + field[x][y].print(); + else + System.out.print("E"); + + System.out.print(" "); + } + System.out.println(""); + } + } + + private boolean isInBounds(int x, int y) { + return x >= 0 && y >= 0 && x < fieldSize && y < fieldSize; + } + + private void setTile(int x, int y, Tile tile) { + field[x][y] = tile; + + tile.x = x; + tile.y = y; + } + + public void swapTiles(int x1, int y1, int x2, int y2) { + // must be inside the field + if(!isInBounds(x1, y1) || !isInBounds(x2, y2)) + return; // throw new Exception + + // must be adjacent + if(Math.abs(x1 - x2) > 1 || Math.abs(y1 - y2) > 1) + return; + + // not diagonal + if(Math.abs(x1 - x2) + Math.abs(y1 - y2) != 1) + return; + + // must result in tiles being removed + // TODO: Define checks in the tiles that swapping them will remove at least 1 tile + + System.out.println("Swap tiles (" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ")"); + + Tile t1 = field[x1][y1]; + Tile t2 = field[x2][y2]; + + setTile(x1, y1, t2); + setTile(x2, y2, t1); + + t1.processSwap(); + t2.processSwap(); + } +} diff --git a/materials/2023-2024/11b/2023-10-13-candy-crush/ShapeTile.java b/materials/2023-2024/11b/2023-10-13-candy-crush/ShapeTile.java new file mode 100644 index 0000000..a7fd2cf --- /dev/null +++ b/materials/2023-2024/11b/2023-10-13-candy-crush/ShapeTile.java @@ -0,0 +1,38 @@ +import java.util.Random; + +public class ShapeTile extends Tile { + public ShapeTile(Tile[][] field) { + super(field); + variant = new Random().nextInt(4); + } + + @Override + public void print() { + System.out.print(variant); + } + + // Search in all directions for a straight line of identical tiles that's 3 or more long + @Override + public void processSwap() { + int startX = x; + int endX = x; + + // Search left and right for same tiles + while(startX - 1 >= 0 && field[startX - 1][y] != null && field[startX - 1][y].variant == variant) startX--; + while(endX < 5 && field[endX][y] != null && field[endX][y].variant == variant) endX++; + + // If the the line is 3 or more clear it + if(endX - startX >= 3) + for(int i = startX; i < endX; i++) field[i][y] = null; + + int startY = y; + int endY = y; + + // Same for up and down + while(startY - 1 >= 0 && field[x][startY - 1] != null && field[x][startY - 1].variant == variant) startY--; + while(endY < 5 && field[x][endY] != null && field[x][endY].variant == variant) endY++; + + if(endY - startY >= 3) + for(int i = startY; i < endY; i++) field[x][i] = null; + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2023-10-13-candy-crush/Tile.java b/materials/2023-2024/11b/2023-10-13-candy-crush/Tile.java new file mode 100644 index 0000000..efc2bd5 --- /dev/null +++ b/materials/2023-2024/11b/2023-10-13-candy-crush/Tile.java @@ -0,0 +1,27 @@ + +public class Tile { + public int x = -1; + public int y = -1; + public int variant = -1; + + protected Tile[][] field; + + // Minimal dependency on the game and field to let us remove tiles + public Tile(Tile[][] field) { + this.field = field; + } + + // TODO: Make the class abstract: + // public abstract void print(); + // public abstract void processSwap(); + + // Let each tile define how it's printed in the console + public void print() { + System.out.print("Tile"); + }; + + // Called after swapping to handle the result of moving the tile + public void processSwap() { + System.out.println("Not implemented"); + } +} \ No newline at end of file