将C ++ Hello World国际象棋引擎代码转换为C代码失败

时间:2015-03-23 15:41:38

标签: c++ c chess

我正在研究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.

然而,该文件编译并构建但在作为竞技场国际象棋gui中的UCI引擎添加时不响应移动1.e2e4(并且它也不会在Windows命令提示符中响应...除了打印错误消息,例如: UCI 什么?再试一次有错误 e2e4 什么?再试一次有错误等)

我很感谢有用的回复,告诉我问题是什么以及如何解决这个问题,非常感谢。

XXXXXXXXXXXXXXXX-EDIT-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-End edit-XXXXXXXXXXXXXXXX

2 个答案:

答案 0 :(得分:7)

你不能用字符串文字比较指针(记住数组衰减到指针),因为比较比较指针而不是指针所指向的字符串。

要比较C中的字符串,请使用strcmp函数。

答案 1 :(得分:2)

c不支持char * == literal。你需要使用strcmp

line == "ucinewgame"

必须是

strcmp(line, "ucinewgame") == 0