@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()); }
/** * 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; }
/** * 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())); } } }
/** * Method removes squares where this king can't move. * * @param squares squares what king threats * @param danger squares where king can't move * @param mySquares * @param remove squares that needs to remove */ private void removeSquares( SquareList squares, SquareList danger, SquareList mySquares, SquareList remove) { for (Square square : squares.getList()) { if (danger.contains(square) || mySquares.contains(square)) { remove.add(square); } } for (Square square : remove.getList()) { squares.remove(square); } }
@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); }
@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()); }