hexIT
Class HexagonalBoard

java.lang.Object
  extended byhexIT.Board
      extended byhexIT.HexagonalBoard
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class HexagonalBoard
extends Board
implements java.io.Serializable, java.lang.Cloneable

A hexagonal board is a board which is itself arranged into one big hexagon composed of smaller ones. A hexagonal board of size 3 is depicted below.

 Coord System & Example:
                                          y-coord
                                 0 1 2
                                / / / 3
                            0- . . . / 4
                           1- . . . . /
             x-coord      2- . . . . .
                           3- . . . x    <-- that 'x' is at position (3,4)
                            4- . . .


 

See Also:
Serialized Form

Field Summary
static int DIR_E
           
static int DIR_NE
           
static int DIR_NW
           
static int DIR_SE
           
static int DIR_SW
           
static int DIR_W
           
 
Constructor Summary
HexagonalBoard()
          Create an empty hexagonal board with no size.
HexagonalBoard(int new_size)
          Create a hexagonal board of the specified size.
 
Method Summary
 int distance(Hexagon h1, Hexagon h2)
          Returns the minimum # of hexes need to travel from one hexagon to the other.
 java.util.List getExterior()
          Returns the list of hexagons on the outer edges.
 java.util.List getFOV(int hexx1, int hexy1, int maxradius)
          Get the list of hexagons in the Field-of-View from the source hexagon at hex location (hexx1, hexy1).
 Hexagon getHexagon(int x, int y)
          Return the hexagon at hex location (x,y) or null if invalid coordinates, or null if the location is not valid.
 Hexagon getHexagonByPixel(int pixx, int pixy)
          Gets the hexagon which contains this pixel.
 Hexagon[] getHexagons()
          Returns an array of all valid hexagons on this hexagonal board, in no particular order.
 java.util.List getLOS(int hexx1, int hexy1, int hexx2, int hexy2)
          Returns the line-of-sight from hexagon at hex location (hexx1,hexy1).
 java.util.List getLOSPixel(int hexx1, int hexy1, int iBx, int iBy)
          Returns the line-of-sight from hexagon at hex location (hexx1,hexy1).
 Hexagon[] getNonOccupiedHexagons()
          Returns an array of all valid hexagon on this hexagonal board that are not occupied by pieces, in no particular order.
 Hexagon[] getOccupiedHexagons()
          Returns an array of all valid hexagon on this hexagonal board that are occupied by pieces, in no particular order.
 java.util.List getShortestPath(int hexx1, int hexy1, int hexx2, int hexy2)
          Returns the list of hexagons that forms the shortest path between the source hexagon at (hexx1,hexy1) and destination hexagon at (hexx2,hexy2).
 int getSideLength()
          Returns the side length.
 java.util.List getSurrounding(int hexx, int hexy, int radius)
          Get all the hexagons surround the given source hexagon at (hexx,hexy) within the specified radius.
 Hexagon[] getSurroundingArray(Hexagon h, int radius)
          Get all the hexagons surround the given source hexagon (h) within the specified radius.
 int isStraightPath(Hexagon h1, Hexagon h2)
          If there is a straight line of hexagons (ie: you can get there by going in one constant direction) between the source and destination, then the direction (from source to dest) is returned.
 int isStraightPath(int hexx1, int hexy1, int hexx2, int hexy2)
          If there is a straight line of hexagons (ie: you can get there by going in one constant direction) between the source and destination, then the direction (from source to dest) is returned.
 boolean isValidCoord(int x, int y)
          Returns true if the specified hexagonal coordinates are not valid.
 void moveObject(Hexagon h1, Hexagon h2)
          Move the object at hex location h1 to h2.
 void moveObject(int x, int y, int direction)
          Move the object at hex location (x,y) in the specified direction.
 void moveObject(int x1, int y1, int x2, int y2)
          Move the object at hex location (x1,y1) to (x2,y2).
 Hexagon nextHexagon(Hexagon h, int dir)
          Returns the hexagon which is of distance 1 away in the specified direction from the specified Hexagon.
 int nextX(int x, int dir)
          Returns the next value of the hex x-coordinate given the current value x and moving in the specified direction.
 int nextY(int y, int dir)
          Returns the next value of the hex y-coordinate given the current value x and moving in the specified direction.
 void putObject(int x, int y, Drawable obj)
          Put the specified object on hex location (x,y).
 void removeObject(int x, int y)
          Remove the object on hex location (x,y).
 java.util.Iterator tilesIterator()
          Returns an iterator over all hexagons on the board.
 
Methods inherited from class hexIT.Board
createFrame, findObject, setFrameSize, show
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIR_NW

public static final int DIR_NW
See Also:
Constant Field Values

DIR_NE

public static final int DIR_NE
See Also:
Constant Field Values

DIR_W

public static final int DIR_W
See Also:
Constant Field Values

DIR_E

public static final int DIR_E
See Also:
Constant Field Values

DIR_SW

public static final int DIR_SW
See Also:
Constant Field Values

DIR_SE

public static final int DIR_SE
See Also:
Constant Field Values
Constructor Detail

HexagonalBoard

public HexagonalBoard()
Create an empty hexagonal board with no size.


HexagonalBoard

public HexagonalBoard(int new_size)
Create a hexagonal board of the specified size.

Method Detail

distance

public int distance(Hexagon h1,
                    Hexagon h2)
Returns the minimum # of hexes need to travel from one hexagon to the other.

Specified by:
distance in class Board

isValidCoord

public boolean isValidCoord(int x,
                            int y)
Returns true if the specified hexagonal coordinates are not valid. An example of an invalid coordinate is on that does not resolve to a position on an actual board. Using the above example of a hexagonal board of size 3, (-1, -1) is not valid, (3,0) is not valid, (100,1) is not valid, but (1,1) is valid.


getHexagon

public Hexagon getHexagon(int x,
                          int y)
Return the hexagon at hex location (x,y) or null if invalid coordinates, or null if the location is not valid.


getSideLength

public int getSideLength()
Returns the side length. The side length is the number of valid hexagons on one axis: 2*size-1 in the case of these types of boards.


moveObject

public void moveObject(Hexagon h1,
                       Hexagon h2)
Move the object at hex location h1 to h2.


moveObject

public void moveObject(int x1,
                       int y1,
                       int x2,
                       int y2)
Move the object at hex location (x1,y1) to (x2,y2).


moveObject

public void moveObject(int x,
                       int y,
                       int direction)
Move the object at hex location (x,y) in the specified direction.


putObject

public void putObject(int x,
                      int y,
                      Drawable obj)
Put the specified object on hex location (x,y).


removeObject

public void removeObject(int x,
                         int y)
Remove the object on hex location (x,y).


tilesIterator

public java.util.Iterator tilesIterator()
Returns an iterator over all hexagons on the board.

Specified by:
tilesIterator in class Board

getHexagonByPixel

public Hexagon getHexagonByPixel(int pixx,
                                 int pixy)
Gets the hexagon which contains this pixel.


getHexagons

public Hexagon[] getHexagons()
Returns an array of all valid hexagons on this hexagonal board, in no particular order.


getOccupiedHexagons

public Hexagon[] getOccupiedHexagons()
Returns an array of all valid hexagon on this hexagonal board that are occupied by pieces, in no particular order.


getNonOccupiedHexagons

public Hexagon[] getNonOccupiedHexagons()
Returns an array of all valid hexagon on this hexagonal board that are not occupied by pieces, in no particular order.


getShortestPath

public java.util.List getShortestPath(int hexx1,
                                      int hexy1,
                                      int hexx2,
                                      int hexy2)
Returns the list of hexagons that forms the shortest path between the source hexagon at (hexx1,hexy1) and destination hexagon at (hexx2,hexy2).

Specified by:
getShortestPath in class Board

getExterior

public java.util.List getExterior()
Returns the list of hexagons on the outer edges.

Specified by:
getExterior in class Board

nextHexagon

public Hexagon nextHexagon(Hexagon h,
                           int dir)
Returns the hexagon which is of distance 1 away in the specified direction from the specified Hexagon. If the coordinates are invalid, returns null.


nextX

public int nextX(int x,
                 int dir)
Returns the next value of the hex x-coordinate given the current value x and moving in the specified direction.


nextY

public int nextY(int y,
                 int dir)
Returns the next value of the hex y-coordinate given the current value x and moving in the specified direction.


isStraightPath

public int isStraightPath(Hexagon h1,
                          Hexagon h2)
If there is a straight line of hexagons (ie: you can get there by going in one constant direction) between the source and destination, then the direction (from source to dest) is returned. Otherwise, -1 is returned.


isStraightPath

public int isStraightPath(int hexx1,
                          int hexy1,
                          int hexx2,
                          int hexy2)
If there is a straight line of hexagons (ie: you can get there by going in one constant direction) between the source and destination, then the direction (from source to dest) is returned. Otherwise, -1 is returned.


getLOSPixel

public java.util.List getLOSPixel(int hexx1,
                                  int hexy1,
                                  int iBx,
                                  int iBy)
Returns the line-of-sight from hexagon at hex location (hexx1,hexy1). This is done by drawing a line between the middle pixel of the source hexagon to the pixel (iBx,iBy) and seeing which hexagons intersect with the given line. A list of the intersecting hexagons is returned.


getLOS

public java.util.List getLOS(int hexx1,
                             int hexy1,
                             int hexx2,
                             int hexy2)
Returns the line-of-sight from hexagon at hex location (hexx1,hexy1). This is done by drawing a line between the middle pixel of the source hexagon to the middle pixel of the hexagon at (hexx2,hexy2) and seeing which hexagons intersect with the given line. A list of the intersecting hexagons is returned. If the line of sight is blocked by an object on the way, the line of sight is interrupted and does not continue. Note that it is assumed that objects take up the full hexagon on which they reside and that they block the sight of anything behind them.

Specified by:
getLOS in class Board

getSurroundingArray

public Hexagon[] getSurroundingArray(Hexagon h,
                                     int radius)
Get all the hexagons surround the given source hexagon (h) within the specified radius. The radius is the distance measured in hexagons from the surrounding hexagons to the center hexagon. Only valid hexagons are returned.


getSurrounding

public java.util.List getSurrounding(int hexx,
                                     int hexy,
                                     int radius)
Get all the hexagons surround the given source hexagon at (hexx,hexy) within the specified radius. The radius is the distance measured in hexagons from the surrounding hexagons to the center hexagon. Only valid hexagons are returned.

Specified by:
getSurrounding in class Board

getFOV

public java.util.List getFOV(int hexx1,
                             int hexy1,
                             int maxradius)
Get the list of hexagons in the Field-of-View from the source hexagon at hex location (hexx1, hexy1). The field-of-view is what can be seen from a 360-degree cone of sight centered at the source, with any objects considered to be fully occupying their hexagons (blocking the view behind them). Note: the maxradius parameter is not yet being used.

Specified by:
getFOV in class Board