-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (184 loc) · 5.94 KB
/
Copy pathmain.cpp
File metadata and controls
209 lines (184 loc) · 5.94 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
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
#include <iostream>
#include <map>
using namespace std;
#include "thc.cpp"
using namespace thc;
const float MAX_EVAL = numeric_limits<float>::max();
const float MIN_EVAL = numeric_limits<float>::min();
const int DEPTH_SEARCH = 2;
ChessRules BOARD_NULL;
Move bestMove;
void display_position(ChessRules &cr, const string &description){
string fen = cr.ForsythPublish();
string s = cr.ToDebugStr();
//printf( "%s\n", description.c_str() );
//printf( "FEN (Forsyth Edwards Notation) = %s\n", fen.c_str() );
printf( "Position = %s\n", s.c_str() );
}
const map<char, int> whitePieceValue = {
{'P', 1},
{'R', 5},
{'N', 3},
{'B', 3},
{'Q', 9},
{'K', 0},
};
const map<char, int> blackPieceValue = {
{'p', 1},
{'r', 5},
{'n', 3},
{'b', 3},
{'q', 9},
{'k', 0}
};
float evaluatePosition(ChessRules &board){
string fen = board.ForsythPublish();
int whiteMaterial = 0, blackMaterial = 0;
for(char cur : fen){
if(cur == ' '){
break;
}
else if(cur == '/' || (1<=cur-'0' && cur-'0'<=9)){
continue;
}
else if(whitePieceValue.find(cur) != whitePieceValue.end()){
whiteMaterial += whitePieceValue.at(cur);
}
else if(blackPieceValue.find(cur) != blackPieceValue.end()){
blackMaterial += blackPieceValue.at(cur);
}
else{
cout << "ERROR IN EVALUATION FUNCTION" << endl;
}
}
return whiteMaterial - blackMaterial;
}
float dfs(ChessRules &board, int height, float alpha, float beta, bool alphaTurn){ //Return moves as well
TERMINAL check_position_terminal;
bool legal = board.Evaluate(check_position_terminal);
assert(legal == true);
if(check_position_terminal!=NOT_TERMINAL){ // Game is Over
if(check_position_terminal == TERMINAL_WCHECKMATE){
return MAX_EVAL;
}
else if(check_position_terminal == TERMINAL_BCHECKMATE){
return MIN_EVAL;
}
else if(check_position_terminal == TERMINAL_WSTALEMATE
|| check_position_terminal == TERMINAL_BSTALEMATE){
return 0;
}
else{
cout << "ERROR: GAME ENDED BUT NOT CHECKMATES OR STALEMATE" << endl;
}
}
else if(height == 0){ // At Leaf Node
return evaluatePosition(board);
}
else{ // Game Continues
vector<Move> moves;
board.GenLegalMoveList(moves);
if(alphaTurn){ // White's turn
float maxEval = MIN_EVAL;
Move maxMove = moves[0];
for(Move move : moves){
board.PlayMove(move);
float eval = dfs(board, height - 1, alpha, beta, false);
board.PopMove(move);
if(maxEval < eval){
maxEval = eval;
swap(maxMove, move);
}
alpha = max(alpha, eval);
if(alpha >= beta){
break;
}
}
if(height == DEPTH_SEARCH){
bestMove = maxMove;
}
return maxEval;
}
else{ // Black's turn
float minEval = MAX_EVAL;
Move minMove = moves[0];
for(Move move : moves){
board.PlayMove(move);
float eval = dfs(board, height - 1, alpha, beta, true);
board.PopMove(move);
if(minEval > eval){
minEval = eval;
swap(minMove, move);
}
beta = min(beta, eval);
if(beta <= alpha){
break;
}
}
if(height == DEPTH_SEARCH){
bestMove = minMove;
}
return minEval;
}
}
}
void play_OneComputerOneHuman(ChessRules &board, bool whiteFlag){
TERMINAL check_position_terminal;
bool legal = board.Evaluate(check_position_terminal);
assert(legal == true);
int i = 0;
while (check_position_terminal == NOT_TERMINAL) {
display_position(board, "Position");
cout << ++i << ". " << (board.WhiteToPlay() ? "[WHITE] " : "[BLACK] ");
if(whiteFlag==board.WhiteToPlay()){ // Bot's Turn
float eval = dfs(board, 2, MIN_EVAL, MAX_EVAL, whiteFlag);
cout << "BOT PLAYS: " << bestMove.NaturalOut(&board) << endl << endl;
}
else { // Player's Move
string san;
cout << "Enter Move: ";
vector<Move> moves;
board.GenLegalMoveList(moves);
while (true) {
cin >> san;
cout << endl;
try {
bestMove.NaturalIn(&board, san.c_str());
bool moveIsLegal = false;
for (Move move : moves){
if(bestMove == move){
moveIsLegal = true;
break;
}
}
if (!moveIsLegal) {
throw invalid_argument("");
}
}
catch (invalid_argument){
cout << "Invalid Move, Try Again..." << endl;
continue;
}
break;
}
}
board.PlayMove(bestMove);
}
cout << "Game Over!" << endl;
if(check_position_terminal == TERMINAL_WCHECKMATE){
cout << "WHITE WINS!" << endl;
}
else if(check_position_terminal == TERMINAL_BCHECKMATE){
cout << "BLACK WINS!" << endl;
}
else if(check_position_terminal == TERMINAL_WSTALEMATE
|| check_position_terminal == TERMINAL_BSTALEMATE){
cout << "STALEMATE!" << endl;
}
}
int main() {
//display_position(BOARD_NULL, "INITIAL POSITION");
//cout << BOARD_NULL.ForsythPublish();
ChessRules cr;
play_OneComputerOneHuman(cr, true);
}