-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.cpp
executable file
·225 lines (186 loc) · 5.1 KB
/
engine.cpp
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
#include "engine.h"
#include "data.h"
#include "util.h"
#include <string>
#include <vector>
#include <Windows.h>
#include <sstream>
using namespace std;
engine::engine( string FEN )
{
Position.LoadFEN( FEN );
lout.open("Inot_Log.txt");
}
engine::engine()
{
Position.LoadFEN( "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" );
lout.open("Inot_Log.txt");
}
void engine::ProcessCommand( string Cmd )
{
static bool Force = false;
if( Cmd == "" ) return;
lout << Cmd << endl;
//tokenize the command into arguments
Cmd += ' ';
vector<string> Args;
int First = -1;
int Next = 0;
bool InQuote = false;
for(int i=0; i<Cmd.size(); i++){
if( Cmd[i] == '"' ){
InQuote = !InQuote;
continue;
}
if( Cmd[i] == ' ' && !InQuote ){
Next = i;
string Temp = Cmd.substr( First+1, Next-First-1 );
First = Next;
Args.push_back( Temp );
}
}
//process a move
if( Args[ 0 ].size() == 4 || Args[ 0 ].size() == 5 ){
if( Args[ 0 ][ 0 ] >= 'a' && Args[ 0 ][ 0 ] <= 'h' &&
Args[ 0 ][ 1 ] >= '1' && Args[ 0 ][ 1 ] <= '8' &&
Args[ 0 ][ 2 ] >= 'a' && Args[ 0 ][ 2 ] <= 'h' &&
Args[ 0 ][ 3 ] >= '0' && Args[ 0 ][ 3 ] <= '8' )
{
lout << "Enemy Move: " << Args[0] << endl;
int From = (Args[ 0 ][ 0 ]-'a') + (8-(Args[ 0 ][ 1 ]-'0'))*8;
int To = (Args[ 0 ][ 2 ]-'a') + (8-(Args[ 0 ][ 3 ]-'0'))*8;
int Promotion = -1;
if( Args[ 0 ].size() == 5 ){
if( Args[ 0 ][ 4 ] == 'q' ) Promotion = QUEEN;
else if( Args[ 0 ][ 4 ] == 'r' ) Promotion = ROOK;
else if( Args[ 0 ][ 4 ] == 'n' ) Promotion = KNIGHT;
else if( Args[ 0 ][ 4 ] == 'b' ) Promotion = BISHOP;
lout << "Promotion: " << Promotion << endl;
}
bool LegalMove = MakeMove( From, To, OtherColor[ CompColor ], Promotion );
if( !LegalMove ){
//4kbnr/1P2pppp/3p4/8/8/8/P3PPPP/RNBQKBNR w KQk - 0 1
lout << "Illegal move" << endl;
cout << "Illegal move" << endl;
Showbitboard( Position.GetAllPieces() );
Position.Show();
return;
}
if( !Force ) StartSearch();
}
}
//try to find the command
if( Args[ 0 ] == "go" ){
Force = false;
StartSearch();
}
else if( Args[ 0 ] == "force" ){
Force = true;
}
else if( Args[ 0 ] == "white" ){
CompColor = WHITE;
}
else if( Args[ 0 ] == "black" ){
CompColor = BLACK;
}
else if( Args[ 0 ] == "remove" ){
TerminatePonder();
Position.TakeBack();
Position.TakeBack();
}
else if( Args[ 0 ] == "new" ){
TerminatePonder();
Position.LoadFEN( StartingPositionFEN );
lout << "NEW: " << StartingPositionFEN << endl;
}
else if( Args[ 0 ] == "sd" ){
}
else if( Args[ 0 ] == "st" ){
}
else if( Args[ 0 ] == "pause" ){
TerminatePonder();
}
else if( Args[ 0 ] == "level" ){
int NumMoves = atoi( Args[ 1 ].c_str() );
if( NumMoves == 0 ) NumMoves = 60;
SearchTime = ((atoi( Args[ 2 ].c_str() ) * 60) * 1000) / NumMoves;
}
else if( Args[ 0 ] == "setboard" ){
if( Args.size()>1 )
Position.LoadFEN( Args[ 1 ] );
}
else if( Args[ 0 ] == "xboard" ){
cout.setf( ios::unitbuf );
cout << "done=0\n";
//cout << "feature ping=0\n";
cout << "feature setboard=1\n";
//cout << "feature playother=1\n";
//cout << "feature san=0\n";
//cout << "feature usermove=0\n";
cout << "feature time=1\n";
//cout << "feature draw=0\n";
//cout << "feature sigint=0\n";
//cout << "feature sigterm=0\n";
//cout << "feature reuse=1\n";
//cout << "feature analyze=0\n";
cout << "feature myname=\"Inot2\"\n";
//cout << "feature variants=\"\"\n";
cout << "feature color=0\n";
//cout << "feature ics=0\n";
cout << "feature name=1\n";
cout << "feature pause=0\n";
cout << "done=1\n";
cout.flush();
}
else if( Args[ 0 ] == "quit" ){
TerminatePonder();
lout << "exiting...";
exit(0);
}
else if( Args[ 0 ] == "post" ){
Post = true;
}
else if( Args[ 0 ] == "nopost" ){
Post = false;
}
else if( Args[ 0 ] == "testsuite" ){
TestSuite( Args[1], atoi(Args[2].c_str()) );
}
}
bool engine::MakeMove( int From, int To, int Turn, int Promotion )
{
int Type = Position.PieceIds[ Turn ][ From ];
if( Type == UNDEFINED ) return false;
move Move( Turn, Type, From, To, 0, Promotion );
if( From == To ) return false;
bool LegalMove = false;
TerminatePonder();
int MoveCount;
vector<move> Moves( MAX_MOVES );
GenMoves( Moves, MoveCount );
//cout << "HERE" << endl
// << "MOVE COUNT " << MoveCount << endl;
for(int i=0; i<MoveCount; i++){
//cout << NotateMove( Moves[ i ] ) << endl;
if( Moves[ i ] == Move ){
LegalMove = true;
cout << "#made " << i << " best move" << endl;
Position.MakeMove( Move );
}
}
return LegalMove;
}
string engine::ShowPieceIds()
{
stringstream sout;
for(int Color=BLACK; Color<=WHITE; Color++){
for(int Location=0; Location<64; Location++){
if( Location%8 == 0 ) sout << "\n";
int Type = Position.PieceIds[ Color ][ Location ];
sout << (Type == UNDEFINED ? '\'' : PieceChars[ Type ]) << " ";
}
}
sout << endl;
cout << sout.str();
return sout.str();
}