rules
Class Position

java.lang.Object
  extended by rules.Position

public final class Position
extends java.lang.Object

Myriad's representation of a particular position. This is a basic class that underlines the properties of a position, such as the availability of castling, 50 move rule count, etc. The piece-centric board representation is used, with coordinates used by the "0x88" algorithm. Note that this is an immutable object. Once an object is created via a constructor it cannot be changed!

Author:
Spork Innovation Technologies

Field Summary
static byte[] BLACK_PAWN_ATTACK
          The storage for the difference of all pawn capture moves for black.
static int BLACK_WINS
          The signal given by the gameResult() method that means black wins.
static byte[] DIAGONALS
          The storage for the differences of all diagonal moves.
static byte DOWN_MOVE
          The distance between 1 down move.
static int DRAW
          The signal given by the gameResult() method that means a draw (or stalemate).
static byte[] HORIZONTALS
          The storage for the differences of all horizontal/vertical moves.
static byte[] KNIGHT_MOVES
          The storage for the differences of all knight moves.
static byte LEFT_DOWN_MOVE
          The distance between 1 diagonal left and down move.
static byte LEFT_MOVE
          The distance between 1 left move.
static byte LEFT_UP_MOVE
          The distance between 1 diagonal left and up move.
static int NO_RESULT
          The signal given by the gameResult() method that means no result has been reached yet.
static byte[] RADIALS
          The storage for the differences of all radial moves.
static byte RIGHT_DOWN_MOVE
          The distance between 1 diagonal right and down move.
static byte RIGHT_MOVE
          The distance between 1 right move.
static byte RIGHT_UP_MOVE
          The distance between 1 diagonal right and up move.
static byte UP_MOVE
          The distance between 1 up move.
static byte[] WHITE_PAWN_ATTACK
          The storage for the difference of all pawn capture moves for white.
static int WHITE_WINS
          The signal given by the gameResult() method that means white wins.
 
Constructor Summary
Position()
          Default Constructor: Constructs a Position object with the same settings as the initial start-up position.
Position(byte fifty_move, byte epsq, boolean[] castling_rights, boolean whiteturn, Piece[] w_map, Piece[] b_map)
          Constructor: Constructs a board objects with the following parameters:
 
Method Summary
 Move[] generateAllMoves()
          Generates all the moves possible in this Position object.
 byte get50MoveCount()
          Returns this position's current 50 move rule counter.
 Piece[] getBlackPieces()
          Returns an array containing all the black pieces.
 boolean[] getCastlingRights()
          Gets the castling rights of a board in the order specified in the Constructor.
 byte getEnPassantSquare()
          Returns the "en passant-able" square using 0x88 cooridinates in this position.
 int getResult()
          Returns the ending game decision for the positions.
 Piece getSquareOccupier(byte square)
          Returns the occupier of a specific square, or the null piece if the square is empty.
 Piece getSquareOccupier(byte square, boolean toSearch)
          Returns the occupier of a specific square, or the null piece if the square is empty.
 Piece getSquareOccupier(byte square, Piece[] map)
          Returns the occupier of a specific square, or the null piece if the square is empty.
 Piece[] getWhitePieces()
          Returns an array containing all the white pieces.
 boolean isInCheck()
          Checks if in the current position, whether or not the king is in check.
 boolean isWhiteToMove()
          Returns whether or not if it is white to play in this position.
 Position makeMove(Move m)
          Makes a move on the position.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UP_MOVE

public static final byte UP_MOVE
The distance between 1 up move.

See Also:
Constant Field Values

DOWN_MOVE

public static final byte DOWN_MOVE
The distance between 1 down move.

See Also:
Constant Field Values

LEFT_MOVE

public static final byte LEFT_MOVE
The distance between 1 left move.

See Also:
Constant Field Values

RIGHT_MOVE

public static final byte RIGHT_MOVE
The distance between 1 right move.

See Also:
Constant Field Values

LEFT_UP_MOVE

public static final byte LEFT_UP_MOVE
The distance between 1 diagonal left and up move.

See Also:
Constant Field Values

RIGHT_UP_MOVE

public static final byte RIGHT_UP_MOVE
The distance between 1 diagonal right and up move.

See Also:
Constant Field Values

LEFT_DOWN_MOVE

public static final byte LEFT_DOWN_MOVE
The distance between 1 diagonal left and down move.

See Also:
Constant Field Values

RIGHT_DOWN_MOVE

public static final byte RIGHT_DOWN_MOVE
The distance between 1 diagonal right and down move.

See Also:
Constant Field Values

KNIGHT_MOVES

public static final byte[] KNIGHT_MOVES
The storage for the differences of all knight moves.


DIAGONALS

public static final byte[] DIAGONALS
The storage for the differences of all diagonal moves.


HORIZONTALS

public static final byte[] HORIZONTALS
The storage for the differences of all horizontal/vertical moves.


RADIALS

public static final byte[] RADIALS
The storage for the differences of all radial moves.


WHITE_PAWN_ATTACK

public static final byte[] WHITE_PAWN_ATTACK
The storage for the difference of all pawn capture moves for white.


BLACK_PAWN_ATTACK

public static final byte[] BLACK_PAWN_ATTACK
The storage for the difference of all pawn capture moves for black.


DRAW

public static final int DRAW
The signal given by the gameResult() method that means a draw (or stalemate).

See Also:
Constant Field Values

WHITE_WINS

public static final int WHITE_WINS
The signal given by the gameResult() method that means white wins.

See Also:
Constant Field Values

BLACK_WINS

public static final int BLACK_WINS
The signal given by the gameResult() method that means black wins.

See Also:
Constant Field Values

NO_RESULT

public static final int NO_RESULT
The signal given by the gameResult() method that means no result has been reached yet.

See Also:
Constant Field Values
Constructor Detail

Position

public Position(byte fifty_move,
                byte epsq,
                boolean[] castling_rights,
                boolean whiteturn,
                Piece[] w_map,
                Piece[] b_map)
Constructor: Constructs a board objects with the following parameters:

Parameters:
fifty_move - The 50 move rule counter.
three_fold - The 3 fold repetition counter.
epsq - The en passant square.
castling_rights - An array storing the castling rights, with index 0 being white to the kingside, 1 being black to the kingside, 2 being white to the queenside, 3 being black to the queenside.
whiteturn - If it is currently white to move.
w_map - An array containing all the current white pieces.
b_map - An array containing all the current black pieces.

Position

public Position()
Default Constructor: Constructs a Position object with the same settings as the initial start-up position.

Method Detail

getCastlingRights

public boolean[] getCastlingRights()
Gets the castling rights of a board in the order specified in the Constructor.


get50MoveCount

public byte get50MoveCount()
Returns this position's current 50 move rule counter.

Returns:
The 50 move rule counter.

getEnPassantSquare

public byte getEnPassantSquare()
Returns the "en passant-able" square using 0x88 cooridinates in this position.

Returns:
The en passant square in 0x88 coordinates.

isWhiteToMove

public boolean isWhiteToMove()
Returns whether or not if it is white to play in this position.

Returns:
A boolean signalling whether or not it is white's turn.

getWhitePieces

public Piece[] getWhitePieces()
Returns an array containing all the white pieces.

Returns:
an array containing all the white pieces.

getBlackPieces

public Piece[] getBlackPieces()
Returns an array containing all the black pieces.

Returns:
an array containing all the black pieces.

generateAllMoves

public Move[] generateAllMoves()
Generates all the moves possible in this Position object. This method does so by generating all the moves according to the pieces and filters out all moves that result in a check.

Returns:
An array containing all the legal moves in this position.

isInCheck

public boolean isInCheck()
Checks if in the current position, whether or not the king is in check.

Returns:
true if the king is in check, false otherwise.

makeMove

public Position makeMove(Move m)
Makes a move on the position. Since Position objects are immutable, one must reassign the variable. e.g. p = p.makeMove(m).

Parameters:
m - The move to make on the current Position.
Returns:
A new position with the move made on it.

getResult

public int getResult()
Returns the ending game decision for the positions. This returns the result of this position object, it it has already been decided.

Returns:
the result of the game, masked by one of the constants. DRAW for a draw. WHITE_WINS if white wins. BLACK_WINS if black wins. NO_RESULT otherwise.

getSquareOccupier

public Piece getSquareOccupier(byte square)
Returns the occupier of a specific square, or the null piece if the square is empty. This method does so by running through both arrays of maps.

Returns:
the occupier of a specific square, the null piece if the square is empty.

getSquareOccupier

public Piece getSquareOccupier(byte square,
                               Piece[] map)
Returns the occupier of a specific square, or the null piece if the square is empty. This method does so by running a specific map.

Returns:
the occupier of a specific square, the null piece if the square is empty.

getSquareOccupier

public Piece getSquareOccupier(byte square,
                               boolean toSearch)
Returns the occupier of a specific square, or the null piece if the square is empty. This method does so by running a specific map.

Parameters:
square - The square to search for.
toSearch - The map to search in, true if white, false if black.
Returns:
the occupier of the specific square, the null piece if the square is empty.