Home Previous Bottom Next

Writing a chess program in 99 steps


 
step 88: command.cpp

(...)
 
BOOLTYPE doCommand(const char *buf)
{
       Move move, dummy;
       char sanMove[12];
       Timer timer;
       U64 msStart;
       U64 msStop;
       U64 perftcount;
       char userinput[80];
       int i, j, number;
 
// =================================================================
// return when command buffer is empty
// =================================================================
 
       if (!strcmp(buf, ""))
       {
              CMD_BUFF_COUNT = '\0';
              return true;   
       }
 
// =================================================================
// help, h, or ?: show this help
// =================================================================
 
       if ((!strcmp(buf, "help")) || (!strcmp(buf, "h")) || (!strcmp(buf, "?")))
       {
              std::cout << std::endl << "help:" << std::endl;
              std::cout << "black               : BLACK to move" << std::endl;
              std::cout << "cc                  : play computer-to-computer " << std::endl;
              std::cout << "d                   : display board " << std::endl;
              std::cout << "exit                : exit program " << std::endl;
              std::cout << "eval                : show static evaluation of this position" << std::endl;
              std::cout << "game                : show game moves " << std::endl;
              std::cout << "go                  : computer next move " << std::endl;
              std::cout << "help, h, or ?       : show this help " << std::endl;
              std::cout << "info                : display variables (for testing purposes)" << std::endl;
              std::cout << "move e2e4, or h7h8q : enter a move (use this format)" << std::endl;
              std::cout << "moves               : show all legal moves" << std::endl;
              std::cout << "new                 : start new game" << std::endl;
              std::cout << "perf                : benchmark a number of key functions" << std::endl;
              std::cout << "perft n             : calculate raw number of nodes from here, depth n " << std::endl;
              std::cout << "quit                : exit program " << std::endl;
              std::cout << "r                   : rotate board " << std::endl;
              std::cout << "readfen filename n  : reads #-th FEN position from filename" << std::endl;
              #ifdef WINGLET_VERBOSE_SEE
                     std::cout << "qsearch             : shows sorted capture movelist" << std::endl;
              #endif
              std::cout << "sd n                : set the search depth to n" << std::endl;
              std::cout << "setup               : setup board... " << std::endl;
              std::cout << "time s              : time per move in seconds" << std::endl;
              std::cout << "undo                : take back last move" << std::endl;
              std::cout << "white               : WHITE to move" << std::endl;
              std::cout << std::endl;
              CMD_BUFF_COUNT = '\0';
              return true;
       }
 
(...)
 
#ifdef WINGLET_VERBOSE_SEE
// =================================================================
// qsearch : shows sorted capture move list
// =================================================================
       if (!strncmp(buf, "qsearch", 3))
       {
              board.moveBufLen[0] = 0;
              board.moveBufLen[1] = captgen(board.moveBufLen[0]);
              std::cout << std::endl << "sorted capturing moves from this position:" << std::endl;
              std::cout << std::endl << "        score:" << std::endl;
              number = 0;
              for (i = board.moveBufLen[0]; i < board.moveBufLen[1]; i++)
              {
                     makeMove(board.moveBuffer[i]);
                     if (isOtherKingAttacked())
                     {
                           unmakeMove(board.moveBuffer[i]);
                     }
                     else
                     {
                           unmakeMove(board.moveBuffer[i]);
                           std::cout << ++number << ". ";
                           displayMove(board.moveBuffer[i]);
                           std::cout << "   " << board.moveBuffer[i + OFFSET].moveInt << std::endl;
                     }
              }
              CMD_BUFF_COUNT = '\0';
              return true;
       }
#endif

(...)



step 89: defines.h

#ifndef WINGLET_DEFINES_H
#define WINGLET_DEFINES_H
 
#define WINGLET_PROG_VERSION "winglet 0.0, Copyright (C) 2011, Stef Luijten"
//#define WINGLET_DEBUG_PERFT
//#define WINGLET_DEBUG_MOVES
//#define WINGLET_VERBOSE_EVAL
//#define WINGLET_DEBUG_EVAL
//#define WINGLET_VERBOSE_SEE
 
#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 90: wingletx.cpp

#include <Windows.h>
#include <iostream>
#include "defines.h"
#include "protos.h"
#include "globals.h"
 
int main(int argc, char *argv[])
{
       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
 
       dataInit();
       board.init();
       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;
       std::cout <<  "'help' displays a list of commands" << std::endl;
 
       readCommands();
       return 0;
 
}

 

  a new compiler directive WINGLET_VERBOSE_SEE will activate verbose output during quiescence search, it also activates a new user command qsearch. Intended use is to read, or set up, a position and type 'qsearch'  to see how the list of sorted / filtered moves is produced
  the search produces reasonable results now, so you can actually start playing games against winglet.

 


Home Previous Top Next

last update: Sunday 26 June 2011