Skip to content

Commit

Permalink
10a, 10b - candy crush example solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Benov committed Oct 13, 2023
1 parent b26f91c commit e453f37
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
24 changes: 24 additions & 0 deletions materials/2023-2024/11b/2023-10-13-candy-crush/BombTile.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
91 changes: 91 additions & 0 deletions materials/2023-2024/11b/2023-10-13-candy-crush/CandyGame.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
38 changes: 38 additions & 0 deletions materials/2023-2024/11b/2023-10-13-candy-crush/ShapeTile.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions materials/2023-2024/11b/2023-10-13-candy-crush/Tile.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit e453f37

Please sign in to comment.