-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
163 lines (141 loc) · 4.27 KB
/
enemy.cpp
File metadata and controls
163 lines (141 loc) · 4.27 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
#include "board.hpp"
#include "enemy.hpp"
#include "level.hpp"
#include "random.hpp"
namespace roadagain
{
Enemy::Enemy(Level level) : level_(level)
{
}
Enemy::~Enemy()
{
}
Point Enemy::select(const Board* board, const CellColor& stone) const
{
switch (level_){
case EASY:
return (randomized_select(board, stone));
case MEDIUM:
return (maximized_select(board, stone));
default:
return (evaluated_select(board, stone));
}
}
const int Enemy::SCORE_TABLE[Board::ROW][Board::COL] = {
{ 50, -20, 20, 15, 15, 20, -20, 50 },
{ -20, -20, 20, -10, -10, 20, -20, -20 },
{ 20, 20, 20, 0, 0, 20, 20, 20 },
{ 15, -10, 0, 15, 15, 0, -10, 15 },
{ 15, -10, 0, 15, 15, 0, -10, 15 },
{ 20, 20, 20, 0, 0, 20, 20, 20 },
{ -20, -20, 20, -10, -10, 20, -20, -20 },
{ 50, -20, 20, 15, 15, 20, -20, 50 },
};
Point Enemy::randomized_select(const Board* board, const CellColor& stone) const
{
int n = random();
Cell c(stone);
do {
c.point.y++;
if (c.point.y >= Board::ROW){
c.point.y = 0;
c.point.x = (c.point.x + 1) % Board::COL;
}
while (not board->can_put(c)){
c.point.y++;
if (c.point.y >= Board::ROW){
c.point.y = 0;
c.point.x = (c.point.x + 1) % Board::COL;
}
}
n--;
} while (n > 0);
return (c.point);
}
Point Enemy::maximized_select(const Board* board, const CellColor& stone) const
{
Point p;
int maximum = 0;
for (int i = 0; i < Board::ROW; i++){
for (int j = 0; j < Board::COL; j++){
Cell c(i, j, stone);
if (board->can_put(c)){
int tmp = board->reverse_num(c);
if (maximum < tmp){
maximum = tmp;
p = c.point;
}
else if (maximum == tmp && random() % 2 == 0){
p = c.point;
}
}
}
}
return (p);
}
Point Enemy::evaluated_select(const Board* board, const CellColor& stone, int depth) const
{
if (depth >= MAX_DEPTH){
return Point(-1, -1);
}
Point p;
int maximum = MIN_EVALUTE_VALUE;
for (int i = 0; i < Board::ROW; i++){
for (int j = 0; j < Board::COL; j++){
Cell c(i, j, stone);
if (board->can_put(c)){
Board t_board(*board);
int tmp = reverse_score(&t_board, c, depth);
if (maximum < tmp){
maximum = tmp;
p = c.point;
}
else if (maximum == tmp && random() % 2 == 0){
p = c.point;
}
}
}
}
return (p);
}
int Enemy::reverse_score(Board* board, const Cell& cell, int depth) const
{
int score = SCORE_TABLE[cell.point.y][cell.point.x] + board->count_neighbor(cell.point, cell.color.reversed()) * 3;
board->put(cell, false);
for (const Point& d : Board::D){
score += reverse_score(board, cell, d);
}
bool player_put = false;
for (int i = 0; i < Board::ROW; i++){
for (int j = 0; j < Board::COL; j++){
if (board->can_put(Cell(i, j, cell.color.reversed()))){
score -= 3;
player_put = true;
}
}
}
if (not player_put){
score += 50;
Point player_point = evaluated_select(board, cell.color, depth + 1);
if (player_point.y != -1 && player_point.x != -1){
score += reverse_score(board, Cell(player_point, cell.color), depth + 1);
}
}
else {
Point player_point = evaluated_select(board, cell.color.reversed(), depth + 1);
if (player_point.y != -1 && player_point.x != -1){
score -= reverse_score(board, Cell(player_point, cell.color.reversed()), depth + 1);
}
}
return (score);
}
int Enemy::reverse_score(const Board* board, const Cell& cell, const Point& d) const
{
int score = 0;
int num = board->reverse_num(cell, d);
for (int i = 1; i < num; i++){
score += SCORE_TABLE[cell.point.y + d.y * i][cell.point.x + d.x * i];
}
return (score - num * 3);
}
} // namespace roadagain