views:

123

answers:

2

The most basic way of representing a quadrile plane (a bunch of squares) is to use a two-dimensional array.

In C# we declare this as int[,] and can make our plane as big as we want:

string[3,3] => tic-tac-toe board (or similar)
string[8,8] => chess or checkers board

To "move" an item on the plane, we would just asign it toa new "position"

//using our tic-tac-toe board:
string[0,0] = "x"; //top-left
string[1,1] = "o"; //middle-middle

//to move
string[0,1] = bN; //Black Knight's starting positon
string[2,2] = bN; //Black Knight moves
string[0,1] = String.Empty;

So, how would you represent a hexagonal plane (a bunch of hexagons) and how would movement from one position to the next be handled?

Note: This is not purely theoretical, as I have an idea for a little game in my head which would require this kind of movement, but I can't wrap my head around how it would be done. I've looked through some of the other questions here, but can't really find a good match...

+7  A: 

I do not know if this is the optimal solution but what I would do is create a new class of board the board would be a collection of "cells" each cell would contain a pointer to each of it's neighboring cell (or null if the cell is on a edge). You could implement some iterators on the board class that would walk the cells.

You would have to treat it more like a List instead of a vector. But it is at least a start.


Another solution is set you board up like this alt text

and still just use the [,] to access each cell but it will take a little more math to figure out if you are traversing cells (Up Right is [+1,-1], Right is [+1,0], Down Right is [0,+1], Down Left is [-1,+1], Left is [-1,0], Up Left is [0,-1])

EDIT

If you want vertical walls instead of a slant just take make your width(X) equal X + Y*2 then on each row make the current row number (y) and make the cells 0 to Y-y and X-y to X off limits.

Example:

const int X = 10;
const int Y = 10;
int grid[,] = new int[X+(2*Y), Y];

bool IsCellOffLimits(int x, int y)
{
    return (x < Y-y || x > X-y || y < 0 || y > Y);
}

you waste a little memory space but it gives you a board like this alt text

If you are Very Clever© you can just use the normal space but just have your code have anything in that Y-y or X-y range be on the opposite side of the board. But ill leave that code up to the reader.

Scott Chamberlain
This is probably your best solution. The only real difference between a board of squares and a board of hexes is how you display it, not how you store it.
Brian S
A: 

Three directions, left/right, up/down, funny angle one way/other way.

public class Player
{
    public int X { get; set; }
    public int Y { get; set; }

    public void MoveLeft() { X++; }
    public void MoveRight() { X--; }

    public void MoveUp() { Y++; }
    public void MoveDown() { Y--; }

    public void MoveFunny() { Y++; X++; }
    public void MoveOtherFunny() { Y--; X--; }
}
Jimmy Hoffa