Reputation: 81
If I have to iterate through a table of hexagonal cells checking for text inside them by conducting a recursive depth first search, arranged as shown: [Typing it out on StackOverflow apparently doesn't keep the formatting.]
Example 1:
Example 2:
What would be the best way to identifying them as "cells?" In other words, besides removing the textual diagonal lines and converting them into a 2D array with just numbers in it, what would be the best way to tell the computer in code to recognize x certain number of y characters resembles a "cell?"
Thanks in advance.
Upvotes: 2
Views: 151
Reputation: 7028
Easiest way to represent a hexagonal grid would be plain 2-d array with special rule about neighborhood of the cells. Take your second case for example, in matrix form it would be:
char M[][] =
{
{ 'b', 'g', 'g', 'b', ' ' },
{ 'g', ' ', 'B', 'B', 'B' },
{ 'g', 'B', ' ', 'b', 'g' },
{ 'B', ' ', 'g', 'g', 'g' }
}
Element in column m
in row n
is neighbor with:
m
and m + 1
in row n - 1
m - 1
and m + 1
in row n
m - 1
and m
in row n + 1
Upvotes: 1