Home Previous Bottom Next

Writing a chess program in 99 steps


 

step 105: globals.h

#ifndef WINGLET_GLOBALS_H
#define WINGLET_GLOBALS_H
 
#include <iostream>
#include "defines.h"
#include "board.h"
#include "hash.h"
 
char INIFILE[80];
char PATHNAME[80];
char CMD_BUFF[MAX_CMD_BUFF];
int CMD_BUFF_COUNT = 0;
 
(...)
 
// peek interval in searched node units
int UPDATEINTERVAL = 100000;
// don't start a new iteration if STOPFRAC fraction of our max search time has passed:
double STOPFRAC = 0.6;
 
// keep track of stdout (writing to a file or to the console):
int TO_CONSOLE;
 
// Winboard constants & variables:
BOOLTYPE XB_MODE;
BOOLTYPE XB_PONDER;
BOOLTYPE XB_POST;
BOOLTYPE XB_DO_PENDING;
BOOLTYPE XB_NO_TIME_LIMIT;
unsigned char XB_NONE = 2;
unsigned char XB_ANALYZE = 3;
unsigned char XB_COMPUTER_SIDE;
int XB_MIN;
int XB_SEC;
int XB_MPS;
int XB_INC;
int XB_OTIM;
int XB_CTIM;
 
#endif

 

step 106: extglobals.h

#ifndef WINGLET_EXTGLOBALS_H
#define WINGLET_EXTGLOBALS_H
 
#include <iostream>
#include "defines.h"
#include "board.h"
#include "hash.h"
 
extern char INIFILE[];
extern char PATHNAME[];
extern char CMD_BUFF[];
extern int CMD_BUFF_COUNT;
 
extern Board board;
 
(...)
 
extern int NULLMOVE_REDUCTION;
extern int NULLMOVE_LIMIT;
extern int UPDATEINTERVAL;
extern double STOPFRAC;
 
extern int TO_CONSOLE;
 
extern BOOLTYPE XB_MODE;
extern BOOLTYPE XB_PONDER;
extern BOOLTYPE XB_POST;
extern BOOLTYPE XB_DO_PENDING;
extern BOOLTYPE XB_NO_TIME_LIMIT;
extern unsigned char XB_NONE;
extern unsigned char XB_ANALYZE;
extern unsigned char XB_COMPUTER_SIDE;
extern int XB_MIN;
extern int XB_SEC;
extern int XB_MPS;
extern int XB_INC;
extern int XB_OTIM;
extern int XB_CTIM;
 
#endif

 

step 107: winglet.cpp

#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
// windows only:
#include <Windows.h>
#include <signal.h>
#include <iostream>
#include "defines.h"
#include "protos.h"
#include "globals.h"
 
int main(int argc, char *argv[])
{
       int i;
 
       signal(SIGINT, SIG_IGN);
       setbuf(stdin, NULL);
 
       // windows only:
       SYSTEM_INFO sysinfo;
 
       std::cout << WINGLET_PROG_VERSION << std::endl;
       #ifdef WINGLET_DEBUG_PERFT
              std::cout << "WINGLET_DEBUG_PERFT defined" << std::endl;
       #endif
       #ifdef WINGLET_DEBUG_MOVES
              std::cout << "WINGLET_DEBUG_MOVES defined" << std::endl;
       #endif
       #ifdef WINGLET_VERBOSE_EVAL
              std::cout << "WINGLET_VERBOSE_EVAL defined" << std::endl;
       #endif
       #ifdef WINGLET_DEBUG_EVAL
              std::cout << "WINGLET_DEBUG_EVAL defined" << std::endl;
       #endif
       #ifdef WINGLET_VERBOSE_SEE
              std::cout << "WINGLET_VERBOSE_SEE defined" << std::endl;
       #endif
       #ifdef WINGLET_DEBUG_WINBOARD
              std::cout << "WINGLET_DEBUG_WINBOARD defined" << std::endl;
       #endif
 
       dataInit();
       board.init();
 
       // windows only:
       GetSystemInfo(&sysinfo);
    if (sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
              std::cout << "Version: X86, ";
       else if (sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
              std::cout << "Version: IA64, ";
       else if (sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
              std::cout << "Version: X64, ";
       std::cout << sysinfo.dwNumberOfProcessors << " CPU's (1 CPU used)" << std::endl;
       std::cout << "Search structure = " << sizeof(board)/1024 << "kB" << std::endl;
 
       // read the initialization file:
       strcpy(PATHNAME, argv[0]);
       strcpy(INIFILE, "wingletx.ini");  // default name
       // check command-line to see if we need to use another ini-file:
       // usage: "wingletx.exe i=somefile.ini"
       for (i = 1; i < argc; i++)
       {
              if (!strncmp(argv[i], "i=", 2)) sscanf_s(argv[i]+2,"%s", INIFILE);
       }
       readIniFile();
 
       std::cout <<  "'help' displays a list of commands" << std::endl;
       commands();
 
       return 0;
 
}

 

step 108: defines.h

#ifndef WINGLET_DEFINES_H
#define WINGLET_DEFINES_H
 
#define WINGLET_PROG_VERSION "Winglet 0.0"
//#define WINGLET_DEBUG_PERFT
//#define WINGLET_DEBUG_MOVES
//#define WINGLET_VERBOSE_EVAL
//#define WINGLET_DEBUG_EVAL
//#define WINGLET_VERBOSE_SEE
#define WINGLET_DEBUG_WINBOARD
 
#define MAX_CMD_BUFF     256   // Console command input buffer
#define MAX_MOV_BUFF    4096   // Max number of moves that we can store (all plies)
#define MAX_PLY           64   // Max search depth
#define MAX_GAME_LINE   1024   // Max number of moves in the (game + search) line that we can store
 
typedef unsigned long long U64;
typedef unsigned long long BitMap;
typedef short SHORTINT;
typedef unsigned short USHORTINT;
typedef int BOOLTYPE;
 
#endif

 

step 109: data.cpp

#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <iostream>
#include <iomanip>
#include "defines.h"
#include "protos.h"
#include "extglobals.h"
 
void dataInit()
{
(...) 
       // required for running test suites, to prevent
       // writing escape characters to a file
       TO_CONSOLE = 1;
 
       // Winboard parameters:
       XB_NONE = 2;  // not 0 or 1
       XB_ANALYZE = 3;   // not 0, 1, or 2
       XB_MODE = false;
       XB_POST = true;
       XB_PONDER = false;
       XB_DO_PENDING = false;
       XB_NO_TIME_LIMIT = false;
    return;
}
 
void info()
{
 
       //  your playground... display variables - meant for testing/verification purposes only
       std::cout << std::endl << "============ info start ==============" << std::endl;
       std::cout << "size of board, in bytes   = " << sizeof(board) << std::endl;
       std::cout << "Material value            = " << board.Material << std::endl;
       std::cout << "White castling rights     = " << int(board.castleWhite) << std::endl;
       std::cout << "Black castling rights     = " << int(board.castleBlack) << std::endl;
       std::cout << "En-passant square         = " << board.epSquare << std::endl;
       std::cout << "Fifty move count          = " << board.fiftyMove << std::endl;
 
       std::cout << "bitCnt of white pawns     = " << bitCnt(board.whitePawns) << std::endl;
       std::cout << std::endl << "bitmap of blackKnights | board.whitePawns:" << std::endl;
       displayBitmap(board.blackKnights | board.whitePawns);
       std::cout << "============ info end ================" << std::endl << std::endl;
 
       return;
}
 
void readIniFile()
{
       int nextc;
       FILE* inifile;
 
//     ===========================================================================
//     Open the ini file:
//     ===========================================================================
       inifile = fopen(INIFILE, "r");
       if (!inifile)
       {
              std::cout << "warning: no ini file " << INIFILE << std::endl;
              std::cout << "path: " << PATHNAME << std::endl;
              return;
       }
       else
       {
              std::cout << "Initialization file: " << INIFILE << std::endl;
       }
 
       while ((nextc = getc(inifile)) != EOF)
       {
              if (nextc == '\n')
              {
                     CMD_BUFF[CMD_BUFF_COUNT] = '\0';
                     CMD_BUFF_COUNT = '\0';
                     if (!doIniCommand(CMD_BUFF)) break;
              }
              else
              {
                     if (CMD_BUFF_COUNT >= MAX_CMD_BUFF-1)
                     {
                           std::cout << "Warning: command buffer full !! " << std::endl;
                           CMD_BUFF_COUNT = 0;
                     }
                     CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
              }
       }
       fclose(inifile);
       return;
}
 
BOOLTYPE doIniCommand(const char *buf)
{
       int number;
 
       if (!strncmp(buf, "depth", 5))
       {
              sscanf(buf+5, "%d", &board.searchDepth);
              if (board.searchDepth < 1) board.searchDepth = 1;
              if (board.searchDepth > MAX_PLY) board.searchDepth = MAX_PLY;
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
       if (!strncmp(buf, "time", 4))
       {
              sscanf(buf+4,"%d",&board.maxTime);
              board.maxTime *= 1000;  // convert to milliseconds
              if (board.maxTime < 0) board.maxTime = 1;
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
       if (!strncmp(buf, "NULLMOVE_REDUCTION", 18))
       {
              sscanf(buf+18, "%d", &NULLMOVE_REDUCTION);
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
       if (!strncmp(buf, "NULLMOVE_LIMIT", 14))
       {
              sscanf(buf+14, "%d", &NULLMOVE_LIMIT);
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
       if (!strncmp(buf, "STOP_FRAC", 9))
       {
              sscanf(buf+9, "%d", &number);
              STOPFRAC = (float)(number/100.0);
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
       return true;
}


Home Previous Top Next

last update: Friday 08 July 2011