Example #1
0
  private List<Square> validMovesForKnight(Square square) {
    List<Square> validMoves = new ArrayList<Square>();

    // Up two, right one. Up two, left one.
    // Down two, right one. Down two, left one.
    // Right two, up one. Right two, down one.
    // Left two, up one. Left two, down one.

    int rowColPairs[][] = {
      {-2, 1}, {-2, -1},
      {2, 1}, {2, -1},
      {-1, 2}, {1, 2},
      {-1, -2}, {1, -2}
    };

    for (int i = 0; i < rowColPairs.length; i++) {
      int row = square.Row + rowColPairs[i][0];
      int col = square.Col + rowColPairs[i][1];
      if (rowAndColAreWithinBounds(row, col)) {
        Square s = Board[row][col];
        if (!s.isOccupiedByPieceOfColor(square.getPiece().Color)) validMoves.add(s);
      }
    }

    return validMoves;
  }
Example #2
0
  private List<Square> validMovesForSquareInDirection(
      Square square, int length, int up, int right, int down, int left) {

    List<Square> validMoves = new ArrayList<Square>();
    for (int i = 1; i <= length; i++) {
      if (up != 0) up = i;
      if (down != 0) down = i;
      if (right != 0) right = i;
      if (left != 0) left = i;

      int row = square.Row - up + down;
      int col = square.Col + right - left;

      if (!rowAndColAreWithinBounds(row, col)) break;
      Square s = Board[row][col];

      if (s.isOccupied()) {
        if (!s.isOccupiedByPieceOfColor(square.Piece.Color)) validMoves.add(s);
        break;
      } else {
        validMoves.add(s);
      }
    }
    return validMoves;
  }
Example #3
0
  public void move(Square fromSquare, Square toSquare) {
    Piece pieceToMove = fromSquare.empty();
    Piece pieceToRemove = toSquare.empty();

    if (pieceToMove == null) throw new IllegalArgumentException("No piece at " + fromSquare);

    addPieceAndSquareToPieceLocations(pieceToMove, toSquare);
    removePieceAndSquareFromPieceLocations(pieceToMove, fromSquare);
    removePieceAndSquareFromPieceLocations(pieceToRemove, toSquare);
    toSquare.setPiece(pieceToMove);
  }
Example #4
0
 private void removePieceAndSquareFromPieceLocations(Piece piece, Square squareToRemove) {
   if (piece == null) return;
   HashMap<Piece, ArrayList<Square>> PieceLocations =
       piece.isWhite() ? WhitePieceLocations : BlackPieceLocations;
   ArrayList<Square> squares = PieceLocations.get(piece);
   if (squares == null) return;
   Iterator<Square> i = squares.iterator();
   while (i.hasNext()) {
     Square s = i.next();
     if (s.equals(squareToRemove)) {
       i.remove();
       break;
     }
   }
 }
Example #5
0
  public boolean isValidMoveForColor(String from, String to, String color) {
    // TODO: castling, en passant
    if (from.equals(to)) return false;

    Square fromSquare = getSquareAtCoordinates(from);
    if (fromSquare == null) return false;

    Square toSquare = getSquareAtCoordinates(to);
    if (toSquare == null) return false;

    if (!fromSquare.isOccupiedByPieceOfColor(color)) return false;
    if (toSquare.isOccupiedByPieceOfColor(color)) return false;

    if (validMovesForPieceOnSquare(fromSquare).contains(toSquare)) return true;
    else return false;
  }
Example #6
0
 public String toString() {
   String board = "";
   String horizontalLabel = "  A B C D E F G H\n";
   for (int i = 0; i < 8; i++) {
     if (i == 0) board += horizontalLabel;
     for (int j = 0; j < 8; j++) {
       if (j == 0) board += Integer.toString(8 - i);
       Square square = Board[i][j];
       board += "|";
       if (square.isOccupiedByPieceOfColor("W")) board += "\033[31;1m";
       board += square.toString();
       if (square.isOccupiedByPieceOfColor("W")) board += "\033[0m";
       if (j == 7) board += "|" + Integer.toString(8 - i) + "\n";
     }
     if (i == 7) board += horizontalLabel;
   }
   return board;
 }
Example #7
0
  public boolean isColorInCheck(String color) {
    boolean colorIsWhite = color.equals("W");
    String attackingColor = colorIsWhite ? "B" : "W";
    HashMap<Piece, ArrayList<Square>> EnemyPieceLocations =
        colorIsWhite ? BlackPieceLocations : WhitePieceLocations;

    for (Map.Entry<Piece, ArrayList<Square>> entry : EnemyPieceLocations.entrySet()) {
      Piece piece = entry.getKey();
      ArrayList<Square> squares = entry.getValue();

      for (Square s : squares) {
        for (Square move : validMovesForPieceOnSquareExCheck(s))
          if (move.containsKingOfColor(color)) return true;
      }
    }

    return false;
  }
Example #8
0
  private List<Square> validMovesForPawn(Square square) {
    List<Square> validMoves = new ArrayList<Square>();

    // Regular moves, 1 or 2 up or down depending on color
    Piece piece = square.getPiece();
    if (piece.isWhite()) {
      int movesUp = piece.movesUp();
      if (square.containsPawnOnStartingRow()) movesUp += 1;
      validMoves.addAll(validMovesForSquareInDirection(square, movesUp, 1, 0, 0, 0));
    } else {
      int movesDown = piece.movesDown();
      if (square.containsPawnOnStartingRow()) movesDown += 1;
      validMoves.addAll(validMovesForSquareInDirection(square, movesDown, 0, 0, 1, 0));
    }

    // Attacking move
    int rowColPairs[][] = {{-1, -1}, {-1, 1}};
    for (int i = 0; i < rowColPairs.length; i++) {
      int row = square.Row + rowColPairs[i][0];
      int col = square.Col + rowColPairs[i][1];
      if (rowAndColAreWithinBounds(row, col)) {
        Square s = Board[row][col];
        if (s.isOccupiedByPieceOfColor(square.getPiece().oppositeColor())) validMoves.add(s);
      }
    }

    return validMoves;
  }
Example #9
0
  public List<Square> validMovesForPieceOnSquare(Square square) {
    Piece piece = square.getPiece();
    if (piece == null) return null;

    List<Square> validMovesForSquare = validMovesForPieceOnSquareExCheck(square);
    Iterator<Square> i = validMovesForSquare.iterator();
    while (i.hasNext()) {
      Square s = i.next();
      this.move(square.location(), s.location());
      if (this.isColorInCheck(piece.Color)) i.remove();
      this.move(s.location(), square.location());
    }
    return validMovesForSquare;
  }
Example #10
0
  private List<Square> validMovesForPieceOnSquareExCheck(Square square) {
    Piece piece = square.getPiece();
    List<Square> validMoves = new ArrayList<Square>();

    // Special moves the piece is a pawn or a knight
    if (piece.canMoveLikePawn()) return validMovesForPawn(square);
    if (piece.canMoveLikeKnight()) return validMovesForKnight(square);

    // Add all normal directional moves
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesUp(), 1, 0, 0, 0));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesSide(), 0, 1, 0, 0));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesDown(), 0, 0, 1, 0));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesSide(), 0, 0, 0, 1));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesDiagonal(), 1, 1, 0, 0));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesDiagonal(), 1, 0, 0, 1));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesDiagonal(), 0, 1, 1, 0));
    validMoves.addAll(validMovesForSquareInDirection(square, piece.movesDiagonal(), 0, 0, 1, 1));

    return validMoves;
  }