Example #1
0
 @Test
 public void basicMovesTest() {
   this.board.getSquare('e', 4).setPiece(wRook);
   this.board.getSquare('d', 5).setPiece(bRook);
   SquareList wSquares = wRook.whereToMove(board, board.getSquare('e', 4));
   boolean ok = true;
   ArrayList<String> squares = new ArrayList();
   squares.add("e5");
   squares.add("e3");
   squares.add("a4");
   squares.add("b4");
   squares.add("c4");
   squares.add("d4");
   squares.add("h4");
   squares.add("g4");
   squares.add("f4");
   squares.add("e7");
   squares.add("e6");
   for (Square square : wSquares.getList()) {
     String s = square.toString();
     if (!squares.contains(s)) {
       ok = false;
     }
   }
   if (squares.size() != wSquares.size()) {
     ok = false;
   }
   assertEquals(true, ok);
   assertEquals(0, bRook.whereToMove(board, board.getSquare('h', 8)).size());
 }
Example #2
0
  @Test
  public void threatTest() {
    Square square = this.board.getSquare('e', 1);
    square.setPiece(wPawn);
    SquareList squares = square.getPiece().whatThreats(board, square);
    SquareList list = new SquareList();
    list.add(this.board.getSquare('d', 2));
    list.add(this.board.getSquare('f', 2));
    boolean ok = true;
    for (Square sqr : list.getList()) {
      if (!squares.contains(sqr)) {
        ok = false;
      }
    }
    assertEquals(true, squares.size() == list.size());

    square = this.board.getSquare('e', 8);
    square.setPiece(bPawn);
    squares = square.getPiece().whatThreats(board, square);
    list = new SquareList();
    list.add(this.board.getSquare('d', 7));
    list.add(this.board.getSquare('f', 7));
    for (Square sqr : list.getList()) {
      if (!squares.contains(sqr)) {
        ok = false;
      }
    }
    assertEquals(true, ok);
    assertEquals(true, squares.size() == list.size());
  }
Example #3
0
 /**
  * Method adds squares where to castle
  *
  * @param board current chess board
  * @param from current square
  * @param squares
  */
 private void addCastle(Board board, Square from, SquareList squares) {
   if (this.notMoved() && board.checkThreats(this.getColor())) {
     if (checkShort(board, from)) {
       squares.add(board.getSquare('g', from.getN()));
     }
     if (checkLong(board, from)) {
       squares.add(board.getSquare('c', from.getN()));
     }
   }
 }
Example #4
0
 /**
  * Method checks is it possible to castle long side.
  *
  * @param board is the current chess board.
  * @param from where the king is now.
  * @return true if it's possible.
  */
 private boolean checkLong(Board board, Square from) {
   int n = from.getN();
   if (board.getSquare('a', n).getPiece() != null
       && board.getSquare('a', n).getPiece().toString().equals("R")) {
     SquareList needToCheck;
     boolean isOK;
     if (this.getColor() == 'w') {
       needToCheck = board.getBlackThreats();
     } else {
       needToCheck = board.getWhiteThreats();
     }
     if (!needToCheck.contains(board.getSquare('d', n))
         && !needToCheck.contains(board.getSquare('c', n))
         && board.getSquare('d', n).getPiece() == null
         && board.getSquare('c', n).getPiece() == null) {
       isOK = true;
     } else {
       isOK = false;
     }
     if (board.getSquare('a', n).getPiece().notMoved() && isOK) {
       return true;
     }
   }
   return false;
 }
Example #5
0
 /**
  * Method returns all the squares where this king can move.
  *
  * @param board is the current chess board.
  * @param from where it's now.
  * @return list of squares where this king can move.
  */
 @Override
 public SquareList whereToMove(Board board, Square from) {
   SquareList squares = this.whatThreats(board, from);
   from.setPiece(null);
   board.updateInfo();
   SquareList remove = new SquareList();
   SquareList danger;
   SquareList mySquares;
   if (this.getColor() == 'w') {
     danger = board.getBlackThreats();
     mySquares = board.getWhites();
   } else {
     danger = board.getWhiteThreats();
     mySquares = board.getBlacks();
   }
   removeSquares(squares, danger, mySquares, remove);
   addCastle(board, from, squares);
   from.setPiece(this);
   return squares;
 }
Example #6
0
 @Test
 public void sideMoves() {
   this.board.move('d', 2, 'd', 4);
   this.board.move('e', 7, 'e', 5);
   boolean ok = true;
   String s1 = "e5";
   String s2 = "d5";
   for (Square square : wPawn.whereToMove(board, board.getSquare('d', 4)).getList()) {
     if (!square.toString().equals(s1) && !square.toString().equals(s2)) {
       ok = false;
     }
   }
   this.board.move('f', 2, 'f', 4);
   s1 = "e4";
   s2 = "d4";
   String s3 = "f4";
   for (Square square : bPawn.whereToMove(board, board.getSquare('e', 5)).getList()) {
     if (!square.toString().equals(s1)
         && !square.toString().equals(s2)
         && !square.toString().equals(s3)) {
       ok = false;
     }
   }
   assertEquals(true, ok);
 }
Example #7
0
 @Test
 public void FirstPawnMovesTest() {
   SquareList wSquares = wPawn.whereToMove(board, board.getSquare('e', 2));
   SquareList bSquares = bPawn.whereToMove(board, board.getSquare('e', 7));
   boolean ok = true;
   if (wSquares.size() != 2 || bSquares.size() != 2) {
     ok = false;
   }
   String s1 = "e3";
   String s2 = "e4";
   for (Square square : wSquares.getList()) {
     if (!square.toString().equals(s1) && !square.toString().equals(s2)) {
       ok = false;
     }
   }
   s1 = "e6";
   s2 = "e5";
   for (Square square : bSquares.getList()) {
     if (!square.toString().equals(s1) && !square.toString().equals(s2)) {
       ok = false;
     }
   }
   assertEquals(true, ok);
 }