Sunday, December 16, 2012

How to program a chess engine in C++ language compatible with Xboard/Winboard: Chess Board Representation


Chess Board Representation is a very basic necessity in chess engine programming because it is the way to show the movements of the pieces and to visualize the current position of each player in the game. Some people prefer the use of arrays in C++ but this method below uses no arrays. It is just plain and simple way of representing the chess board. The lowercase letters r, n, b, k, q, and o represent Black's pieces and the uppercase letters R, N, B, K, Q, and P represent White's pieces. The numbers 1, 2, 3, 4, 5, 6, 7, 8 on the left represent the ranks while the letters a, b, c, d, e, f, g, h on the bottom represent the files.

Any suggestions and better ideas about how to represent the chess board for the chess engine program would be much more appreciated for the benefit of those who want to learn how to program their own chess engines. Also, a little sharing of your code would be a big bonus from you (I know you have better ideas, ways and methods in C++ programming to accomplish the same task). Thanks in advance :)

Source code:

/*
PROGRAM 7: Chess - board representation
AUTHOR: eternaltreasures
DATE: 2010 September 22
*/


#include <iostream>
#include <string>
using namespace std;

#define newline '\n'

//border represents the horizontal border lines between the ranks or rows of the chessboard
#define border "   +---+---+---+---+---+---+---+---+"

//sp represents the vertical spacer lines between the squares
#define sp " | "

/*
r1 to r8 represents the ranks or rows of the chess board, letters a to h represents the
files. The combination of these letters and numbers will be used in the coordinate
system of representing moves in chess.
*/
#define r8 "8 "
#define r7 "7 "
#define r6 "6 "
#define r5 "5 "
#define r4 "4 "
#define r3 "3 "
#define r2 "2 "
#define r1 "1 "
#define boardfiles "     a   b   c   d   e   f   g   h"

//ranks 8 and 7 are Black's initial position and ranks 1 and 2 for White.
char a8='r', b8='n', c8='b', d8='q', e8='k', f8='b', g8='n', h8='r';
char a7='o', b7='o', c7='o', d7='o', e7='o', f7='o', g7='o', h7='o';
char a6=' ', b6=' ', c6=' ', d6=' ', e6=' ', f6=' ', g6=' ', h6=' ';
char a5=' ', b5=' ', c5=' ', d5=' ', e5=' ', f5=' ', g5=' ', h5=' ';
char a4=' ', b4=' ', c4=' ', d4=' ', e4=' ', f4=' ', g4=' ', h4=' ';
char a3=' ', b3=' ', c3=' ', d3=' ', e3=' ', f3=' ', g3=' ', h3=' ';
char a2='P', b2='P', c2='P', d2='P', e2='P', f2='P', g2='P', h2='P';
char a1='R', b1='N', c1='B', d1='Q', e1='K', f1='B', g1='N', h1='R';


void introduction ()
{
cout << "   Chess Engine Program 1.0";
cout << newline;
cout << "   by eternaltreasures";
cout << newline;
cout << "   September 2010";
cout << newline;
cout << newline;
}

//function called by main () to display the chess board.
void displayboard ()
{

cout << border;
cout << newline;
cout <<r8<<sp <<a8<<sp <<b8<<sp <<c8<<sp <<d8<<sp <<e8<<sp <<f8<<sp <<g8<<sp <<h8<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r7<<sp  <<a7<<sp <<b7<<sp <<c7<<sp <<d7<<sp <<e7<<sp <<f7<<sp <<g7<<sp <<h7<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r6<<sp  <<a6<<sp <<b6<<sp <<c6<<sp <<d6<<sp <<e6<<sp <<f6<<sp <<g6<<sp <<h6<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r5<<sp  <<a5<<sp <<b5<<sp <<c5<<sp <<d5<<sp <<e5<<sp <<f5<<sp <<g5<<sp <<h5<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r4<<sp  <<a4<<sp <<b4<<sp <<c4<<sp <<d4<<sp <<e4<<sp <<f4<<sp <<g4<<sp <<h4<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r3<<sp  <<a3<<sp <<b3<<sp <<c3<<sp <<d3<<sp <<e3<<sp <<f3<<sp <<g3<<sp <<h3<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r2<<sp  <<a2<<sp <<b2<<sp <<c2<<sp <<d2<<sp <<e2<<sp <<f2<<sp <<g2<<sp <<h2<<sp;

cout << newline;
cout <<border;
cout << newline;
cout <<r1<<sp  <<a1<<sp <<b1<<sp <<c1<<sp <<d1<<sp <<e1<<sp <<f1<<sp <<g1<<sp <<h1<<sp;
cout << newline;
cout << border;
cout << newline;
cout << newline;
cout << boardfiles;
cout << newline;
cout << newline;
}

int main ()
{
introduction ();
displayboard ();

//Move entry - it asks the player to enter the move in coordinate system of chess move notations.
string entermove = "e2e4";
cout << newline;
cout << "Enter your move:";
cin >> entermove;
cout << entermove;
}

No comments:

Post a Comment