-
Notifications
You must be signed in to change notification settings - Fork 5
/
Evaluate.h
252 lines (171 loc) · 6.42 KB
/
Evaluate.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "Constants.h"
int evaluate(int board[][boardSize], int playerColor, int currentColor);
int getPieceValue(int board[][boardSize], int row, int col, int currentColor);
int getOppositeColor(int color);
int getValidMoveCount(int board[][boardSize], int row, int col);
// Remember draw is impossible in this game ..........
// Complexity of evaluate function : O(boardSize * boardSize) .........
bool whiteWonGame = false;
bool blackWonGame = false;
/*bool unsafe(int row,int col,int player)
{
if(player==white)
{
if((col-1>=0&&board[row+1][col-1]==nill&&board[row][col]==nill)) danger=true;
else if((col-1)>=0&&(col+1<boardSize)&&(board[row+1][col]==nill)&&board[row][col-1]==nill&&board[row][col+1]==nill) danger=true;
else if(col==0&&)
}
}
int howMany(int player)
{
int cnt=0;
if(player==white)
{
for(int i=0;i<12;i++)
if(unsafe(0,i,player)) cnt++;
}
return cnt;
}*/
int evaluate(int board[][boardSize], int playerColor, int currentColor){
int boardVal = 0;
whiteWonGame = false;
blackWonGame = false;
int remainingWhitePieces = 0;
int remainingBlackPieces = 0;
// Our Code is playing as white ........
if(playerColor == white){
// For white, home row will be 0 and goal row will be 11 ......
// j reprsents column and i repsent row .......
for(int j = 0 ; j < boardSize ; j ++){
bool blackColumnHole = true, whiteColumnHole = true;
for(int i = 0 ; i < boardSize ; i ++){
if(board[i][j] == white){
remainingWhitePieces ++;
whiteColumnHole = false;
boardVal += getPieceValue(board,i,j,currentColor);
if(i == boardSize - 1) whiteWonGame = true;
}
else if(board[i][j] == black){
remainingBlackPieces ++;
blackColumnHole = false;
boardVal -= getPieceValue(board,i,j,currentColor);
if(i == 0) blackWonGame = true;
}
}
if(whiteColumnHole) boardVal += pieceColumnHoleValue;
if(blackColumnHole) boardVal -= pieceColumnHoleValue;
}
if(remainingBlackPieces == 0) whiteWonGame = true;
if(remainingBlackPieces == 0) blackWonGame = true;
if(whiteWonGame) boardVal += winValue;
if(blackWonGame) boardVal -= winValue;
}
// Our Code is playing as black .......
else{
// For black home row will be 11 and goal row will be 0 .......
for(int j = 0 ; j < boardSize ; j ++){
bool blackColumnHole = true, whiteColumnHole = true;
for(int i = 0 ; i < boardSize ; i ++){
if(board[i][j] == white){
remainingWhitePieces ++;
whiteColumnHole = false;
boardVal -= getPieceValue(board,i,j,currentColor);
if(i == boardSize - 1) whiteWonGame = true;
}
else if(board[i][j] == black){
remainingBlackPieces ++;
blackColumnHole = false;
boardVal += getPieceValue(board,i,j,currentColor);
if(i == 0) blackWonGame = true;
}
}
if(whiteColumnHole) boardVal -= pieceColumnHoleValue;
if(blackColumnHole) boardVal += pieceColumnHoleValue;
}
if(remainingBlackPieces == 0) whiteWonGame = true;
if(remainingWhitePieces == 0) blackWonGame = true;
if(whiteWonGame) boardVal -= winValue;
if(blackWonGame) boardVal += winValue;
}
return boardVal;
}
// Think about get piece value function rest all will follow up ..................
int getPieceValue(int board[][boardSize], int row, int col, int currentColor){
int pieceVal = pieceValue;
int pieceColor = board[row][col];
// Adding row worth value .......
if(pieceColor == white)
pieceVal += rowWorthValue[row];
else
pieceVal += rowWorthValue[boardSize - row - 1];
// Adding vertical connection value .......
bool verticalConnected = false;
if(row - 1 >= 0 && board[row][col] == board[row -1][col]) verticalConnected = true;
if(row + 1 < boardSize && board[row][col] == board[row + 1][col]) verticalConnected = true;
if(verticalConnected) pieceVal += pieceVerticalConnectionValue;
// Adding horizontal connection value .......
bool horizontalConnected = false;
if(col - 1 >= 0 && board[row][col] == board[row][col - 1]) horizontalConnected = true;
if(col + 1 < boardSize && board[row][col] == board[row][col + 1]) horizontalConnected = true;
if(horizontalConnected) pieceVal += pieceHorizontalConnectionValue;
int val = 1;
if(pieceColor == currentColor) val = -1;
bool dangerA = false, dangerB = false;
// Right now considering just three moves one forward and two diagonally forward .......
int mDir;
if(pieceColor == white){
if(row == 0)
pieceVal += pieceHomeGroundValue[col];
if(row == 1)
pieceVal += pieceSecondGroundValue[col];
mDir = 1;
}
else if(pieceColor == black){
if(row == boardSize - 1)
pieceVal += pieceHomeGroundValue[col];
if(row == boardSize-2)
pieceVal += pieceSecondGroundValue[col];
mDir = -1;
}
if(row + mDir >= 0 && row + mDir < boardSize){
if(col - 1 >= 0 && pieceColor == board[row + mDir][col - 1]) pieceVal += pieceProtectionValue;
if(col + 1 < boardSize && pieceColor == board[row + mDir][col + 1]) pieceVal += pieceProtectionValue;
if(col - 1 >= 0 && pieceColor == getOppositeColor(board[row + mDir][col -1])){
pieceVal += val*pieceAttackValue;
if(pieceColor == white && row == boardSize - 2 && val == -1) dangerA = true;
if(pieceColor == black && row == 1 && val == -1) dangerA = true;
}
if(col + 1 < boardSize && pieceColor == getOppositeColor(board[row + mDir][col + 1])){
pieceVal += val*pieceAttackValue;
if(pieceColor == white && row == boardSize - 2 && val == -1) dangerB = true;
if(pieceColor == black && row == 1 && val == -1) dangerB = true;
}
if(pieceColor == board[row + mDir][col]){
pieceVal += pieceProtectionValue;
}
}
int validMoveCount = getValidMoveCount(board,row,col);
pieceVal += pieceMobilityValue * validMoveCount;
if(!dangerA && !dangerB)
pieceVal += pieceAlmostWinValue;
if(col == 0 || col == boardSize - 1)
pieceVal += pieceEdgeValue;
return pieceVal;
}
int getOppositeColor(int color){
if(color == black) return white;
if(color == white) return black;
if(color == nill) return nill;
}
int getValidMoveCount(int board[][boardSize], int row, int col){
int pieceColor = board[row][col];
int mDir = 1;
if(pieceColor == black) mDir = -1;
int validMove = 0;
if(row + mDir >= 0 && row + mDir < boardSize){
if(col - 1 >= 0 && board[row + mDir][col - 1] != pieceColor) validMove ++;
if(col + 1 < boardSize && board[row + mDir][col + 1] != pieceColor) validMove ++;
if(board[row + mDir][col] == nill) validMove ++;
}
return validMove;
}