I'm creating a puzzle mini game where there is a board of gems, varying in color, and chosen at random.
This is what my code generates:
When you click on a gem the game should traverse the board and find all connecting gems of the same color. When you click the orange square all orange square neighbors are selected.
Here is my Board class. The locateNeighbors() method recursively searches adjoining grid tiles until it reaches a non-matching gem or the edge of the board.
public class Board {
public int rows, columns;
public int x, y;
private GemPool pool;
private Gem[][] board;
public Board(int rows, int columns) {
x = 0;
y = 0;
this.rows = rows;
this.columns = columns;
board = new Gem[rows][columns];
pool = new GemPool(COLOR.BLUE, COLOR.RED, COLOR.YELLOW, COLOR.GREEN, COLOR.ORANGE, COLOR.PURPLE);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
board[i][j] = pool.grabGem();
}
}
}
public Gem at(int row, int column) {
return board[row][column];
}
public void draw(SpriteBatch batch) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
String color = board[i][j].getColorName();
//I'm aware of the magic numbers here
batch.draw(pool.grabTexture(color), (i * 72) + x, (j * 72) + y);
}
}
}
public void dispose() {
pool.dispose();
}
public void click(Vector3 touchPos) {
//I'm aware of the magic numbers here
int x = (int)(touchPos.x / 72);
int y = (int)(touchPos.y / 72);
if(x < rows && y < columns) {
Gem g = this.at(x, y);
HashSet<Point> matches = new HashSet<Point>();
locateNeighbors(x, y, g.getColor(), matches);
System.out.println(matches.size() + " matches found.");
}
}
private void locateNeighbors(int x, int y, COLOR color, HashSet<Point> matches) {
Point p = new Point(x, y);
if(matches.contains(p))
{
return;
}
else
{
matches.add(p);
}
//Check north
if(y + 1 < columns)
{
if(this.at(x, y + 1).getColor() == color)
locateNeighbors(x, y + 1, color, matches);
}
//Check east
if(x + 1 < rows)
{
if(this.at(x + 1, y).getColor() == color)
locateNeighbors(x + 1, y, color, matches);
}
//Check south
if(y - 1 >= 0)
{
if(this.at(x, y - 1).getColor() == color)
locateNeighbors(x, y - 1, color, matches);
}
//Check west
if(x - 1 >= 0)
{
if(this.at(x - 1, y).getColor() == color)
locateNeighbors(x - 1, y, color, matches);
}
}
}
I would love your opinions on optimizations that could be made to the recursive method.
Point
from a Java or something you wrote? If you wrote it be wary,contains
doesn't work the way you'd expect withHashSet
unless you overridehashCode()
andequals(Object)
from their parent class (probablyObject
). And when you do it, make sure you make anequals(Object)
, not anequals(Point)
(or both if you want), I ran into this issue and it took me a while to figure it out. :) \$\endgroup\$