|
|
|
@ -32,15 +32,36 @@ public class GameState {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Construct a new GameState based on a previous state and a move made on that previous state.
|
|
|
|
|
private GameState(GameState prevState, Move move) {
|
|
|
|
|
ArrayList<Move> moves = prevState.getAllPossibleMoves();
|
|
|
|
|
if (!moves.contains(move)) {
|
|
|
|
|
throw new IllegalArgumentException("Move "+move+" not available for given game state.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isWhitesTurn = !prevState.isWhitesTurn;
|
|
|
|
|
boardSquares = new Square[prevState.boardSquares.length];
|
|
|
|
|
for(int i = 0; i < boardSquares.length; i++) {
|
|
|
|
|
boardSquares[i] = new Square(prevState.boardSquares[i]);
|
|
|
|
|
}
|
|
|
|
|
boardSquares[move.getDestIndex()] = new Square(boardSquares[move.getSourceIndex()]);
|
|
|
|
|
boardSquares[move.getSourceIndex()] = new Square(Square.EMPTY);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// zero-based index for col and row. Note that -1 and -2 are valid indices into the array, just OUT_OF_BOUNDS.
|
|
|
|
|
public Square getSquare(int col, int row) {
|
|
|
|
|
return boardSquares[12*2 + 2 + col + row * 12];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GameState getStateAfterMove(Move m) {
|
|
|
|
|
//See if it is legal. If not, return null.
|
|
|
|
|
//Create a new object based on this one, update the board state accordingly.
|
|
|
|
|
return null;
|
|
|
|
|
try {
|
|
|
|
|
return new GameState(this, m); //Create a new object based on this one, update the board state accordingly.
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
|
return null; //If it is not a legal move, return null.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ArrayList<Move> getAllPossibleMoves() {
|
|
|
|
@ -49,6 +70,7 @@ public class GameState {
|
|
|
|
|
//Loop through all squares
|
|
|
|
|
for(int index = 0; index < boardSquares.length; index++) {
|
|
|
|
|
int type = boardSquares[index].getType();
|
|
|
|
|
boolean w = boardSquares[index].isWhite();
|
|
|
|
|
if (type == Square.OUT_OF_BOUNDS) continue;
|
|
|
|
|
if (type == Square.EMPTY) continue;
|
|
|
|
|
|
|
|
|
@ -58,6 +80,8 @@ public class GameState {
|
|
|
|
|
if (type == Square.BISHOP) moves.addAll(findAllMovesFor(index, new int[] {13,-13,11,-11}, true));
|
|
|
|
|
if (type == Square.ROOK) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12}, true));
|
|
|
|
|
if (type == Square.KNIGHT) moves.addAll(findAllMovesFor(index, new int[] {10,14,23,25,-10,-14,-23,-25}, false));
|
|
|
|
|
if (type == Square.PAWN && w) moves.addAll(findAllMovesFor(index, new int[] {12}, false));
|
|
|
|
|
if (type == Square.PAWN && !w) moves.addAll(findAllMovesFor(index, new int[] {-12}, false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moves;
|
|
|
|
@ -65,21 +89,28 @@ public class GameState {
|
|
|
|
|
|
|
|
|
|
private ArrayList<Move> findAllMovesFor(int index, int[] directions, boolean canMoveMultipleSquares) {
|
|
|
|
|
ArrayList<Move> moves = new ArrayList<Move>();
|
|
|
|
|
if (boardSquares[index].isWhite() != isWhitesTurn) return moves; //don't move other person's pieces!
|
|
|
|
|
|
|
|
|
|
//loop through the directions, see if they work - return the resulting arraylist.
|
|
|
|
|
for(int dir : directions) {
|
|
|
|
|
int destIndex = index + dir;
|
|
|
|
|
int type = boardSquares[destIndex].getType();
|
|
|
|
|
if (boardSquares[index].isWhite() != isWhitesTurn) continue; //don't move other person's pieces!
|
|
|
|
|
|
|
|
|
|
if (type == Square.OUT_OF_BOUNDS) continue;
|
|
|
|
|
if (type == Square.EMPTY || boardSquares[destIndex].isWhite() != isWhitesTurn) {
|
|
|
|
|
moves.add(new Move(index, destIndex));
|
|
|
|
|
}
|
|
|
|
|
int destIndex = index;
|
|
|
|
|
do {
|
|
|
|
|
destIndex += dir;
|
|
|
|
|
int type = boardSquares[destIndex].getType();
|
|
|
|
|
if (type == Square.OUT_OF_BOUNDS) break;
|
|
|
|
|
if (type == Square.EMPTY || boardSquares[destIndex].isWhite() != isWhitesTurn) {
|
|
|
|
|
moves.add(new Move(index, destIndex));
|
|
|
|
|
}
|
|
|
|
|
} while(boardSquares[destIndex].getType() == Square.EMPTY && canMoveMultipleSquares);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return moves;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isWhitesTurn() {
|
|
|
|
|
return isWhitesTurn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns something like "checkmate", "stalemate", etc.
|
|
|
|
|
public String getFeedback() {
|
|
|
|
|
return null;
|
|
|
|
|