First differentiate between logic and visualization. It probably makes sense to have an array where each cell is either grass or water for the game logic (let's call it a
). From that you create a second array to store how that cell is rendered and consider edges (let's call that one v
).
To compute v
from a
you can look at the problem like this: for each cell v_ij
in v
you want to compute how to render it depending on a_ij
and the values from the neighbouring cells.
Example:
Theses are the values in a
:
0 0 0
1 0 0
1 1 1
How would you render the cell in the middle?
Without considering symmetry you actually have 2^8=256
different cases.
A good and fast solution could be to build a lookup table where you map each of the 256
cases to one of the edges you choose for that situation.
You can get the index into that lookup table by going through the 8 cells, thus getting 8 times 0 or 1, and setting this as the bits of an unsigned integer.
In the example above you would for example get 000 100 111 = 39
, thus the 39-th entry in your lookup.