Prima:
Codice:
private Droppable getGemToDelete(Droppable falshingGem)
{
//TODO: REFACTOR THIS
Region region = falshingGem.getRegion();
Cell cell = new Cell(region.getTopRow(), region.getLeftColumn());
Droppable gemToDelete = searchGemToDelete(cell);
if(gemToDelete == null)
{
gemToDelete = searchLeft(cell);
if(gemToDelete == null)
{
gemToDelete = searchRight(cell);
if(gemToDelete == null)
{
gemToDelete = searchUp(cell);
}
}
}
return gemToDelete;
}
private Droppable searchGemToDelete(Cell cell)
{
if((cell.getRow() < getGrid().getNumberOfRows() - 1))
{
return getGem(cell.getLower());
}
return null;
}
private Droppable searchLeft(Cell cell)
{
if(cell.getColumn() >= 1)
{
return getGem(cell.getLeft());
}
return null;
}
private Droppable searchRight(Cell cell)
{
if(cell.getColumn() < getGrid().getNumberOfColumns() - 1)
{
return getGem(cell.getRight());
}
return null;
}
private Droppable searchUp(Cell cell)
{
if(cell.getRow() > 1)
{
return getGem(cell.getUpper());
}
return null;
}
Dopo:
Codice:
private Droppable getGemToDelete(Droppable falshingGem)
{
Region region = falshingGem.getRegion();
Cell cell = new Cell(region.getTopRow(), region.getLeftColumn());
Direction directions[] = {Direction.GO_DOWN, Direction.GO_LEFT, Direction.GO_RIGHT, Direction.GO_UP};
for (Direction direction : directions)
{
Droppable gemToDelete = searchGemToDelete(cell, direction);
if (gemToDelete != null)
{
return gemToDelete;
}
}
return null;
}
private Droppable searchGemToDelete(Cell cell, Direction direction)
{
final int rowToSearch = cell.getRow() + direction.deltaY();
final int columnToSearch = cell.getColumn() + direction.deltaX();
final boolean isValidCell = getGrid().isValidCell(rowToSearch, columnToSearch);
if (!isValidCell)
{
return null;
}
return getGem(new Cell(rowToSearch, columnToSearch));
}