chess:programming:winboard
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
chess:programming:winboard [2021/10/11 19:15] – peter | chess:programming:winboard [2021/10/11 19:30] (current) – peter | ||
---|---|---|---|
Line 9: | Line 9: | ||
---- | ---- | ||
+ | ===== Model WinBoard Protocol Driver ===== | ||
+ | <code cpp> | ||
+ | / | ||
+ | /* Example of a WinBoard-protocol driver, by H.G.Muller */ | ||
+ | / | ||
+ | |||
+ | #include < | ||
+ | |||
+ | // four different constants, with values for WHITE and BLACK that suit your engine | ||
+ | #define WHITE 1 | ||
+ | #define BLACK 2 | ||
+ | #define NONE 0 | ||
+ | #define ANALYZE 3 | ||
+ | |||
+ | // some value that cannot occur as a valid move | ||
+ | #define INVALID 666 | ||
+ | |||
+ | // some parameter of your engine | ||
+ | #define MAXMOVES 500 /* maximum game length | ||
+ | #define MAXPLY | ||
+ | |||
+ | #define OFF 0 | ||
+ | #define ON 1 | ||
+ | |||
+ | #define DEFAULT_FEN " | ||
+ | |||
+ | typedef int MOVE; // in this example moves are encoded as an int | ||
+ | |||
+ | int moveNr; | ||
+ | MOVE gameMove[MAXMOVES]; | ||
+ | |||
+ | // Some routines your engine should have to do the various essential things | ||
+ | int MakeMove(int stm, MOVE move); | ||
+ | void UnMake(MOVE move); | ||
+ | int Setup(char *fen); | ||
+ | void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB | ||
+ | char *MoveToText(MOVE move); | ||
+ | MOVE ParseMove(char *moveText); | ||
+ | int SearchBestMove(int stm, int timeLeft, int mps, int timeControl, | ||
+ | void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input. | ||
+ | |||
+ | // Some global variables that control your engine' | ||
+ | int ponder; | ||
+ | int randomize; | ||
+ | int postThinking; | ||
+ | int resign; | ||
+ | int contemptFactor; | ||
+ | |||
+ | int TakeBack(int n) | ||
+ | { // reset the game and then replay it to the desired point | ||
+ | int last, stm; | ||
+ | stm = Setup(NULL); | ||
+ | last = moveNr - n; if(last < 0) last = 0; | ||
+ | for(moveNr=0; | ||
+ | } | ||
+ | |||
+ | void PrintResult(int stm, int score) | ||
+ | { | ||
+ | if(score == 0) printf(" | ||
+ | if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf(" | ||
+ | else printf(" | ||
+ | } | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | int stm; // side to move | ||
+ | int engineSide=NONE; | ||
+ | int timeLeft; | ||
+ | int mps, timeControl, | ||
+ | int maxDepth; | ||
+ | MOVE move, ponderMove; | ||
+ | int i, score; | ||
+ | char inBuf[80], command[80]; | ||
+ | |||
+ | while(1) { // infinite loop | ||
+ | |||
+ | fflush(stdout); | ||
+ | |||
+ | if(stm == engineSide) { // if it is the engine' | ||
+ | | ||
+ | score = SearchBestMove(stm, | ||
+ | |||
+ | if(move == INVALID) { // game apparently ended | ||
+ | engineSide = NONE; // so stop playing | ||
+ | PrintResult(stm, | ||
+ | } else { | ||
+ | stm = MakeMove(stm, | ||
+ | gameMove[moveNr++] = move; // remember game | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | fflush(stdout); | ||
+ | |||
+ | // now it is not our turn (anymore) | ||
+ | if(engineSide == ANALYZE) { // in analysis, we always ponder the position | ||
+ | PonderUntilInput(stm); | ||
+ | } else | ||
+ | if(engineSide != NONE && ponder == ON && moveNr != 0) { // ponder while waiting for input | ||
+ | if(ponderMove == INVALID) { // if we have no move to ponder on, ponder the position | ||
+ | PonderUntilInput(stm); | ||
+ | } else { | ||
+ | int newStm = MakeMove(stm, | ||
+ | PonderUntilInput(newStm); | ||
+ | UnMake(ponderMove); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | noPonder: | ||
+ | // wait for input, and read it until we have collected a complete line | ||
+ | for(i = 0; (inBuf[i] = getchar()) != ' | ||
+ | inBuf[i+1] = 0; | ||
+ | |||
+ | // extract the first word | ||
+ | sscanf(inBuf, | ||
+ | |||
+ | // recognize the command,and execute it | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | int min, sec=0; | ||
+ | sscanf(inBuf, | ||
+ | sscanf(inBuf, | ||
+ | timeControl = 60*min + sec; timePerMove = -1; | ||
+ | continue; | ||
+ | } | ||
+ | if(!strcmp(command, | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | continue; | ||
+ | } | ||
+ | if(!strcmp(command, | ||
+ | if(sscanf(inBuf+7, | ||
+ | if(sscanf(inBuf+7, | ||
+ | continue; | ||
+ | } | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | // if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | // ignored commands: | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | if(!strcmp(command, | ||
+ | int move = ParseMove(inBuf+9); | ||
+ | if(move == INVALID) printf(" | ||
+ | else { | ||
+ | stm = MakeMove(stm, | ||
+ | | ||
+ | gameMove[moveNr++] = move; // remember game | ||
+ | } | ||
+ | continue; | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE:** This is by by H.G. Muller. | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | https:// |
chess/programming/winboard.1633979707.txt.gz · Last modified: 2021/10/11 19:15 by peter