First, what happens when u set a boolean statement to an array ( e.g. bool hasObstacle[width * height] = { false }; )? does it set up a boolean for each cell in the grid?
Kind of, it makes an array with the same number of bool elements as the grid.
Secondly, the boolean function you have set up in board.cpp :
bool Board::CheckForObstacle( const Location & loc ) const
{
return hasObstacle[loc.y * width + loc.x];
}
why do you take loc.y * width + loc.x inside the array number an what does that statement return exactly?
If you have made it this far in the tutorials, you would have covered functions. Look at the function declaration and it tells you want it returns.
As far as ( loc.y * width + loc.x ) goes, this is covered in one of the tutorials, but I don't remember if chili covered it at that point or not. Each row is width wide, so loc.y being the vertical axis ( row number ), when you multiply the row number by the number of columns, you get an offset to the beginning of that row. When you add loc.x to it, you get the location of the cell in the array.
Imagine you have a 10x10 grid
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
...
If you want to know the index of the cell in the array you want, you multiply row number by the width of the grid.
Example ( x = 3, y = 2, width = 10 )
row_offset = y * width // 2 * 10 = 20
index = x + row_offset // 20 + 3 = 23
So now
return hasObstacle[loc.y * width + loc.x];
would return a bool from the array hasObstacle at index 23.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com