-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheuristics.cpp
More file actions
152 lines (122 loc) · 3.38 KB
/
heuristics.cpp
File metadata and controls
152 lines (122 loc) · 3.38 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
/**************************************************************/
// Heuristic
#include "heuristics.h"
std::string row_as_string(const TBoard& board, int row)
{
std::string out("");
for (int j = 0; j < 10; j++)
out += board[row][j];
return out;
}
std::string column_as_string(const TBoard& board, int column)
{
std::string out("");
for (int i = 0; i < 10; i++)
out += board[i][column];
return out;
}
std::string diag_nw_se_as_string(const TBoard& board, int diag)
{
std::string out("");
for (int j = 0; j < 10; j++)
if ((diag + j - 9 < 10) && (diag + j - 9 >= 0))
out += board[j][j + diag - 9];
return out;
}
std::string diag_ne_sw_as_string(const TBoard& board, int diag)
{
std::string out("");
for (int j = 0; j < 10; j++)
if ((diag - j < 10) && (diag - j >= 0))
out += board[j][diag - j];
return out;
}
int evalstring(const std::string& line, char mark)
{
const char* win;
const char* almostwin;
const char* threat1;
const char* threat2;
const char* threat3;
const char* good1;
const char* init1;
if (mark == 'X')
{
win = "XXXXX";
almostwin = "_XXXX_";
threat1 = "_XXX_";
threat2 = "_XX_X_";
threat3 = "_X_XX_";
good1 = "_XX_";
init1 = "___X___";
}
else
{
win = "OOOOO";
almostwin = "_OOOO_";
threat1 = "_OOO_";
threat2 = "_OO_O_";
threat3 = "_O_OO_";
good1 = "_OO_";
init1 = "___O___";
}
int score = 0;
if (line.find(win) != std::string::npos)
{
score += 100;
}
else if (line.find(almostwin) != std::string::npos)
{
score += 75;
}
else if (line.find(threat1) != std::string::npos)
{
score += 20;
}
else if (line.find(threat2) != std::string::npos)
{
score += 20;
}
else if (line.find(threat3) != std::string::npos)
{
score += 20;
}
else if (line.find(good1) != std::string::npos)
{
score += 5;
}
else if (line.find(init1) != std::string::npos)
{
score += 1;
}
return score;
}
int eval(const TBoard& board, char mark)
{
int score = 0;
// look for good structures horizontally
for (int i = 0; i < 10; i++)
{
score += evalstring(row_as_string(board, i), mark);
}
// look for good structures vertically
for (int j = 0; j < 10; j++)
{
score += evalstring(column_as_string(board, j), mark);
}
// look for good structures diagonally
for (int k = 0; k < 19; k++)
{
score += evalstring(diag_nw_se_as_string(board, k), mark);
}
// look for good structures diagonally
for (int k = 0; k < 19; k++)
{
score += evalstring(diag_ne_sw_as_string(board, k), mark);
}
return score;
}
int eval(const TBoard& board)
{
return eval(board, 'X') - eval(board, 'O');
}