forked from tseip/fourinarow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathninarow_heuristic_feature_ut.cpp
105 lines (95 loc) · 4.87 KB
/
ninarow_heuristic_feature_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
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
#include <gtest/gtest.h>
#include "ninarow_board.h"
#include "ninarow_heuristic_feature.h"
using namespace NInARow;
/**
* Tests HeuristicFeature implementation.
*/
TEST(NInARowHeuristicFeatureTest, TestHeuristicFeature) {
using Board = Board<3, 3, 3>;
/*
* Represents the following feature:
* x.x
* .o.
* o.o
* where x is a piece, . is don't care, and o is a space.
* If we are in this position and either o is unoccupied, we have a win.
*/
HeuristicFeature<Board> feature{{0b000000101}, {0b101010000}, 2};
// Player 1 starts forming the feature.
Board board;
board.add({0, 0, 0.0, Player::Player1});
EXPECT_FALSE(feature.contained_in(board, Player::Player1));
EXPECT_FALSE(feature.contained_in(board, Player::Player2));
EXPECT_TRUE(feature.can_be_completed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player2));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player2));
EXPECT_EQ(Board::PatternT{0b000000100},
feature.missing_pieces(board, Player::Player1));
EXPECT_EQ(Board::PatternT{0b000000101},
feature.missing_pieces(board, Player::Player2));
EXPECT_EQ(1, feature.count_pieces(board, Player::Player1));
EXPECT_EQ(0, feature.count_pieces(board, Player::Player2));
EXPECT_EQ(3, feature.count_spaces(board));
// Player 2 encroaches.
board.add({0, 2, 0.0, Player::Player2});
EXPECT_FALSE(feature.contained_in(board, Player::Player1));
EXPECT_FALSE(feature.contained_in(board, Player::Player2));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player2));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player2));
EXPECT_EQ(Board::PatternT{0b000000100},
feature.missing_pieces(board, Player::Player1));
EXPECT_EQ(Board::PatternT{0b000000001},
feature.missing_pieces(board, Player::Player2));
EXPECT_EQ(1, feature.count_pieces(board, Player::Player1));
EXPECT_EQ(1, feature.count_pieces(board, Player::Player2));
EXPECT_EQ(3, feature.count_spaces(board));
// Remove Player 2's encroachment and replace it with Player 1.
board.remove({0, 2, 0.0, Player::Player2});
board.add({0, 1, 0.0, Player::Player2});
board.add({0, 2, 0.0, Player::Player1});
EXPECT_TRUE(feature.contained_in(board, Player::Player1));
EXPECT_FALSE(feature.contained_in(board, Player::Player2));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player2));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player2));
EXPECT_EQ(Board::PatternT{0}, feature.missing_pieces(board, Player::Player1));
EXPECT_EQ(Board::PatternT{0b000000101},
feature.missing_pieces(board, Player::Player2));
EXPECT_EQ(2, feature.count_pieces(board, Player::Player1));
EXPECT_EQ(0, feature.count_pieces(board, Player::Player2));
EXPECT_EQ(3, feature.count_spaces(board));
// Cover a space in the pattern from player 2. can_be_removed should now
// become true.
board.add({2, 0, 0.0, Player::Player2});
EXPECT_TRUE(feature.contained_in(board, Player::Player1));
EXPECT_FALSE(feature.contained_in(board, Player::Player2));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player2));
EXPECT_TRUE(feature.can_be_removed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player2));
EXPECT_EQ(Board::PatternT{0}, feature.missing_pieces(board, Player::Player1));
EXPECT_EQ(Board::PatternT{0b000000101},
feature.missing_pieces(board, Player::Player2));
EXPECT_EQ(2, feature.count_pieces(board, Player::Player1));
EXPECT_EQ(0, feature.count_pieces(board, Player::Player2));
EXPECT_EQ(2, feature.count_spaces(board));
// Cover another space. Now the feature should be inactive.
board.add({1, 1, 0.0, Player::Player1});
EXPECT_FALSE(feature.contained_in(board, Player::Player1));
EXPECT_FALSE(feature.contained_in(board, Player::Player2));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_completed(board, Player::Player2));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player1));
EXPECT_FALSE(feature.can_be_removed(board, Player::Player2));
EXPECT_EQ(Board::PatternT{0}, feature.missing_pieces(board, Player::Player1));
EXPECT_EQ(Board::PatternT{0b000000101},
feature.missing_pieces(board, Player::Player2));
EXPECT_EQ(2, feature.count_pieces(board, Player::Player1));
EXPECT_EQ(0, feature.count_pieces(board, Player::Player2));
EXPECT_EQ(1, feature.count_spaces(board));
}