// hexagonal coordinate class // understands adjacency // coordinates are only by reference to one grid import java.lang.Object; import HexGrid; public class HexCoord extends Object { public int x, y; HexGrid myHexGrid; public HexCoord(HexGrid g, int newx, int newy) { myHexGrid = g; x = newx; y = newy; } // how is this different than clone? public HexCoord(HexCoord hc) { myHexGrid = hc.myHexGrid; x = hc.x; y = hc.y; } public void move(int newx, int newy) { x = newx; y = newy; } public void moveInDirection(int direction) { if (x % 2 == 0) switch(direction) { case 0: y--; break; case 1: x++; y--; break; case 2: x++; break; case 3: y++; break; case 4: x--; break; case 5: x--; y--; break; } else switch(direction) { case 0: y--; break; case 1: x++; break; case 2: x++; y++; break; case 3: y++; break; case 4: x--; y++; break; case 5: x--; break; } } public void moveAwayFromDirection(int direction) { moveInDirection((direction + 3) % 6); } // restrict coord to grid, doing wraparound public void renormalize() { x %= myHexGrid.width; if (x < 0) x += myHexGrid.width; y %= myHexGrid.height; if (y < 0) y += myHexGrid.height; } public String toString() { return new String("HexCoord: (" + x + "," + y + ")"); } }