-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqh2ucci.cpp
304 lines (286 loc) · 7.84 KB
/
qh2ucci.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
QH2UCCI - a Qianhong to UCCI Protocol Adapter
Designed by Morning Yellow, Version: 1.6, Last Modified: Jun. 2006
Copyright (C) 2004-2006 www.elephantbase.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "../base/base.h"
#include "../base/base2.h"
#include "../base/parse.h"
#include "../base/pipe.h"
#include "../eleeye/ucci.h"
const int MAX_CHAR = 1024; // 配置文件的最大长度
// ICCS格式转换:浅红到UCCI
inline uint32_t ICCS_QH_UCCI(const char *szIccs) {
union {
char c[4];
uint32_t dw;
} Ret;
Ret.c[0] = szIccs[0] - 'A' + 'a';
Ret.c[1] = szIccs[1];
Ret.c[2] = szIccs[3] - 'A' + 'a';
Ret.c[3] = szIccs[4];
return Ret.dw;
}
// ICCS格式转换:UCCI到浅红
inline void ICCS_UCCI_QH(char *szIccs, uint32_t dwMoveStr) {
char *lpMoveStr;
lpMoveStr = (char *) &dwMoveStr;
szIccs[0] = lpMoveStr[0] - 'a' + 'A';
szIccs[1] = lpMoveStr[1];
szIccs[2] = '-';
szIccs[3] = lpMoveStr[2] - 'a' + 'A';
szIccs[4] = lpMoveStr[3];
szIccs[5] = '\0';
}
// 把UCCI引擎的FEN串转换为浅红插件的FEN串
static void FenUcci2QH(char *lp, bool bBlackMoves) {
while (*lp != ' ' && *lp != '\0') {
switch (*lp) {
case 'B':
*lp = 'E';
break;
case 'N':
*lp = 'H';
break;
case 'b':
*lp = 'e';
break;
case 'n':
*lp = 'h';
break;
default:
break;
}
lp ++;
}
strcpy(lp, bBlackMoves ? " b - - - 1" : " w - - - 1");
}
PipeStruct pipePlugin;
// 向浅红插件发送指令
inline void Adapter2QH(const char *szLineStr) {
pipePlugin.LineOutput(szLineStr);
printf("info lineoutput [%s]\n", szLineStr);
fflush(stdout);
}
// 接收浅红插件的反馈信息
inline bool QH2Adapter(char *szLineStr) {
if (pipePlugin.LineInput(szLineStr)) {
printf("info lineinput [%s]\n", szLineStr);
fflush(stdout);
return true;
} else {
return false;
}
}
inline void PrintLn(const char *sz) {
printf("%s\n", sz);
fflush(stdout);
}
// 主函数
int main(void) {
int i, nLevel, nThinkTime;
bool bQuit, bBlackMoves, bTimeOut;
uint32_t dwMoveStr;
FILE *fpIniFile;
char *lp;
int64_t llTime;
UcciCommStruct UcciComm;
char szIccs[8];
char szIniFile[MAX_CHAR], szCommand[MAX_CHAR];
char szLineStr[LINE_INPUT_MAX_CHAR];
if (BootLine() != UCCI_COMM_UCCI) {
return 0;
}
// 收到"ucci"指令后,完成以下几个任务:
// 1. 读取适配器配置文件"QH2UCCI.INI"
LocatePath(szIniFile, "QH2UCCI.INI");
nLevel = 0;
fpIniFile = fopen(szIniFile, "rt");
if (fpIniFile == NULL) {
PrintLn("uccierror");
PrintLn("bye");
return 0;
}
while (!feof(fpIniFile)) {
fgets(szLineStr, MAX_CHAR, fpIniFile);
lp = strchr(szLineStr, '\n');
if (lp != NULL) {
*lp = '\0';
}
lp = szLineStr;
if (false) {
} else if (StrEqvSkip(lp, "Command=")) {
LocatePath(szCommand, lp);
} else if (StrEqvSkip(lp, "Name=")) {
printf("id name %s\n", lp);
fflush(stdout);
} else if (StrEqvSkip(lp, "Copyright=")) {
printf("id copyright %s\n", lp);
fflush(stdout);
} else if (StrEqvSkip(lp, "Author=")) {
printf("id author %s\n", lp);
fflush(stdout);
} else if (StrEqvSkip(lp, "User=")) {
printf("id user %s\n", lp);
fflush(stdout);
} else if (StrEqvSkip(lp, "Level=")) {
nLevel = Str2Digit(lp, 0, 99);
}
}
fclose(fpIniFile);
// 2. 初使化适配器参数
bQuit = bBlackMoves = false;
pipePlugin.Open(szCommand);
sprintf(szLineStr, "LEVEL %d", nLevel);
Adapter2QH(szLineStr);
while (!QH2Adapter(szLineStr)) {
Idle();
}
Adapter2QH("BGTHINK OFF");
while (!QH2Adapter(szLineStr)) {
Idle();
}
PrintLn("option usemillisec type check default true");
PrintLn("option dualtime type label");
PrintLn("ucciok");
// 3. 接收UCCI指令
while (!bQuit) {
switch (IdleLine(UcciComm, false)) {
case UCCI_COMM_ISREADY:
PrintLn("readyok");
break;
case UCCI_COMM_STOP:
PrintLn("nobestmove");
break;
case UCCI_COMM_POSITION:
bBlackMoves = (strstr(UcciComm.szFenStr, " b") != NULL);
FenUcci2QH(UcciComm.szFenStr, bBlackMoves);
sprintf(szLineStr, "FEN %s", UcciComm.szFenStr);
Adapter2QH(szLineStr);
while (!QH2Adapter(szLineStr)) {
Idle();
}
if (UcciComm.nMoveNum > 0) {
sprintf(szLineStr, "LOAD %d", UcciComm.nMoveNum);
Adapter2QH(szLineStr);
for (i = 0; i < UcciComm.nMoveNum; i ++) {
ICCS_UCCI_QH(szIccs, UcciComm.lpdwMovesCoord[i]);
Adapter2QH(szIccs);
bBlackMoves = !bBlackMoves;
}
while (!QH2Adapter(szLineStr)) {
Idle();
}
}
break;
case UCCI_COMM_BANMOVES:
if (UcciComm.nBanMoveNum > 0) {
sprintf(szLineStr, "BAN %d", UcciComm.nMoveNum);
Adapter2QH(szLineStr);
for (i = 0; i < UcciComm.nBanMoveNum; i ++) {
ICCS_UCCI_QH(szIccs, UcciComm.lpdwBanMovesCoord[i]);
Adapter2QH(szIccs);
}
while (!QH2Adapter(szLineStr)) {
Idle();
}
}
break;
case UCCI_COMM_GO:
switch (UcciComm.Go) {
case UCCI_GO_DEPTH:
case UCCI_GO_NODES:
// 适配器不支持"depth"和"nodes"思考模式
PrintLn("nobestmove");
break;
case UCCI_GO_TIME_MOVESTOGO:
case UCCI_GO_TIME_INCREMENT:
// 在"time"思考模式下,分配适当的时间作思考
if (UcciComm.Go == UCCI_GO_TIME_MOVESTOGO) {
nThinkTime = UcciComm.nTime / UcciComm.nMovesToGo;
} else {
nThinkTime = UcciComm.nTime / 20 + UcciComm.nIncrement;
}
Adapter2QH("AI");
llTime = GetTime();
bTimeOut = false;
while (!bQuit) {
// 等待思考结果,需要检查以下几方面内容:
// (1) 控制时间
if (!bTimeOut && (int) (GetTime() - llTime) > nThinkTime) {
Adapter2QH("TIMEOUT");
bTimeOut = true;
}
// (2) 处理UCCI指令
switch (BusyLine(UcciComm, false)) {
case UCCI_COMM_STOP:
if (!bTimeOut) {
Adapter2QH("TIMEOUT");
bTimeOut = true;
}
break;
case UCCI_COMM_QUIT:
Adapter2QH("ABORT");
bQuit = true;
break;
default:
break;
}
// (3) 处理浅红插件的反馈信息
if (QH2Adapter(szLineStr)) {
if (StrEqv(szLineStr, "ERROR") || StrEqv(szLineStr, "ABORTED")) {
PrintLn("nobestmove");
} else {
dwMoveStr = ICCS_QH_UCCI(szLineStr);
printf("bestmove %.4s\n", (const char *) &dwMoveStr);
fflush(stdout);
}
Adapter2QH("UNDO");
while (!QH2Adapter(szLineStr)) {
Idle();
}
break;
} else {
Idle();
}
}
break;
default:
break;
}
break;
case UCCI_COMM_QUIT:
bQuit = true;
break;
default:
break;
}
}
// 4. 关闭浅红插件和管道
Adapter2QH("QUIT");
llTime = GetTime();
while ((int) (GetTime() - llTime) < 1000) {
if (QH2Adapter(szLineStr)) {
if (StrEqv(szLineStr, "BYE")) {
break;
}
}
}
pipePlugin.Close();
PrintLn("bye");
return 0;
}