I am attempting to make a grid system with two sides controlling half each, however i want to have one side able to walk over to the other side and change grids he’s walking on to his colour, however, if he boxes in some grids they would also change even tho the character has not walked on the grid(Shown in picture below).
Question now is how can i change the red squares surrounded by blue squares to blue
I’m assuming you’re question is ‘how do i figure out if a group of tiles is surrounded and identify all the surrounded tiles’ rather than just ‘how to i change the colour of something’.
When a new tile is changed, check its neighbours and on any neighbour that isn’t already the same colour, perform a function that expands out from that tile, only considering tiles of the same color, until it finds an edge of the board. If it finds an edge of the board then it wasn’t surrounded. If it doesn’t find an edge of the board then it was surrounded, and in the process of doing the search you’ve created a list of all the tiles that have become surrounded.
The basic logic would look something like this:
- I would then just iterate over the list of tiles it produced and change their color accordingly.
CheckIfSurrounded (Tile TileToCheck)
{
List<Tile> OpenList
List<Tile> ClosedList
Add the TileToCheck to the openlist to start the loop off.
While (OpenList is not empty)
{
CurrentTile = Grab an item from the openlist
Remove CurrentTile from the open list and add it to the closed list.
For each (neighbour of CurrentTile)
{
if (the owner of the neighbouring tile is the same as the owner of CurrentTile)
{
if (neighbouring tile is not already in the openlist or the closedlist)
{
if (Neighbouring tile has fewer than 4 neighbours)
Return; - We found an edge of the board, so this group of tiles arn't surrounded.
else
Add the neighbouring tile to the OpenList
}
}
}
// If we hit the end of the while loop without finding an edge of the board then the group of tiles was surrounded
// Helpfully, the ClosedList is also now a list of all the tiles in this group of surrounded tiles.
}
}