forked from tseip/fourinarow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathninarow_heuristic_ut.cpp
54 lines (43 loc) · 1.55 KB
/
ninarow_heuristic_ut.cpp
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
#include <gtest/gtest.h>
#include "fourbynine_features.h"
#include "ninarow_bfs.h"
#include "ninarow_board.h"
#include "ninarow_heuristic.h"
using namespace NInARow;
TEST(NInARowHeuristicTest, TestHeuristicRandomMoves) {
using Board = Board<4, 9, 4>;
auto heuristic = Heuristic<Board>::create();
heuristic->seed_generator(0);
Board b;
EXPECT_EQ(heuristic->evaluate(b), 0);
std::size_t moves_remaining = Board::get_max_num_moves();
while (moves_remaining-- != 0) {
b = b + heuristic->get_random_move(b);
EXPECT_EQ(heuristic->get_moves(b, Player::Player1).size(), moves_remaining);
}
EXPECT_EQ(b.num_pieces(), Board::get_max_num_moves());
}
TEST(NInARowHeuristicTest, TestHeuristicGetBestMoveBFS) {
using Board = Board<4, 9, 4>;
auto heuristic = Heuristic<Board>::create();
heuristic->seed_generator(10);
Board b;
Player player = Player::Player1;
std::size_t moves_remaining = Board::get_max_num_moves();
while (moves_remaining-- != 0) {
auto moves = heuristic->get_moves(b, player);
std::sort(moves.begin(), moves.end(), [](const auto& m1, const auto& m2) {
return m1.board_position < m2.board_position;
});
auto bfs = std::make_shared<NInARowBestFirstSearch<Heuristic<Board>>>(
heuristic, b);
bfs->complete_search();
auto move = heuristic->get_best_move(bfs->get_tree());
b = b + move;
player = get_other_player(player);
if (b.player_has_won(Player::Player1) ||
b.player_has_won(Player::Player2)) {
break;
}
}
}