Generating a new chess project step after step

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 20

I see that I added board.h that I did not need to do at step 19 to data.cpp because data.cpp include extglobals.h that include board.h so I delete it.

I want to add
for (int i = 0; i < 64; i++) square = EMPTY; to the init function in board.cpp but EMPTY is not defined
so I need to add it to extglobals.h and also to define the value of EMPTY to be 0 in globals.h

content of globals.h

Code: Select all

#pragma once
#include "defines.h"//needed to include it so the compiler can know that MAX_CMD_BUFF is the constant I defined there
#include "board.h"
char CMD_BUFF[MAX_CMD_BUFF];
int CMD_BUFF_COUNT = 0;

BitMap BITSET[64];//added at step 18
int BOARDINDEX[9][9]; // index 0 is not used, only 1..8. added at step 19
Board board;//added at step 19
extern const unsigned char EMPTY = 0;                //  0000 added in step 20
content of extglobals.h

Code: Select all

#pragma once
#include "defines.h"
#include "board.h"//added at step 19

extern char CMD_BUFF[];
extern int CMD_BUFF_COUNT;
extern Board board;//added at step 19
extern BitMap BITSET[];//added at step 18
extern int BOARDINDEX[9][9]; //added at step 19
extern const unsigned char EMPTY;//added at step 20
content of board.cpp

Code: Select all

//I added this file at step 19
#include "extglobals.h"//added in step 20
void Board::init()
{
	for (int i = 0; i < 64; i++) square[i] = EMPTY;//added in step 20
}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 21 adding the name of the squares
The plan is to fill them with pieces and have code like

Code: Select all

square[E1] = WHITE_KING;
but first we need to define both squares like E1 and pieces like WHITE_KING.

added the following code to extglobals.h

Code: Select all

extern const int A8; extern const int B8; extern const int C8; extern const int D8;//added const for the squares at step 21
extern const int E8; extern const int F8; extern const int G8; extern const int H8;
extern const int A7; extern const int B7; extern const int C7; extern const int D7;
extern const int E7; extern const int F7; extern const int G7; extern const int H7;
extern const int A6; extern const int B6; extern const int C6; extern const int D6;
extern const int E6; extern const int F6; extern const int G6; extern const int H6;
extern const int A5; extern const int B5; extern const int C5; extern const int D5;
extern const int E5; extern const int F5; extern const int G5; extern const int H5;
extern const int A4; extern const int B4; extern const int C4; extern const int D4;
extern const int E4; extern const int F4; extern const int G4; extern const int H4;
extern const int A3; extern const int B3; extern const int C3; extern const int D3;
extern const int E3; extern const int F3; extern const int G3; extern const int H3;
extern const int A2; extern const int B2; extern const int C2; extern const int D2;
extern const int E2; extern const int F2; extern const int G2; extern const int H2;
extern const int A1; extern const int B1; extern const int C1; extern const int D1;
extern const int E1; extern const int F1; extern const int G1; extern const int H1;
added the following code to globals.h

Code: Select all

extern const int A8 = 56; extern const int B8 = 57; extern const int C8 = 58; extern const int D8 = 59;//added values of the squares at step 21
extern const int E8 = 60; extern const int F8 = 61; extern const int G8 = 62; extern const int H8 = 63;
extern const int A7 = 48; extern const int B7 = 49; extern const int C7 = 50; extern const int D7 = 51;
extern const int E7 = 52; extern const int F7 = 53; extern const int G7 = 54; extern const int H7 = 55;
extern const int A6 = 40; extern const int B6 = 41; extern const int C6 = 42; extern const int D6 = 43;
extern const int E6 = 44; extern const int F6 = 45; extern const int G6 = 46; extern const int H6 = 47;
extern const int A5 = 32; extern const int B5 = 33; extern const int C5 = 34; extern const int D5 = 35;
extern const int E5 = 36; extern const int F5 = 37; extern const int G5 = 38; extern const int H5 = 39;
extern const int A4 = 24; extern const int B4 = 25; extern const int C4 = 26; extern const int D4 = 27;
extern const int E4 = 28; extern const int F4 = 29; extern const int G4 = 30; extern const int H4 = 31;
extern const int A3 = 16; extern const int B3 = 17; extern const int C3 = 18; extern const int D3 = 19;
extern const int E3 = 20; extern const int F3 = 21; extern const int G3 = 22; extern const int H3 = 23;
extern const int A2 = 8; extern const int B2 = 9; extern const int C2 = 10; extern const int D2 = 11;
extern const int E2 = 12; extern const int F2 = 13; extern const int G2 = 14; extern const int H2 = 15;
extern const int A1 = 0; extern const int B1 = 1; extern const int C1 = 2; extern const int D1 = 3;
extern const int E1 = 4; extern const int F1 = 5; extern const int G1 = 6; extern const int H1 = 7;
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 22 added const and numbers for the different white and black pieces

added to extraglobals.h

Code: Select all

extern const unsigned char WHITE_KNIGHT;//added const for the different pieces at step 22
extern const unsigned char WHITE_PAWN;
extern const unsigned char WHITE_KING;
extern const unsigned char WHITE_BISHOP;
extern const unsigned char WHITE_ROOK;
extern const unsigned char WHITE_QUEEN;
extern const unsigned char BLACK_KNIGHT;
extern const unsigned char BLACK_PAWN;
extern const unsigned char BLACK_KING;
extern const unsigned char BLACK_BISHOP;
extern const unsigned char BLACK_ROOK;
extern const unsigned char BLACK_QUEEN;
content of globals.h after step 22

Code: Select all

#pragma once
#include "defines.h"//needed to include it so the compiler can know that MAX_CMD_BUFF is the constant I defined there
#include "board.h"
char CMD_BUFF[MAX_CMD_BUFF];
int CMD_BUFF_COUNT = 0;

BitMap BITSET[64];//added at step 18
int BOARDINDEX[9][9]; // index 0 is not used, only 1..8. added at step 19
Board board;//added at step 19
extern const int A8 = 56; extern const int B8 = 57; extern const int C8 = 58; extern const int D8 = 59;//added values of the squares at step 21
extern const int E8 = 60; extern const int F8 = 61; extern const int G8 = 62; extern const int H8 = 63;
extern const int A7 = 48; extern const int B7 = 49; extern const int C7 = 50; extern const int D7 = 51;
extern const int E7 = 52; extern const int F7 = 53; extern const int G7 = 54; extern const int H7 = 55;
extern const int A6 = 40; extern const int B6 = 41; extern const int C6 = 42; extern const int D6 = 43;
extern const int E6 = 44; extern const int F6 = 45; extern const int G6 = 46; extern const int H6 = 47;
extern const int A5 = 32; extern const int B5 = 33; extern const int C5 = 34; extern const int D5 = 35;
extern const int E5 = 36; extern const int F5 = 37; extern const int G5 = 38; extern const int H5 = 39;
extern const int A4 = 24; extern const int B4 = 25; extern const int C4 = 26; extern const int D4 = 27;
extern const int E4 = 28; extern const int F4 = 29; extern const int G4 = 30; extern const int H4 = 31;
extern const int A3 = 16; extern const int B3 = 17; extern const int C3 = 18; extern const int D3 = 19;
extern const int E3 = 20; extern const int F3 = 21; extern const int G3 = 22; extern const int H3 = 23;
extern const int A2 = 8; extern const int B2 = 9; extern const int C2 = 10; extern const int D2 = 11;
extern const int E2 = 12; extern const int F2 = 13; extern const int G2 = 14; extern const int H2 = 15;
extern const int A1 = 0; extern const int B1 = 1; extern const int C1 = 2; extern const int D1 = 3;
extern const int E1 = 4; extern const int F1 = 5; extern const int G1 = 6; extern const int H1 = 7;
//added explanations for the numbers in step 22 and all the comments in the next lines are in step 22
//Piece identifiers, 4 bits each.
// Usefull bitwise properties of this numbering scheme:
// white = 0..., black = 1..., sliding = .1.., nonsliding = .0..
// rank/file sliding pieces = .11., diagonally sliding pieces = .1.1
// pawns and kings (without color bits), are < 3
// major pieces (without color bits set), are > 5
// minor and major pieces (without color bits set), are > 2
extern const unsigned char EMPTY = 0;                //  0000 added in step 20
extern const unsigned char WHITE_PAWN = 1;           //  0001 added numbers to the pieces in step 22
extern const unsigned char WHITE_KING = 2;           //  0010
extern const unsigned char WHITE_KNIGHT = 3;         //  0011
extern const unsigned char WHITE_BISHOP = 5;        //  0101
extern const unsigned char WHITE_ROOK = 6;           //  0110
extern const unsigned char WHITE_QUEEN = 7;          //  0111
extern const unsigned char BLACK_PAWN = 9;           //  1001
extern const unsigned char BLACK_KING = 10;          //  1010
extern const unsigned char BLACK_KNIGHT = 11;        //  1011
extern const unsigned char BLACK_BISHOP = 13;        //  1101
extern const unsigned char BLACK_ROOK = 14;          //  1110
extern const unsigned char BLACK_QUEEN = 15;         //  1111
added initializing the board at step 22 for board.cpp
content of board.cpp

Code: Select all

//I added this file at step 19
#include "extglobals.h"//added in step 20
void Board::init()
{
	for (int i = 0; i < 64; i++) square[i] = EMPTY;//added in step 20
	square[E1] = WHITE_KING;//added all the non empty pieces in step 22
	square[D1] = WHITE_QUEEN;
	square[A1] = WHITE_ROOK;
	square[H1] = WHITE_ROOK;
	square[B1] = WHITE_KNIGHT;
	square[G1] = WHITE_KNIGHT;
	square[C1] = WHITE_BISHOP;
	square[F1] = WHITE_BISHOP;
	square[A2] = WHITE_PAWN;
	square[B2] = WHITE_PAWN;
	square[C2] = WHITE_PAWN;
	square[D2] = WHITE_PAWN;
	square[E2] = WHITE_PAWN;
	square[F2] = WHITE_PAWN;
	square[G2] = WHITE_PAWN;
	square[H2] = WHITE_PAWN;

	square[E8] = BLACK_KING;
	square[D8] = BLACK_QUEEN;
	square[A8] = BLACK_ROOK;
	square[H8] = BLACK_ROOK;
	square[B8] = BLACK_KNIGHT;
	square[G8] = BLACK_KNIGHT;
	square[C8] = BLACK_BISHOP;
	square[F8] = BLACK_BISHOP;
	square[A7] = BLACK_PAWN;
	square[B7] = BLACK_PAWN;
	square[C7] = BLACK_PAWN;
	square[D7] = BLACK_PAWN;
	square[E7] = BLACK_PAWN;
	square[F7] = BLACK_PAWN;
	square[G7] = BLACK_PAWN;
	square[H7] = BLACK_PAWN;

}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 23 added function that is called info

In this step I do the following:
1)to define the structure of the function in protos.h
2)to define the function in data.cpp
3)to call the function and use it in command.cpp

added following code to protos.h

Code: Select all

void info();//added in step 23
added the following code to data.cpp in order to use printf also in that file.

Code: Select all

#include <iostream>//added in step 23
added the following code to data.cpp that define the function that now does not do much but I will add details into it later.

Code: Select all

void info()//added this function at step 23
{
	//target is to display variables for testing
	printf("info start\n");
	printf("size of board in bytes = %d\n", sizeof(board));
}
added the following code to command.cpp

Code: Select all

if (!strcmp(buf, "info"))//added in step 23
	{
		info();
		return true;
	}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 24:

add a new function to board.h

Code: Select all

void initFromSquares(int input[64], unsigned char next, int fiftyM, int castleW, int castleB, int epSq);//added at step 24
added the following code to board.cpp
for now the function does nothing and first I need to define the parameters it use in order to use it.

Code: Select all

void Board::initFromSquares(int input[64], unsigned char next, int fiftyM, int castleW, int castleB, int epSq) //added this function at step 24
{
	// sets up the board variables according to the information found in
    // the input[64] array
    // All board & game initializations are done through this function (including readfen and setup).
	//note that I need to add some variables or const in globals.h and extglobals.h in order to use this function
}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 25: adding the following lines to globals.h

Code: Select all

// Identifier of next move: added the next 2 lines in step 25
extern const unsigned char WHITE_MOVE = 0;
extern const unsigned char BLACK_MOVE = 1;
adding the following lines to extglobals.h

Code: Select all

extern const unsigned char WHITE_MOVE;//added this line and the next line in step 25
extern const unsigned char BLACK_MOVE;
adding the following line to the init function as last line in it.

Code: Select all

initFromSquares(square, WHITE_MOVE, 0, 0, 0, 0);//added this at step 25 I will correct part of the 0 when I add variables
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 26:adding the following lines to globals.h

Code: Select all

unsigned char CANCASTLEOO = 1;//added this line and next line in step 26
unsigned char CANCASTLEOOO = 2;
adding the following lines to extglobals.h

Code: Select all

extern unsigned char CANCASTLEOO;//added this line and the next line at step 26
extern unsigned char CANCASTLEOOO;
changed the last line of board::init

here is the code of board.cpp after step 26

Code: Select all

//I added this file at step 19
#include "extglobals.h"//added in step 20
void Board::init()
{
	for (int i = 0; i < 64; i++) square[i] = EMPTY;//added in step 20
	square[E1] = WHITE_KING;//added all the non empty pieces in step 22
	square[D1] = WHITE_QUEEN;
	square[A1] = WHITE_ROOK;
	square[H1] = WHITE_ROOK;
	square[B1] = WHITE_KNIGHT;
	square[G1] = WHITE_KNIGHT;
	square[C1] = WHITE_BISHOP;
	square[F1] = WHITE_BISHOP;
	square[A2] = WHITE_PAWN;
	square[B2] = WHITE_PAWN;
	square[C2] = WHITE_PAWN;
	square[D2] = WHITE_PAWN;
	square[E2] = WHITE_PAWN;
	square[F2] = WHITE_PAWN;
	square[G2] = WHITE_PAWN;
	square[H2] = WHITE_PAWN;

	square[E8] = BLACK_KING;
	square[D8] = BLACK_QUEEN;
	square[A8] = BLACK_ROOK;
	square[H8] = BLACK_ROOK;
	square[B8] = BLACK_KNIGHT;
	square[G8] = BLACK_KNIGHT;
	square[C8] = BLACK_BISHOP;
	square[F8] = BLACK_BISHOP;
	square[A7] = BLACK_PAWN;
	square[B7] = BLACK_PAWN;
	square[C7] = BLACK_PAWN;
	square[D7] = BLACK_PAWN;
	square[E7] = BLACK_PAWN;
	square[F7] = BLACK_PAWN;
	square[G7] = BLACK_PAWN;
	square[H7] = BLACK_PAWN;
	initFromSquares(square, WHITE_MOVE, 0, CANCASTLEOO + CANCASTLEOOO, CANCASTLEOO + CANCASTLEOOO, 0);//added this at step 25 and
	//changed the castle rights to be correct parameters in the opening position in step 26
}
void Board::initFromSquares(int input[64], unsigned char next, int fiftyM, int castleW, int castleB, int epSq) //added this function at step 24
{
	// sets up the board variables according to the information found in
    // the input[64] array
    // All board & game initializations are done through this function (including readfen and setup).
	//note that I need to add some variables or const in globals.h and extglobals.h in order to use this function
}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 27 using all the bitboards that I defined in step 16 in board.h inside board.cpp

I think that step 27 should be closer to step 16 and I will probably rewrite generating a new chess project step after step.

I do not like having variables that I do not use for a long time in the project because it means that I can forget about them so
I think that it is better to use what I define as soon as possible.

In any case this is the code that I added in step 27

Code: Select all

whiteKing = 0;//step 27 initializing all the bitboards that are defined in board.h to be 0.
	whiteQueens = 0;
	whiteRooks = 0;
	whiteBishops = 0;
	whiteKnights = 0;
	whitePawns = 0;
	blackKing = 0;
	blackQueens = 0;
	blackRooks = 0;
	blackBishops = 0;
	blackKnights = 0;
	blackPawns = 0;
	whitePieces = 0;
	blackPieces = 0;
	occupiedSquares = 0;
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 28

added code to initFromSquares function

Here is how the content of this function

Code: Select all

void Board::initFromSquares(int input[64], unsigned char next, int fiftyM, int castleW, int castleB, int epSq) //added this function at step 24
{
	// sets up the board variables according to the information found in
    // the input[64] array
    // All board & game initializations are done through this function (including readfen and setup).
	//note that I need to add some variables or const in globals.h and extglobals.h in order to use this function
	// bitboards
	int i;//added step 28
	whiteKing = 0;//step 27 initializing all the bitboards that are defined in board.h to be 0.
	whiteQueens = 0;
	whiteRooks = 0;
	whiteBishops = 0;
	whiteKnights = 0;
	whitePawns = 0;
	blackKing = 0;
	blackQueens = 0;
	blackRooks = 0;
	blackBishops = 0;
	blackKnights = 0;
	blackPawns = 0;
	whitePieces = 0;
	blackPieces = 0;
	occupiedSquares = 0;
	// populate the 12 bitboard:/ (step 28)
	
	for (i = 0; i < 64; i++)
	{
		square[i] = input[i];
		if (square[i] == WHITE_KING)   whiteKing = whiteKing | BITSET[i];
		if (square[i] == WHITE_QUEEN)  whiteQueens = whiteQueens | BITSET[i];
		if (square[i] == WHITE_ROOK)   whiteRooks = whiteRooks | BITSET[i];
		if (square[i] == WHITE_BISHOP) whiteBishops = whiteBishops | BITSET[i];
		if (square[i] == WHITE_KNIGHT) whiteKnights = whiteKnights | BITSET[i];
		if (square[i] == WHITE_PAWN)   whitePawns = whitePawns | BITSET[i];
		if (square[i] == BLACK_KING)   blackKing = blackKing | BITSET[i];
		if (square[i] == BLACK_QUEEN)  blackQueens = blackQueens | BITSET[i];
		if (square[i] == BLACK_ROOK)   blackRooks = blackRooks | BITSET[i];
		if (square[i] == BLACK_BISHOP) blackBishops = blackBishops | BITSET[i];
		if (square[i] == BLACK_KNIGHT) blackKnights = blackKnights | BITSET[i];
		if (square[i] == BLACK_PAWN)   blackPawns = blackPawns | BITSET[i];
	}
	whitePieces = whiteKing | whiteQueens | whiteRooks | whiteBishops | whiteKnights | whitePawns;
	blackPieces = blackKing | blackQueens | blackRooks | blackBishops | blackKnights | blackPawns;
	occupiedSquares = whitePieces | blackPieces;//end of code in step 28
	
}
Uri Blass
Posts: 10269
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Generating a new chess project step after step

Post by Uri Blass »

step 29
In step 29 I initialize other variables that are about side to move rights to castle for white and black and e.p capture square and fifty move variable

This is the code that I add in step 29 to board.cpp

Code: Select all

	nextMove = next;//step 29 adding code for initializing some variables that are not 64 bit integer
	castleWhite = castleW;
	castleBlack = castleB;
	epSquare = epSq;
	fiftyMove = fiftyM;
Note that part of step 16 should be done later before step 29 and I plan to rewrite it to have a better order of events.