-1

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

rpd
  • 1,135
  • 4
  • 15
  • 24
  • 2
    Why do you go from c++ to c ... back in time?? – DrKoch Mar 23 '15 at 15:46
  • 2
    If you want to do this in C, you might want want to *learn* C first (and learn enough C++ too). – crashmstr Mar 23 '15 at 15:46
  • It would be easier to convert a Java chess engine to C++ than a C++ chess engine to C. – Neil Kirk Mar 23 '15 at 15:48
  • possible duplicate of [How do I properly compare strings in C?](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Julian Mar 23 '15 at 16:16
  • Thanks for the comments..@crashmstr I am learning C! Once I've learnt C I will probably go on to C++...Anyway using stcmp I have no warnings and it compiles builds and runs but still it will not play the move e7e5 in Arena..... – rpd Mar 23 '15 at 16:59
  • @rpd `I have tried to convert Silvestro Fantacci C++ HelloWorld chess engine to C but without success` Unless you know both C++ *and* C, then this conversion is doomed for failure. Second, you say you used `strcmp`, but you didn't show how you're using it. Unlike C++ strings, `strcmp` returns `0` if the strings match. Did you compare if the return for strcmp is 0? Or did you just say `if ( strcmp(line, "whaetver") ) { }`? – PaulMcKenzie Mar 23 '15 at 17:05
  • 2
    @rpd If you want to learn C++, then there is no need to learn C first. In fact it makes it harder. – Neil Kirk Mar 23 '15 at 18:35
  • @rpd - You skipped the `substr` part of the "go" line. Comparing the whole line agains "go " is not the same as comparing just the first 3 characters! Guess you will be really relieved later, when you can use proper C++ and start to forget all this stuff again. :-) – Bo Persson Apr 06 '15 at 10:27
  • Bo..you are exactly right! I skipped it because I don't know what it is and consequently have no idea how to convert it to C! Some help to do this and convert this to working C code would be greatly appreciated..thanks – rpd Apr 07 '15 at 18:22

2 Answers2

7

You can't compare pointers (remember that arrays decays to pointers) with string literals, as the comparison compare the pointers and not the strings the pointers point to.

To compare strings in C use the strcmp function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

c doesnt support char* == literal. You need to use strcmp

line == "ucinewgame"

must be

strcmp(line, "ucinewgame") == 0
pm100
  • 48,078
  • 23
  • 82
  • 145
  • 3
    The most common gotcha from people moving from C++ to C is not realizing that strings in C are effectively convention, not an actual type. – aruisdante Mar 23 '15 at 15:46