Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dervism committed May 19, 2024
1 parent 33dd590 commit 2cdf970
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 41 deletions.
8 changes: 7 additions & 1 deletion src/main/java/no/dervis/terminal_aichess/board/Chess.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.dervis.terminal_aichess.board;

import no.dervis.terminal_aichess.moves.Move;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -36,7 +38,7 @@ enum MoveType {
case bishop -> " ♗ ";
case rook -> " ♖ ";
case queen -> " ♕ ";
case king -> " ♔ ";
case king -> " ♔ ";
case bpawn -> " ♟ ";
case bknight -> " ♞ ";
case bbishop -> " ♝ ";
Expand All @@ -47,6 +49,10 @@ enum MoveType {
default -> throw new IllegalStateException(STR."Unexpected value: \{pieceType}");
};

BiFunction<Board.Tuple3, Board.Tuple3, Integer> moveMaker = Move::createMove;
BiFunction<Board.Tuple3, Board.Tuple3, Function<MoveType, Integer>> moveTypeMaker
= (from, to) -> type -> Move.createMove(from.index(), to.index(), type.ordinal());

BiFunction<Bitboard, Boolean, StringBuilder> boardToStr = (board, reverse) -> {
StringBuilder builder = new StringBuilder();
IntStream.range(0, 8)
Expand Down
56 changes: 21 additions & 35 deletions src/main/java/no/dervis/terminal_aichess/moves/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,36 @@ public record Move(
int moveType,
int promotionPiece) {

public static Move createMove(
int piece,
int color,
public static int createMove(
int fromSquare,
int toSquare
) {
return new Move(
piece,
color,
fromSquare,
toSquare,
Chess.MoveType.NORMAL.ordinal(),
0);
}

public static Move createWhiteMove(
int piece,
int fromSquare,
int toSquare
int toSquare,
int moveType
) {
return new Move(
piece,
Chess.white,
fromSquare,
toSquare,
Chess.MoveType.NORMAL.ordinal(),
0);
return createMove(fromSquare, toSquare, moveType, 0);
}

public static Move createMove(
int piece,
int color,
public static int createMove(
int fromSquare,
int toSquare,
int moveType,
int promotionPiece
) {
return new Move(
piece,
color,
fromSquare,
toSquare,
moveType,
promotionPiece);
return (fromSquare << 14) |
(toSquare << 7) |
(moveType << 4) |
promotionPiece;
}

public static int createMove(Move move) {
int fromSquare = move.fromSquare;
int toSquare = move.toSquare;
int moveType = move.moveType;
int promotionPiece = move.promotionPiece;

return (fromSquare << 14) |
(toSquare << 7) |
(moveType << 4) |
promotionPiece;
}

public static Move createMove(int move, Bitboard board){
Expand Down
139 changes: 134 additions & 5 deletions src/test/java/no/dervis/terminal_aichess/moves/CheckHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -16,15 +15,36 @@ class CheckHelperTest implements Board, Chess {

private final boolean reverse = false;

BiFunction<Tuple3, Tuple3, Integer> moveMaker = (from, to) -> (from.index() << 14) | (to.index() << 7);
@Test
void isSquareAttackedCastling() {
Bitboard board = new Bitboard();
board.initialiseBoard();
System.out.println(boardToStr.apply(board, true));

List<Integer> moves = Arrays.asList(
moveMaker.apply(e2, e4),
moveMaker.apply(e7, e5),
moveMaker.apply(f1, c4),
moveMaker.apply(d7, d5),
moveMaker.apply(g1, f3),
moveMaker.apply(f8, b4),
moveTypeMaker.apply(e1, g1).apply(MoveType.CASTLE_KING_SIDE)
);

moves.forEach(board::makeMove);

System.out.println(boardToStr.apply(board, true));

CheckHelper helper = new CheckHelper(board);
assertFalse(helper.isSquareAttackedBySlidingPiece(e1.index(), black));
}

@Test
void isSquareAttackedByOneBishops() {
Bitboard board = new Bitboard();
board.initialiseBoard();
System.out.println(boardToStr.apply(board, true));

// Set up the board so that the white king is attacked by two black bishops
List<Integer> moves = Arrays.asList(
moveMaker.apply(f8, b4),
moveMaker.apply(d2, d4)
Expand All @@ -47,7 +67,6 @@ void isSquareAttackedByTwoBishops() {
board.initialiseBoard();
System.out.println(boardToStr.apply(board, true));

// Set up the board so that the white king is attacked by two black bishops
List<Integer> moves = Arrays.asList(
moveMaker.apply(f8, b4),
moveMaker.apply(c8, h4),
Expand Down Expand Up @@ -227,4 +246,114 @@ void isSquareAttackedByCenterPawn() {
assertFalse(checkHelper.isSquareAttackedByPawn(d5.index(), black));
assertTrue(checkHelper.isSquareAttackedByPawn(d5.index(), white));
}
}

@Test
void isSquareAttackedBySlidingPieceTest1() {
Bitboard board = new Bitboard();
board.setPiece(rook, white, e4.index());
board.setPiece(king, black, e2.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(e2.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest2() {
Bitboard board = new Bitboard();
board.setPiece(rook, white, e4.index());
board.setPiece(king, black, e7.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(e7.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest3() {
Bitboard board = new Bitboard();
board.setPiece(rook, white, a1.index());
board.setPiece(bishop, black, e5.index());
board.setPiece(king, black, h8.index());
System.out.println(boardToStr.apply(board, true));

CheckHelper helper = new CheckHelper(board);
assertFalse(helper.isSquareAttackedBySlidingPiece(h8.index(), white));
}
@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase1() {
Bitboard board = new Bitboard();
board.setPiece(rook, white, e4.index());
board.setPiece(king, black, a7.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertFalse(helper.isSquareAttackedBySlidingPiece(a7.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase2() {
Bitboard board = new Bitboard();
board.setPiece(bishop, white, e4.index());
board.setPiece(king, black, c6.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(c6.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase3() {
Bitboard board = new Bitboard();
board.setPiece(bishop, white, g2.index());
board.setPiece(queen, black, e4.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(e4.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase4() {
Bitboard board = new Bitboard();
board.setPiece(bishop, white, d4.index());
board.setPiece(king, black, a7.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(a7.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase5() {
Bitboard board = new Bitboard();
board.setPiece(rook, white, h1.index());
board.setPiece(king, black, h7.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(h7.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase6() {
Bitboard board = new Bitboard();
board.setPiece(bishop, white, f6.index());
board.setPiece(king, black, h8.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(h8.index(), white));
}

@Test
void isSquareAttackedBySlidingPieceTest_AdditionalCase7() {
Bitboard board = new Bitboard();
board.setPiece(queen, black, d4.index());
board.setPiece(king, white, a1.index());
System.out.println(boardToStr.apply(board, reverse));

CheckHelper helper = new CheckHelper(board);
assertTrue(helper.isSquareAttackedBySlidingPiece(a1.index(), black));
}
}

0 comments on commit 2cdf970

Please sign in to comment.