Skip to content

Commit 0e79279

Browse files
committed
Evaluator is to evaluate the score of each situation
1 parent d157b14 commit 0e79279

File tree

1 file changed

+378
-0
lines changed

1 file changed

+378
-0
lines changed

evaluator.cpp

Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <cstring>
5+
#include <string.h>
6+
#include <set>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
#define AI 1
12+
#define MAN 2
13+
#define MAXVALUE 999999
14+
#define MINVALUE -999999
15+
#define DEPTH 4
16+
#define OFFSET 1
17+
#define RATIO 1
18+
19+
//four directions
20+
int direction[4][2] = {{0, 1}, {1, 0}, {1, -1}, {1, 1}};
21+
22+
//struct of (row, col)
23+
struct Position
24+
{
25+
Position(int row, int col){
26+
this->row = row;
27+
this->col = col;
28+
}
29+
bool operator==(const Position& b)const{
30+
return this->row == b.row && this->col == b.col;
31+
}
32+
bool operator<(const Position& b)const{
33+
if(this->row == b.row){
34+
return this->col < b.col;
35+
}
36+
else{
37+
return this->row < b.row;
38+
}
39+
}
40+
int row, col;
41+
};
42+
43+
//type of chess and its value
44+
struct ModelShape{
45+
string shape;
46+
int value;
47+
};
48+
49+
//the table saved all types of chess
50+
ModelShape modelTable[20];
51+
int modelCnt = 0;
52+
53+
//the chessboard
54+
char chessboard[20][20];
55+
56+
//the answer AI gives
57+
int predictRow, predictCol;
58+
59+
//to mark if the position was calculated
60+
int chessNumOnDrct[20][20][4];
61+
62+
//print the chessboard
63+
void print(){
64+
printf(" ");
65+
for(int i = 1; i <= 15; i++){
66+
printf("%-4d", i);
67+
}
68+
cout << "\n-----------------------------------------------------------------\n";
69+
for(int i = 1; i <= 15; i++){
70+
printf("%2d|", i);
71+
for(int j = 1; j <= 15; j++){
72+
if(chessboard[i][j] == AI){
73+
cout << "+ ";
74+
}
75+
else if(chessboard[i][j] == MAN){
76+
cout << "- ";
77+
}
78+
else{
79+
cout << " ";
80+
}
81+
}
82+
cout << " |" << "\n\n";
83+
}
84+
cout << "-----------------------------------------------------------------\n";
85+
}
86+
87+
//to sort types of chess by their values
88+
bool compare(ModelShape a, ModelShape b){
89+
return a.value > b.value;
90+
}
91+
92+
//initiate the model
93+
void initModel(int nth, string model, int value){
94+
modelTable[nth].value = value;
95+
modelTable[nth].shape = model;
96+
}
97+
98+
//initiate the table of models
99+
void initTable(){
100+
initModel(modelCnt++, "ooooo", 900000);
101+
initModel(modelCnt++, "+oooo+", 50000);
102+
initModel(modelCnt++, "++ooo++", 10000);
103+
initModel(modelCnt++, "+ooo++", 6500);
104+
initModel(modelCnt++, "++ooo+", 6500);
105+
initModel(modelCnt++, "oooo+", 6000);
106+
initModel(modelCnt++, "+oooo", 6000);
107+
initModel(modelCnt++, "+++oo+++", 1000);
108+
initModel(modelCnt++, "+++oo++", 500);
109+
initModel(modelCnt++, "++oo+++", 500);
110+
initModel(modelCnt++, "++oo++", 200);
111+
initModel(modelCnt++, "oo", 100);
112+
initModel(modelCnt++, "++++o++++", 50);
113+
sort(modelTable, modelTable + modelCnt, compare);
114+
}
115+
116+
int getScore(string shape){
117+
for (int i = 0; i < modelCnt; i++){
118+
if(shape.find(modelTable[i].shape) != string::npos){
119+
return modelTable[i].value;
120+
}
121+
}
122+
123+
return 0;
124+
}
125+
126+
bool inBoundary(int row, int col){
127+
return row >= 1 && row <= 15 && col >= 1 && col <= 15;
128+
}
129+
130+
vector<Position> pieceOnBoard;
131+
132+
int evaluator(){
133+
int totalScore = 0;
134+
memset(chessNumOnDrct, 0, sizeof(chessNumOnDrct));
135+
136+
for(vector<Position>::iterator iter = pieceOnBoard.begin(); iter != pieceOnBoard.end(); iter++){
137+
int row = iter->row;
138+
int col = iter->col;
139+
140+
int player = chessboard[row][col];
141+
int enemy = AI + MAN - player;
142+
143+
for (int drct = 0; drct < 4; drct++){
144+
if(chessNumOnDrct[row][col][drct] == -1){
145+
continue;
146+
}
147+
148+
string shape;
149+
int i = row, j = col;
150+
while(inBoundary(i, j) && chessboard[i][j] != enemy){
151+
i -= direction[drct][0];
152+
j -= direction[drct][1];
153+
}
154+
i += direction[drct][0];
155+
j += direction[drct][1];
156+
while(inBoundary(i, j) && chessboard[i][j] != enemy){
157+
if(chessboard[i][j] == 0){
158+
shape.append("+");
159+
}
160+
else if(chessboard[i][j] != 0){
161+
shape.append("o");
162+
chessNumOnDrct[i][j][drct] = -1;
163+
}
164+
i += direction[drct][0];
165+
j += direction[drct][1];
166+
}
167+
168+
if(chessboard[row][col] == AI){
169+
totalScore += getScore(shape) * RATIO;
170+
}
171+
else{
172+
totalScore -= getScore(shape);
173+
}
174+
}
175+
}
176+
177+
return totalScore;
178+
}
179+
180+
bool isOver(){
181+
memset(chessNumOnDrct, 0, sizeof(chessNumOnDrct));
182+
183+
for(vector<Position>::iterator iter = pieceOnBoard.begin(); iter != pieceOnBoard.end(); iter++){
184+
int row = iter->row;
185+
int col = iter->col;
186+
187+
int player = chessboard[row][col];
188+
int enemy = AI + MAN - player;
189+
190+
for (int drct = 0; drct < 4; drct++){
191+
if(chessNumOnDrct[row][col][drct] == -1){
192+
continue;
193+
}
194+
195+
int count = 1; //itself is the one
196+
197+
string shape;
198+
int i = row, j = col;
199+
while(inBoundary(i, j) && chessboard[i][j] == player){
200+
count++;
201+
i -= direction[drct][0];
202+
j -= direction[drct][1];
203+
}
204+
count--;
205+
206+
i = row;
207+
j = col;
208+
while(inBoundary(i, j) && chessboard[i][j] == player){
209+
count++;
210+
chessNumOnDrct[i][j][drct] = -1;
211+
i += direction[drct][0];
212+
j += direction[drct][1];
213+
}
214+
count--;
215+
216+
if(count >= 5){
217+
return true;
218+
}
219+
}
220+
}
221+
222+
return false;
223+
}
224+
225+
void setPiece(int row, int col, int player){
226+
chessboard[row][col] = player;
227+
pieceOnBoard.push_back(Position(row, col));
228+
}
229+
230+
void delPiece(int row, int col){
231+
chessboard[row][col] = 0;
232+
pieceOnBoard.pop_back();
233+
}
234+
235+
236+
237+
238+
239+
240+
241+
void test1(){
242+
for (int i = 4; i <= 7; i++){
243+
setPiece(i + 3, i, AI);
244+
}
245+
for (int i = 7; i <= 10; i++){
246+
setPiece(15 - i, i, MAN);
247+
}
248+
setPiece(9, 7, MAN);
249+
setPiece(8, 8, AI);
250+
setPiece(9, 8, AI);
251+
cout << evaluator() << endl;
252+
cout << isOver() << endl;
253+
254+
cout << "\n\n\n\n\n";
255+
256+
setPiece(4, 11, MAN);
257+
cout << evaluator() << endl;
258+
}
259+
260+
void test2(){
261+
for (int i = 11; i >= 8; i--){
262+
setPiece(i, 10, AI);
263+
}
264+
setPiece(12, 10, MAN);
265+
setPiece(9, 5, AI);
266+
setPiece(10, 6, AI);
267+
for (int i = 6; i <= 9; i++){
268+
setPiece(9, i, MAN);
269+
}
270+
setPiece(8, 6, MAN);
271+
setPiece(8, 7, MAN);
272+
setPiece(8, 8, AI);
273+
setPiece(8, 9, AI);
274+
setPiece(7, 7, AI);
275+
setPiece(7, 8, AI);
276+
setPiece(7, 10, MAN);
277+
setPiece(6, 6, MAN);
278+
setPiece(6, 7, MAN);
279+
cout << evaluator() << endl;
280+
281+
cout << "\n\n\n\n\n";
282+
283+
setPiece(8, 4, AI);
284+
setPiece(7, 6, MAN);
285+
setPiece(5, 6, AI);
286+
setPiece(6, 5, MAN);
287+
cout << evaluator() << endl;
288+
289+
cout << "\n\n\n\n\n";
290+
291+
// setPiece(7, 6, AI);
292+
// setPiece(7, 5, MAN);
293+
// setPiece(10, 8, AI);
294+
// setPiece(6, 4, MAN);
295+
// cout << evaluator() << endl;
296+
297+
cout << "\n\n\n\n\n";
298+
}
299+
300+
int main(){
301+
initTable();
302+
test2();
303+
304+
getchar();
305+
return 0;
306+
}
307+
308+
/*
309+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
310+
-----------------------------------------------------------------
311+
1| |
312+
313+
2| |
314+
315+
3| |
316+
317+
4| |
318+
319+
5| - |
320+
321+
6| - |
322+
323+
7| + - |
324+
325+
8| + - + |
326+
327+
9| + - + |
328+
329+
10| + |
330+
331+
11| |
332+
333+
12| |
334+
335+
13| |
336+
337+
14| |
338+
339+
15| |
340+
341+
-----------------------------------------------------------------
342+
*/
343+
344+
/*
345+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
346+
-----------------------------------------------------------------
347+
1| |
348+
349+
2| |
350+
351+
3| |
352+
353+
4| |
354+
355+
5| |
356+
357+
6| - - |
358+
359+
7| + + - |
360+
361+
8| - - + + + |
362+
363+
9| + - - - - + |
364+
365+
10| + + |
366+
367+
11| + |
368+
369+
12| - |
370+
371+
13| |
372+
373+
14| |
374+
375+
15| |
376+
377+
-----------------------------------------------------------------
378+
*/

0 commit comments

Comments
 (0)