Class Move
java.lang.Object
ubc.team09.state.Move
This class is used to handle moves on the board. Use this class to encode
or decode moves as
The section below describes how moves are encoded as
Representation of Moves
A move can be represented with three values:
Since these are all position indices, they can be represented by something as small as a
ints and check their validity.
The section below describes how moves are encoded as
ints, but
you don't need to understand those details to use the class.
Representation of Moves
A move can be represented with three values:
- The position of the queen we want to move,
- The queen's new position, and
- The position of the queen's arrow.
Since these are all position indices, they can be represented by something as small as a
byte. To ensure we have the convenience of using
primitive values, we will represent these three values with a single
int, where the bits are used like so:
- The first (least significant) 8 bits are the starting position of the queen.
- The next 8 bits are the queen's destination.
- The next 8 bits are the target for the queen's arrow.
- The remaining (most significant) 8 bits indicate which player made the move. This could be deduced from other board state information (i.e., checking which queen position array the starting position belongs to), but we're not using these bits for anything else so we might as well store the player color for convenience.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic bytearrow(int move) Reads the position where the queen fires an arrow from a move.static intencode(byte start, byte end, byte arrow, byte player) Encodes a move as an integer.static byteend(int move) Reads the ending position (of the queen) from a move.static booleanisLegal(BoardState board, int move) Returnstrueif and only if a move is legal from a given board state.static booleanReturnstrueif and only if a move is legal from a given board state.static byteplayer(int move) Reads the active player from a move.static bytestart(int move) Reads the starting position (of the queen) from a move.
-
Field Details
-
NULL_MOVE
public static final int NULL_MOVERepresents a null move.- See Also:
-
-
Constructor Details
-
Move
public Move()
-
-
Method Details
-
encode
public static int encode(byte start, byte end, byte arrow, byte player) Encodes a move as an integer.- Parameters:
start- The starting position index (0-99) of the queen to move.end- The position index the queen will move to.arrow- The position index where the queen will fire her arrow after moving.player- The player making the move (0for White, and1for Black).- Returns:
- An integer representation of the move.
-
start
public static byte start(int move) Reads the starting position (of the queen) from a move.- Parameters:
move- The integer representation of a move. Seeencode(byte, byte, byte, byte)- Returns:
- A position index.
-
end
public static byte end(int move) Reads the ending position (of the queen) from a move.- Parameters:
move- The integer representation of a move. Seeencode(byte, byte, byte, byte)- Returns:
- A position index.
-
arrow
public static byte arrow(int move) Reads the position where the queen fires an arrow from a move.- Parameters:
move- The integer representation of a move. Seeencode(byte, byte, byte, byte)- Returns:
- A position index.
-
player
public static byte player(int move) Reads the active player from a move.- Parameters:
move- The integer representation of a move. Seeencode(byte, byte, byte, byte)- Returns:
0for White, and1for Black.
-
isLegal
Returnstrueif and only if a move is legal from a given board state. -
isLegal
Returnstrueif and only if a move is legal from a given board state.
-