I'm looking at chess programming in C and basic use of UCI commands and I have tried to convert Silvestro Fantacci C++ HelloWorld chess engine to C but without success.
See Silvestro Fantacci C++ HelloWorld chess engine
Below is my code:
#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;
}
When I compile this I get some warnings with 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.
The file however compiles and builds but does not respond to the move 1.e2e4 when added as a UCI engine in Arena chess gui (and nor does it respond in windows command prompt..except to print the error message eg: uci What? Try again there is an error e2e4 What? Try again there is an error etc)
I'm grateful for helpful replies to show me what the problem is and how to fix this, many thanks.
XXXXXXXXXXXXXXXX-EDIT-XXXXXXXXXXXXXXXXXXXX
As I mentioned in a comment the code compiles builds and runs but does not produce the move 1....e7e5 in response to 1.e2e4 in Arena GUI and I still am looking at UCI examples to try and fix this. New code below:
#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
c doesnt support char* == literal. You need to use strcmp
line == "ucinewgame"
must be
strcmp(line, "ucinewgame") == 0