I am currently making a program which optimizes the placement of houses on a grid to gain the most profit.
The area plan is on a grid - all house details (dimensions etc) are entered by a user.
All houses must be placed next to a road - the roads have already been added to the grid by the user.
I am currently struggling to write a function which checks if the grid has any available space left to add another house (the stopping condition for my optimization algorithm).
The available space won't be the empty squares on the grid, but the empty squares adjacent to the road. (This road can go both horizontal and vertical)
The grid:
Coords[][] grid;
If a coordinate on the grid is a road:
grid[x][y].getRoad() == true
The following function finds all neighbouring squares of any coordinate: (cols - number of columns in grid) (rows - number of rows in grid)
public static Coords[] addNeighboursSquare(Coords coord, int x, int y, int cols, int rows)
{
Coords[] neighbours = null;
//right
if (x < cols - 1)
{
neighbours[0] = AStarAlgorithm.grid[x + 1][y];
}
//left
if (x > 0)
{
neighbours[1] = AStarAlgorithm.grid[x - 1][y];
}
//top
if (y < rows - 1)
{
neighbours[2] = AStarAlgorithm.grid[x][y + 1];
}
//bottom
if (y > 0)
{
neighbours[3] = AStarAlgorithm.grid[x][y - 1];
}
return neighbours;
}
How do I loop through all roads squares in the grid, and create an array of ints containing the size of all 'gaps' along the road?