~macaptain/lkdo-chess

ref: 4a145c47f8fbfa347527f38f5105792fa96cc9e7 lkdo-chess/test/attack.zig -rw-r--r-- 4.2 KiB
4a145c47Michael Captain Add king attacks with tests 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
usingnamespace @import("attack.zig");

const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "white pawn attacks two squares in front" {
    const side = Side.White;
    const pawn = Square.e2;
    const expected_attacked_squares = [2]Square{ Square.d3, Square.f3 };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn attacks two squares in front" {
    const side = Side.Black;
    const pawn = Square.c7;
    const expected_attacked_squares = [2]Square{ Square.b6, Square.d6 };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on a file attacks only one square" {
    const side = Side.White;
    const pawn = Square.a4;
    const expected = bitboardFromSquare(Square.b5);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on h file attacks only one square" {
    const side = Side.White;
    const pawn = Square.h7;
    const expected = bitboardFromSquare(Square.g8);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn on a file attacks only one square" {
    const side = Side.Black;
    const pawn = Square.a8;
    const expected = bitboardFromSquare(Square.b7);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on 8th rank attacks nothing" {
    const side = Side.White;
    const pawn = Square.f8;
    const expected = @as(u64, 0);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn on 1st rank attacks nothing" {
    const side = Side.Black;
    const pawn = Square.g1;
    const expected = @as(u64, 0);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "knight in center attacks all 8 squares" {
    const knight = Square.e4;
    const expected_attacked_squares = [8]Square{
        Square.f6,
        Square.g5,
        Square.g3,
        Square.f2,
        Square.d2,
        Square.c3,
        Square.c5,
        Square.d6,
    };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = knightAttacks(knight);
    expectEqual(expected, actual);
}

test "knights in corners attack only 2 squares" {
    const knight_a8 = Square.a8;
    const knight_h8 = Square.h8;
    const knight_h1 = Square.h1;
    const knight_a1 = Square.a1;

    const expected_a8 = [2]Square{ Square.c7, Square.b6 };
    const expected_h8 = [2]Square{ Square.g6, Square.f7 };
    const expected_h1 = [2]Square{ Square.f2, Square.g3 };
    const expected_a1 = [2]Square{ Square.b3, Square.c2 };

    expectEqual(bitboardFromSquares(expected_a8[0..]), knightAttacks(knight_a8));
    expectEqual(bitboardFromSquares(expected_h8[0..]), knightAttacks(knight_h8));
    expectEqual(bitboardFromSquares(expected_h1[0..]), knightAttacks(knight_h1));
    expectEqual(bitboardFromSquares(expected_a1[0..]), knightAttacks(knight_a1));
}

test "king in center attacks all 8 squares" {
    const king = Square.d4;
    const expected_attacked_squares = [8]Square{
        Square.d5,
        Square.e5,
        Square.e4,
        Square.e3,
        Square.d3,
        Square.c3,
        Square.c4,
        Square.c5,
    };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = kingAttacks(king);
    expectEqual(expected, actual);
}

test "kings in corners attack only 3 squares" {
    const king_a8 = Square.a8;
    const king_h8 = Square.h8;
    const king_h1 = Square.h1;
    const king_a1 = Square.a1;

    const expected_a8 = [3]Square{ Square.b8, Square.b7, Square.a7 };
    const expected_h8 = [3]Square{ Square.h7, Square.g7, Square.g8 };
    const expected_h1 = [3]Square{ Square.g1, Square.g2, Square.h2 };
    const expected_a1 = [3]Square{ Square.a2, Square.b2, Square.b1 };

    expectEqual(bitboardFromSquares(expected_a8[0..]), kingAttacks(king_a8));
    expectEqual(bitboardFromSquares(expected_h8[0..]), kingAttacks(king_h8));
    expectEqual(bitboardFromSquares(expected_h1[0..]), kingAttacks(king_h1));
    expectEqual(bitboardFromSquares(expected_a1[0..]), kingAttacks(king_a1));
}