将 C++ HelloWorld 国际象棋引擎代码转换为 C 代码失败
Converting C++ HelloWorld chess engine code to C code fail
我正在研究 C 语言的国际象棋编程和 UCI 命令的基本使用,我尝试将 Silvestro Fantacci C++ HelloWorld 国际象棋引擎转换为 C,但没有成功。
见Silvestro Fantacci C++ HelloWorld chess engine
下面是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define BUFFERSIZE 50
int main(int argc, char **argv)
{
char line[BUFFERSIZE];
while(fgets(line, BUFFERSIZE, stdin) != NULL)
{
if (line == "uci")
{
printf("id name rpdHWchessengineinC\n");
printf("id author Richard\n");
printf("uciok\n");
}
else if (line == "isready")
printf("readyok\n");
else if (line == "ucinewgame")
; // nothing to do
else if (line == "position startpos moves e2e4\n")
; // nothing to do
else if (line == "go ") // SF HW code here: (Line.substr (0, 3) == "go ")
// Received a command like: "go wtime 300000 btime 300000 winc 0 binc 0"
printf("bestmove e7e5\n");
else
// Command not handled
printf("What? Try again there is an error\n");
}
return 0;
}
当我编译这个时,我收到了一些 GCC 警告。
gcc -Wall -c "rpdHWchessengineinC.c" (in directory: C:\RPD_Computing_Programming\RPD_Chess_programming\RPD HelloWorld C engine)
rpdHWchessengineinC.c: In function 'main':
rpdHWchessengineinC.c:41:18: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:47:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:51:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:55:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:59:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
Compilation finished successfully.
然而,该文件编译和构建但不响应移动 1.e2e4 当添加为竞技场国际象棋 gui 中的 UCI 引擎时(它也不响应 windows 命令提示符..除了打印错误信息例如:
uci
什么?重试有错误
e2e4
什么?重试有错误等)
感谢您提供有用的回复,告诉我问题是什么以及如何解决这个问题,非常感谢。
XXXXXXXXXXXXXXXX-编辑-XXXXXXXXXXXXXXXXXXXX
正如我在评论中提到的,代码编译构建并运行但不会产生移动 1....e7e5 以响应 Arena GUI 中的 1.e2e4,我仍在查看 UCI 示例以尝试修复这个。新代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define BUFFERSIZE 50
int main(int argc, char **argv)
{
char line[BUFFERSIZE];
while(fgets(line, BUFFERSIZE, stdin) != NULL)
{
if (strcmp (line, "uci")== 0)
{
printf("id name rpdHWchessengineinC\n");
printf("id author Richard\n");
printf("uciok\n");
}
else if(strcmp(line, "isready")== 0)
{
printf("readyok\n");
}
else if (strcmp (line, "ucinewgame") == 0)
{
// nothing to do
}
else if (strcmp (line, "position startpos moves e2e4\n") == 0)
{
// nothing to do
}
else if (strcmp (line, "go ") == 0)
// SF HW code here: Line.substr (0, 3) == "go ")
{
// Received a cmd like:"go wtime 300000 btime 300000 winc 0 binc0"
printf("bestmove e7e5\n");
}
else {
// Command not handled
printf("What? Try again..there is an error.\n");
}
}
return 0;
}
XXXXXXXXXXXXXXXXXXX-结束编辑-XXXXXXXXXXXXXXXX
您不能将指针(记住数组会衰减为指针)与字符串文字进行比较,因为比较比较的是 指针 而不是指针指向的字符串。
要在 C 中比较字符串,请使用 strcmp
函数。
c 不支持 char* == 文字。你需要使用 strcmp
line == "ucinewgame"
必须
strcmp(line, "ucinewgame") == 0
我正在研究 C 语言的国际象棋编程和 UCI 命令的基本使用,我尝试将 Silvestro Fantacci C++ HelloWorld 国际象棋引擎转换为 C,但没有成功。
见Silvestro Fantacci C++ HelloWorld chess engine
下面是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define BUFFERSIZE 50
int main(int argc, char **argv)
{
char line[BUFFERSIZE];
while(fgets(line, BUFFERSIZE, stdin) != NULL)
{
if (line == "uci")
{
printf("id name rpdHWchessengineinC\n");
printf("id author Richard\n");
printf("uciok\n");
}
else if (line == "isready")
printf("readyok\n");
else if (line == "ucinewgame")
; // nothing to do
else if (line == "position startpos moves e2e4\n")
; // nothing to do
else if (line == "go ") // SF HW code here: (Line.substr (0, 3) == "go ")
// Received a command like: "go wtime 300000 btime 300000 winc 0 binc 0"
printf("bestmove e7e5\n");
else
// Command not handled
printf("What? Try again there is an error\n");
}
return 0;
}
当我编译这个时,我收到了一些 GCC 警告。
gcc -Wall -c "rpdHWchessengineinC.c" (in directory: C:\RPD_Computing_Programming\RPD_Chess_programming\RPD HelloWorld C engine)
rpdHWchessengineinC.c: In function 'main':
rpdHWchessengineinC.c:41:18: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:47:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:51:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:55:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
rpdHWchessengineinC.c:59:23: warning: comparison with string literal results in unspecified behavior [-Waddress]
Compilation finished successfully.
然而,该文件编译和构建但不响应移动 1.e2e4 当添加为竞技场国际象棋 gui 中的 UCI 引擎时(它也不响应 windows 命令提示符..除了打印错误信息例如: uci 什么?重试有错误 e2e4 什么?重试有错误等)
感谢您提供有用的回复,告诉我问题是什么以及如何解决这个问题,非常感谢。
XXXXXXXXXXXXXXXX-编辑-XXXXXXXXXXXXXXXXXXXX
正如我在评论中提到的,代码编译构建并运行但不会产生移动 1....e7e5 以响应 Arena GUI 中的 1.e2e4,我仍在查看 UCI 示例以尝试修复这个。新代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define BUFFERSIZE 50
int main(int argc, char **argv)
{
char line[BUFFERSIZE];
while(fgets(line, BUFFERSIZE, stdin) != NULL)
{
if (strcmp (line, "uci")== 0)
{
printf("id name rpdHWchessengineinC\n");
printf("id author Richard\n");
printf("uciok\n");
}
else if(strcmp(line, "isready")== 0)
{
printf("readyok\n");
}
else if (strcmp (line, "ucinewgame") == 0)
{
// nothing to do
}
else if (strcmp (line, "position startpos moves e2e4\n") == 0)
{
// nothing to do
}
else if (strcmp (line, "go ") == 0)
// SF HW code here: Line.substr (0, 3) == "go ")
{
// Received a cmd like:"go wtime 300000 btime 300000 winc 0 binc0"
printf("bestmove e7e5\n");
}
else {
// Command not handled
printf("What? Try again..there is an error.\n");
}
}
return 0;
}
XXXXXXXXXXXXXXXXXXX-结束编辑-XXXXXXXXXXXXXXXX
您不能将指针(记住数组会衰减为指针)与字符串文字进行比较,因为比较比较的是 指针 而不是指针指向的字符串。
要在 C 中比较字符串,请使用 strcmp
函数。
c 不支持 char* == 文字。你需要使用 strcmp
line == "ucinewgame"
必须
strcmp(line, "ucinewgame") == 0