forked from obryanlouis/4pchess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer_test.cc
More file actions
180 lines (153 loc) · 5.78 KB
/
player_test.cc
File metadata and controls
180 lines (153 loc) · 5.78 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "gmock/gmock.h"
#include <chrono>
#include <gtest/gtest.h>
#include <unordered_map>
#include <vector>
#include "utils.h"
#include "board.h"
#include "player.h"
namespace chess {
using ::testing::UnorderedElementsAre;
const Player kRedPlayer = Player(RED);
const Player kBluePlayer = Player(BLUE);
const Player kYellowPlayer = Player(YELLOW);
const Player kGreenPlayer = Player(GREEN);
TEST(PlayerTest, EvaluateCheckmate) {
// Team is in checkmate already
AlphaBetaPlayer player;
std::unordered_map<BoardLocation, Piece> location_to_piece = {
{BoardLocation(3, 0), Piece(kRedPlayer, KING)},
{BoardLocation(5, 2), Piece(kBluePlayer, KING)},
{BoardLocation(7, 7), Piece(kYellowPlayer, KING)},
{BoardLocation(9, 9), Piece(kGreenPlayer, KING)},
{BoardLocation(4, 1), Piece(kGreenPlayer, QUEEN)},
};
Board board(kRedPlayer, location_to_piece);
const auto& res = player.MakeMove(board);
ASSERT_TRUE(res.has_value());
float valuation = std::get<0>(*res);
const auto& move_or = std::get<1>(*res);
EXPECT_FALSE(move_or.has_value());
EXPECT_EQ(valuation, -kMateValue);
}
TEST(PlayerTest, EvaluateCheckmateNextMove) {
// Team is in checkmate next move
PlayerOptions options;
AlphaBetaPlayer player(options);
auto board = ParseBoardFromFEN("R-0,0,0,0-1,1,1,1-1,1,1,1-0,0,0,0-0-x,x,x,bK,7,x,x,x/x,x,x,7,rQ,x,x,x/x,x,x,2,yK,5,x,x,x/14/14/14/13,gK/14/14/14/14/x,x,x,8,x,x,x/x,x,x,8,x,x,x/x,x,x,4,rK,3,x,x,x");
ASSERT_NE(board, nullptr);
const auto& res = player.MakeMove(*board, std::nullopt, 2);
ASSERT_TRUE(res.has_value());
float valuation = std::get<0>(*res);
const auto& move_or = std::get<1>(*res);
EXPECT_TRUE(move_or.has_value());
EXPECT_EQ(*move_or, Move(BoardLocation(1, 10), BoardLocation(1, 4)));
EXPECT_EQ(valuation, kMateValue);
}
TEST(PlayerTest, EvaluateStalemate) {
// Team is in stalemate
AlphaBetaPlayer player;
std::unordered_map<BoardLocation, Piece> location_to_piece = {
{BoardLocation(3, 0), Piece(kRedPlayer, KING)},
{BoardLocation(5, 2), Piece(kBluePlayer, KING)},
{BoardLocation(7, 7), Piece(kYellowPlayer, KING)},
{BoardLocation(9, 9), Piece(kGreenPlayer, KING)},
{BoardLocation(4, 2), Piece(kGreenPlayer, QUEEN)},
};
Board board(kRedPlayer, location_to_piece);
const auto& res = player.MakeMove(board, std::nullopt, 1);
ASSERT_TRUE(res.has_value());
float valuation = std::get<0>(*res);
const auto& move_or = std::get<1>(*res);
EXPECT_FALSE(move_or.has_value());
EXPECT_EQ(valuation, 0);
}
TEST(PlayerTest, EvaluatePieceImbalance) {
// Unequal pieces with a clear winning side
AlphaBetaPlayer player;
std::unordered_map<BoardLocation, Piece> location_to_piece = {
{BoardLocation(3, 0), Piece(kBluePlayer, KING)},
{BoardLocation(5, 2), Piece(kRedPlayer, KING)},
{BoardLocation(7, 7), Piece(kGreenPlayer, KING)},
{BoardLocation(9, 9), Piece(kYellowPlayer, KING)},
{BoardLocation(4, 8), Piece(kYellowPlayer, QUEEN)},
};
Board board(kRedPlayer, location_to_piece);
const auto& res = player.MakeMove(board, std::nullopt, 1);
ASSERT_TRUE(res.has_value());
float valuation = std::get<0>(*res);
const auto& move_or = std::get<1>(*res);
EXPECT_TRUE(move_or.has_value());
EXPECT_GT(valuation, 0);
}
TEST(PlayerTest, EvaluateCheckmateForcedCheckmate1) {
AlphaBetaPlayer player;
auto board = Board::CreateStandardSetup();
board->MakeMove(Move(BoardLocation(12, 7), BoardLocation(11, 7)));
board->MakeMove(Move(BoardLocation(3, 1), BoardLocation(3, 2)));
board->MakeMove(Move(BoardLocation(1, 8), BoardLocation(2, 8)));
board->MakeMove(Move(BoardLocation(3, 12), BoardLocation(3, 11)));
const auto& res = player.MakeMove(*board);
ASSERT_TRUE(res.has_value());
float valuation = std::get<0>(*res);
EXPECT_EQ(valuation, kMateValue);
const auto& move_or = std::get<1>(*res);
ASSERT_TRUE(move_or.has_value());
EXPECT_EQ(*move_or, Move(BoardLocation(13, 8), BoardLocation(11, 6)));
}
TEST(PlayerTest, CheckPVInfoProducesValidMoves) {
AlphaBetaPlayer player;
auto board = Board::CreateStandardSetup();
board->MakeMove(Move(BoardLocation(12, 7), BoardLocation(11, 7)));
board->MakeMove(Move(BoardLocation(7, 1), BoardLocation(7, 2)));
board->MakeMove(Move(BoardLocation(1, 6), BoardLocation(2, 6)));
board->MakeMove(Move(BoardLocation(6, 12), BoardLocation(6, 11)));
constexpr int kDepth = 4;
const auto& res = player.MakeMove(*board, std::nullopt, kDepth);
ASSERT_TRUE(res.has_value());
const auto& move_or = std::get<1>(*res);
ASSERT_TRUE(move_or.has_value());
const PVInfo* pvinfo = &player.GetPVInfo();
int num_pvmoves = 0;
while (pvinfo != nullptr) {
const auto& pvmove_or = pvinfo->GetBestMove();
if (pvmove_or.has_value()) {
num_pvmoves++;
const auto& move = *pvmove_or;
Move moves[300];
size_t num_moves = board->GetPseudoLegalMoves2(moves, 300);
bool found = false;
for (size_t i = 0; i < num_moves; i++) {
if (moves[i] == move) {
found = true;
break;
}
}
ASSERT_TRUE(found);
board->MakeMove(move);
}
pvinfo = pvinfo->GetChild().get();
}
EXPECT_GE(num_pvmoves, kDepth);
}
//TEST(PlayerTest, StaticExchangeEvaluation) {
// PlayerOptions options;
// AlphaBetaPlayer player(options);
//
// auto board = Board::CreateStandardSetup();
//
// board->MakeMove(Move(BoardLocation(12, 7), BoardLocation(11, 7)));
// board->MakeMove(Move(BoardLocation(7, 1), BoardLocation(7, 2)));
// board->MakeMove(Move(BoardLocation(1, 6), BoardLocation(2, 6)));
// board->MakeMove(Move(BoardLocation(6, 12), BoardLocation(6, 11)));
//
// // Qg1 x m7
// BoardLocation from(13, 6);
// BoardLocation to(7, 12);
// Move move(from, to, board->GetPiece(to));
//
// int see = player.StaticExchangeEvaluationCapture(*board, move);
//
// EXPECT_LT(see, 0);
//}
} // namespace chess