forked from parismav87/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.h
107 lines (94 loc) · 2.82 KB
/
Bot.h
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
#ifndef SRC_BOT_H_
#define SRC_BOT_H_
class Bot
{
private:
int ownMap[10][10];
public:
Bot();
int* getNextMove(int map[], int nextMove[]);
bool isLegalMove(int x1, int y1, int x2, int y2);
bool isLegalH(int x1, int y1, int x2, int y2);
bool isLegalV(int x1, int y1, int x2, int y2);
void fillMap(int map[]);
void printMap();
};
Bot::Bot() //initialise Bot class - constructor
{
}
void Bot::fillMap(int map[]){
for(int i=0; i<100; i++){
ownMap[i/10][i%10] = map[i];
}
}
bool Bot::isLegalMove(int x1, int y1, int x2, int y2){
if((isLegalH(x1,y1,x2,y2) || isLegalV(x1,y1,x2,y2)) && ownMap[x1][y1]!=0 && ownMap[x2][y2]!=0){
return true;
}
return false;
}
bool Bot::isLegalH(int x1, int y1, int x2, int y2){
if((y2>=y1 && y1>1 && ownMap[x2][y2]==ownMap[x1][y1-1] && ownMap[x2][y2]==ownMap[x1][y1-2])
|| (y1>=y2 && y2>1 && ownMap[x1][y1]==ownMap[x2][y2-1] && ownMap[x1][y1]==ownMap[x2][y2-2])
|| (y2>=y1 && y2<8 && ownMap[x1][y1]==ownMap[x2][y2+1] && ownMap[x1][y1]==ownMap[x2][y2+2])
|| (y1>=y2 && y1<8 && ownMap[x2][y2]==ownMap[x1][y1+1] && ownMap[x2][y2]==ownMap[x1][y1+2])
|| (x1!=x2 && y1>0 && y1<9 && ownMap[x2][y2]==ownMap[x1][y1+1] && ownMap[x2][y2]==ownMap[x1][y1-1])
|| (x1!=x2 && y2>0 && y2<9 && ownMap[x1][y1]==ownMap[x2][y2+1] && ownMap[x1][y1]==ownMap[x2][y2-1])){
return true;
} else {
return false;
}
}
bool Bot::isLegalV(int x1, int y1, int x2, int y2){
if((x2>=x1 && x1>1 && ownMap[x2][y2]==ownMap[x1-2][y1] && ownMap[x2][y2]==ownMap[x1-1][y1])
|| (x1>=x2 && x2>1 && ownMap[x1][y1]==ownMap[x2-2][y2] && ownMap[x1][y1]==ownMap[x2-1][y2])
|| (x1>=x2 && x1<8 && ownMap[x2][y2]==ownMap[x1+2][y1] && ownMap[x2][y2]==ownMap[x1+1][y1])
|| (x2>=x1 && x2<8 && ownMap[x1][y1]==ownMap[x2+2][y2] && ownMap[x1][y1]==ownMap[x2+1][y2])
|| (y1!=y2 && x1>0 && x1<9 && ownMap[x2][y2]==ownMap[x1-1][y1] && ownMap[x2][y2]==ownMap[x1+1][y1])
|| (y1!=y2 && x2>0 && x2<9 && ownMap[x1][y1]==ownMap[x2-1][y2] && ownMap[x1][y1]==ownMap[x2+1][y2])){
return true;
} else {
return false;
}
}
int* Bot::getNextMove(int map[], int nextMove[]) //decide and return next move, given the current state of the map.
{
fillMap(map);
for(int x =0; x<10; x++){
for(int y=0; y<10; y++){
if(y!=9 && isLegalMove(x,y,x,y+1)){
nextMove[0] = x;
nextMove[1] = y;
nextMove[2] = x;
nextMove[3] = y+1;
return nextMove;
}
if(x!=9 && isLegalMove(x,y,x+1,y)){
nextMove[0] = x;
nextMove[1] = y;
nextMove[2] = x+1;
nextMove[3] = y;
return nextMove;
}
}
}
nextMove[0] = 1;
nextMove[1] = 2;
nextMove[2] = 3;
nextMove[3] = 4;
return nextMove;
}
void Bot::printMap(){
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
if(j != 9){
cout << ownMap[i][j] << " ";
} else {
cout << ownMap[i][j];
}
}
cout << endl;
}
cout<<endl;
}
#endif /* SRC_BOT_H_ */