Package threeChess
Class Board
- java.lang.Object
-
- threeChess.Board
-
- All Implemented Interfaces:
java.lang.Cloneable
- Direct Known Subclasses:
CheatBoard
public class Board extends java.lang.Object implements java.lang.Cloneable
Main class for representing game state. The board maps each position to the piece at that posiiton, or null is free. It also records previous moves, as well as whose move it is, and which pieces have been captured by which player.
-
-
Constructor Summary
Constructors Constructor Description Board(int time)
Initialises the board, placing all pieces at their initial position.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
clone()
Returns a deep clone of the board state, such that no operations will affect the original board instance.boolean
gameOver()
java.util.List<Piece>
getCaptured(Colour player)
Colour
getLoser()
The loser of the game is the player who had their King taken, or the player who ran out of time.Position[]
getMove(int index)
returns the move made at the corresponding index (starting from 1).int
getMoveCount()
Returns the number of moves made so far.Piece
getPiece(Position position)
Gets the piece at a specified position.java.util.Set<Position>
getPositions(Colour player)
Return a set of all the positions of pieces belonging to a player.int
getTimeLeft(Colour colour)
Get the time left for the specified player.Colour
getTurn()
Gets the player whose turn it currently isColour
getWinner()
The winner of the game is the player who takes another player's King, or the player with the highest score when another player runs out of time.boolean
isLegalMove(Position start, Position end)
Checks if a move is legal.void
move(Position start, Position end)
Executes a legal move.void
move(Position start, Position end, int time)
Executes a legal move.int
score(Colour player)
Calculates a players score, used for some variants of the game.Position
step(Piece piece, Direction[] step, Position current)
Performs one step of a move such as the L shaped move of a knight, or a diagonal step of a Bishop.
-
-
-
Constructor Detail
-
Board
public Board(int time)
Initialises the board, placing all pieces at their initial position. Note, unlike two person chess, the Queen is always on the left, and the King is always on his own colour.- Parameters:
time
- the number of milliseconds each player has in total for the entire game.
-
-
Method Detail
-
getPositions
public java.util.Set<Position> getPositions(Colour player)
Return a set of all the positions of pieces belonging to a player. This is a method of convenience, but is not very fast. Time concious players may prefer to maintain their own data structure that persists between moves.- Parameters:
player
- the Colour of the player owing the pieces- Returns:
- a Set of the positions that are occupied by a piece of the given colour.
-
getCaptured
public java.util.List<Piece> getCaptured(Colour player)
- Returns:
- a set of all the pieces captured by {@param player}.
-
getPiece
public Piece getPiece(Position position)
Gets the piece at a specified position.- Parameters:
position
- the position of the piece,- Returns:
- the piece at that position or null, if the position is vacant.
-
step
public Position step(Piece piece, Direction[] step, Position current) throws ImpossiblePositionException
Performs one step of a move such as the L shaped move of a knight, or a diagonal step of a Bishop. Rooks, Bishops and Queens may iterate one step repeatedly, but all other pieces can only move one step per move. Note the colour of the piece is relevant as moving forward past the 4th row is actually moving backwards relative to the board. It does not check whether the move is legal or possible.- Parameters:
piece
- the piece being movedstep
- an array of the direction sequence in the stepcurrent
- the starting position of the step.- Returns:
- the position at the end of the step.
- Throws:
ImpossiblePositionException
- if the step takes piece off the board.
-
isLegalMove
public boolean isLegalMove(Position start, Position end)
Checks if a move is legal. The move is specified by the start position (where the moving piece begins), and the end position, where the piece intends to move to. The conditions checked are: there is a piece at the start position; the colour of that piece correspond to the player whose turn it is; if there is a piece at the end position, it cannot be the same as the moving piece; the moving piece must be executing one or more steps allowed for their type, including two steps forward for initial pawn moves and castling left and right; pieces that can make iterated moves must iterate a single step type and cannot pass through any other piece. Note, en passant is not allowed, you can castle after King or rook have moved but they must have returned to their initial position, all pawns reaching the back row are promoted to Queen, you may move into check, and you may leave your king in check, and you may castle across check.- Parameters:
start
- the starting position of the pieceend
- the end position the piece intends to move to- Returns:
- true if and only if the move is legal in the rules of the game.
-
move
public void move(Position start, Position end, int time) throws ImpossiblePositionException
Executes a legal move. If a piece is taken it is replaced at that position by the taking piece. The game ends when a King is taken. When a pawn reaches the back rank it is automatically promoted to Queen.- Parameters:
start
- the starting position of the moveend
- the ending position of the movetime
- the number of milliseconds taken to play the move- Throws:
ImpossiblePositionException
- if the move is not legal
-
move
public void move(Position start, Position end) throws ImpossiblePositionException
Executes a legal move. If a piece is taken it is replaced at that position by the taking piece. The game ends when a King is taken. When a pawn reaches the back rank it is automatically promoted to Queen. Method overloaded to allow for untimed games.- Parameters:
start
- the starting position of the moveend
- the ending position of the move- Throws:
ImpossiblePositionException
- if the move is not legal
-
getTurn
public Colour getTurn()
Gets the player whose turn it currently is- Returns:
- the colour of the player whose turn it is.
-
getMoveCount
public int getMoveCount()
Returns the number of moves made so far.- Returns:
- the number of moves made in the game.
-
getMove
public Position[] getMove(int index)
returns the move made at the corresponding index (starting from 1).- Parameters:
index
- the index of the move- Returns:
- an array containing the start position and the end position of the move, in that order
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the index does not correspond to a move.
-
score
public int score(Colour player)
Calculates a players score, used for some variants of the game. The score is the combined piece values of the players pieces on the board, plus the value of the pieces taken by that player. This is a convenience method which gives a basic utility value. It can be used to encourage more aggressive play in agents, but the traditional scoring is +1 for taking a King, and -1 for losing a King,- Parameters:
player
- the colour of the player- Returns:
- the score of the player.
-
gameOver
public boolean gameOver()
- Returns:
- true if the game has ended
-
getWinner
public Colour getWinner()
The winner of the game is the player who takes another player's King, or the player with the highest score when another player runs out of time.- Returns:
- the winner of the game or null if it's a draw or not yet decided.
-
getLoser
public Colour getLoser()
The loser of the game is the player who had their King taken, or the player who ran out of time.- Returns:
- the loser of the game or null if its a draw or not yet decided.
-
getTimeLeft
public int getTimeLeft(Colour colour)
Get the time left for the specified player.- Returns:
- the time remaining, in milliseconds.
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
Returns a deep clone of the board state, such that no operations will affect the original board instance.- Overrides:
clone
in classjava.lang.Object
- Returns:
- a deep clone of the board state.
- Throws:
java.lang.CloneNotSupportedException
-
-