|
|
|
@ -47,6 +47,14 @@ public class GameState {
|
|
|
|
|
boardSquares[move.getDestIndex()] = new Square(boardSquares[move.getSourceIndex()]);
|
|
|
|
|
boardSquares[move.getSourceIndex()] = new Square(Square.EMPTY);
|
|
|
|
|
|
|
|
|
|
//Queening (no underpromotion for now)
|
|
|
|
|
if ((move.getDestRow() == 0 || move.getDestRow() == 7) && boardSquares[move.getDestIndex()].getType() == Square.PAWN) {
|
|
|
|
|
boolean w = boardSquares[move.getDestIndex()].isWhite();
|
|
|
|
|
boardSquares[move.getDestIndex()] = new Square(Square.QUEEN, w); //Make that pawn a queen
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boardSquares[move.getDestIndex()].setHasMoved(); //For knowledge about moving pawns two squares and castling.
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -71,37 +79,50 @@ public class GameState {
|
|
|
|
|
for(int index = 0; index < boardSquares.length; index++) {
|
|
|
|
|
int type = boardSquares[index].getType();
|
|
|
|
|
boolean w = boardSquares[index].isWhite();
|
|
|
|
|
boolean pieceMoved = boardSquares[index].isMoved();
|
|
|
|
|
if (type == Square.OUT_OF_BOUNDS) continue;
|
|
|
|
|
if (type == Square.EMPTY) continue;
|
|
|
|
|
|
|
|
|
|
//For each square, find all possible places that piece can move to.
|
|
|
|
|
if (type == Square.KING) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, false));
|
|
|
|
|
if (type == Square.QUEEN) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, true));
|
|
|
|
|
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));
|
|
|
|
|
if (type == Square.KING) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, 1, true, true));
|
|
|
|
|
if (type == Square.QUEEN) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, 99, true, true));
|
|
|
|
|
if (type == Square.BISHOP) moves.addAll(findAllMovesFor(index, new int[] {13,-13,11,-11}, 99, true, true));
|
|
|
|
|
if (type == Square.ROOK) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12}, 99, true, true));
|
|
|
|
|
if (type == Square.KNIGHT) moves.addAll(findAllMovesFor(index, new int[] {10,14,23,25,-10,-14,-23,-25}, 1, true, true));
|
|
|
|
|
|
|
|
|
|
//white
|
|
|
|
|
if (type == Square.PAWN && w && pieceMoved) moves.addAll(findAllMovesFor(index, new int[] {12}, 1, true, false));
|
|
|
|
|
if (type == Square.PAWN && w && !pieceMoved) moves.addAll(findAllMovesFor(index, new int[] {12}, 2, true, false));
|
|
|
|
|
if (type == Square.PAWN && w) moves.addAll(findAllMovesFor(index, new int[] {11, 13}, 1, false, true));
|
|
|
|
|
|
|
|
|
|
//black
|
|
|
|
|
if (type == Square.PAWN && !w && pieceMoved) moves.addAll(findAllMovesFor(index, new int[] {-12}, 1, true, false));
|
|
|
|
|
if (type == Square.PAWN && !w && !pieceMoved) moves.addAll(findAllMovesFor(index, new int[] {-12}, 2, true, false));
|
|
|
|
|
if (type == Square.PAWN && !w) moves.addAll(findAllMovesFor(index, new int[] {-11, -13}, 1, false, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moves;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<Move> findAllMovesFor(int index, int[] directions, boolean canMoveMultipleSquares) {
|
|
|
|
|
///movementDistance is the number of times that piece can move in the given directions.
|
|
|
|
|
private ArrayList<Move> findAllMovesFor(int index, int[] directions, int movementDistance, boolean canMove, boolean canCapture) {
|
|
|
|
|
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;
|
|
|
|
|
int numMoves = movementDistance;
|
|
|
|
|
do {
|
|
|
|
|
destIndex += dir;
|
|
|
|
|
int type = boardSquares[destIndex].getType();
|
|
|
|
|
if (type == Square.OUT_OF_BOUNDS) break;
|
|
|
|
|
if (type == Square.EMPTY || boardSquares[destIndex].isWhite() != isWhitesTurn) {
|
|
|
|
|
if (type == Square.EMPTY && canMove ||
|
|
|
|
|
type != Square.EMPTY && type != Square.OUT_OF_BOUNDS && boardSquares[destIndex].isWhite() != isWhitesTurn && canCapture) {
|
|
|
|
|
moves.add(new Move(index, destIndex));
|
|
|
|
|
}
|
|
|
|
|
} while(boardSquares[destIndex].getType() == Square.EMPTY && canMoveMultipleSquares);
|
|
|
|
|
numMoves--;
|
|
|
|
|
} while(boardSquares[destIndex].getType() == Square.EMPTY && numMoves > 0);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return moves;
|
|
|
|
@ -111,8 +132,8 @@ public class GameState {
|
|
|
|
|
return isWhitesTurn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns something like "checkmate", "stalemate", etc.
|
|
|
|
|
// TODO: should also include information regarding check, checkmate, and stalemate.
|
|
|
|
|
public String getFeedback() {
|
|
|
|
|
return null;
|
|
|
|
|
return isWhitesTurn ? "White's turn" : "Black's turn";
|
|
|
|
|
}
|
|
|
|
|
}
|