Communicating with XBoard (chess engine) (C++/C)

。_饼干妹妹 提交于 2020-01-02 05:13:55

问题


I was just toying around with making a basic chess engine. I was able to get a lot of advice from http://web.archive.org/web/20070704121716/http://www.brucemo.com/compchess/programming/alphabeta.htm, but the real site is down and not all the pages are archived. (Anyone know where to find a full version of Bruce's site?)

But now to the real question: how do I communicate with XBoard? I understand it is via stdin and stdout, but I've been having problems in code. Basically, to get started, I just want to

  1. receive input from XBoard and print it to the console/screen
  2. Give a move of hard-coded input to XBoard and have it make the move
  3. program utility functions and have a random chess ai which chooses random moves.

After that, I can start implementing real things like alpha-beta searching.

I am stuck on the first two things right now. Here is some code I have tried to write/borrowed.

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STR_BUFF 256

using namespace std;

int main (int argc, const char * argv[])
{
    char input[STR_BUFF];
    char output[STR_BUFF];
    while(true){
        fflush(stdout);
        // read input
        if (!fgets(input, STR_BUFF, stdin)){
            printf("terminated");
            return 0;;
        }

        printf("%s", input);
    }
    return 0;
}

I am just getting back into C after about 6 months break and this is the first project that I have used stdin/stdout pipelines to communicate with another program (minus a few basic programs) so I would appreciate any help and any explanations. I know programming a chess engine is a herculean task, but I have already programmed the rules of chess before and what I can find of Bruce's site is absolutely amazing.


回答1:


You are doing it almost right: get a command from XBoard with fgets, then report a move with printf and fflush. (One thing is wrong, though: you don't need to 'print the command to the console/screen'; you are not communicating with the console/screen; you only read commands from XBoard and send moves back to XBoard).

Probably, it would be easier to start with some existing code. Try to read sources for GNU Chess. Or download sources for any other chess engine, supporting XBoard protocol.

And here is other question with lots of information on chess engine programming: "What are some good resources for writing a chess engine?".




回答2:


I think that you're looking for pipe(), included in unistd.h. Take a look at Can popen() make bidirectional pipes like pipe() + fork()? for notes on implementation.



来源:https://stackoverflow.com/questions/9556577/communicating-with-xboard-chess-engine-c-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!