The Minefield game I am developing for the Ultimate Retro Project uses a hex grid instead of your typical square grid. The reason for this decision was twofold. First, I wanted my minefield game to be different from other ones that I have seen and having a different type of grid certainly accomplishes that goal. The second reason was future planning. I knew that in the future I wanted to create some strategy games and a lot of the pen and paper strategy games use hex maps. The thought here is that by using hex maps in my Minefield game I would be able to build some of the basic technology that would be used by future games. Those strategy games are still in the back of my mind so perhaps someday this will actually happen but I have other projects that need to be finished before I even think of developing these future strategy games.
The key question is why do so many pen and paper strategy games use hex maps? The answer to that is that you gain more flexibility in movement. A square only has four directions that you can move in while a hex map gives you 6 directions. If you count diagonal directions then the case for hex maps grows even stronger. With a square grid, you would have 8 directions, with the cardinal directions having an distance of 1 unit while the diagonal directions having a distance of 1.4142 so moving diagonally gives a player extra movement which in strategy games may not be desirable. More to the point, if you do allow for diagonal movement, then the hex map gives you 12 directions and the error for moving diagonally is slightly smaller so diagonal movement on a hex map gives a player less of an advantage.
Implementing a hex map
When I got around to working on hex minefield, the first problem I encountered was the big one of how to actually deal with the hex map. Grids are very simple to deal with, but the hex map looks much more complex. Thankfully, this was one of those cases where looks are deceiving. When originally working out the code I looked at a sheet of hex paper while scratching my head. I then looked up at my ceiling which happens to have an offset grid pattern on it. Looking back at the hex paper, the obvious solution immediately clicked in. A hex map is essentially a grid but with ever other row offset by half a square. That may not be technically correct, but from a data management point of view it works perfectly. What this means is that map information can simply be stored as if it was a grid making it incredibly easy to manipulate.
Movement through the map is a bit trickier. How the hexes are laid out can alter this, but if the point of the hex is facing North then movement through the grid is as follows:
- North East = X + 1 if on odd row otherwise X unchanged, Y - 1
- East = X + 1, Y unchanged
- South East = X + 1 if on odd row otherwise X unchanged, Y + 1
- South West = X – 1 if on even row otherwise X unchanged, Y + 1
- West = X – 1, Y unchanged
- North West = X – 1 if on even row otherwise X unchanged, Y – 1
Determining if the row is even or not can be done using a very simple boolean operation. if ((row & 1) == 1) then the row is odd.