Generating a new chess project step after step

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10297
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 30 add a new file bitops.cpp to calculate things for 64 bit numbers

First function is to count bits that is the number of 1's in the 64 bit number
content of initial code that I can compile is

Code: Select all

#include "defines.h"//added this file at step 30
unsigned int bitCnt(U64 bitmap)
{
	return 0;//plan to change it when I add the code to count bits but first I want to have something that I can compile without errors.
}
Uri Blass
Posts: 10297
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 31:adding the content of bitCnt function that count bits so now I am ready to use it.
adding bitCnt to protos.h
adding include to board.cpp
content of bitopps.cpp

Code: Select all

#include "defines.h"//added this file at step 30
unsigned int bitCnt(U64 bitmap)
{
	// MIT HAKMEM algorithm, see http://graphics.stanford.edu/~seander/bithacks.html added all the content of this function(step 31)
	static const U64  M1 = 0x5555555555555555;  // 1 zero,  1 one ...
	static const U64  M2 = 0x3333333333333333;  // 2 zeros,  2 ones ...
	static const U64  M4 = 0x0f0f0f0f0f0f0f0f;  // 4 zeros,  4 ones ...
	static const U64  M8 = 0x00ff00ff00ff00ff;  // 8 zeros,  8 ones ...
	static const U64 M16 = 0x0000ffff0000ffff;  // 16 zeros, 16 ones ...
	static const U64 M32 = 0x00000000ffffffff;  // 32 zeros, 32 ones
	bitmap = (bitmap & M1) + ((bitmap >> 1) & M1);   //put count of each  2 bits into those  2 bits
	bitmap = (bitmap & M2) + ((bitmap >> 2) & M2);   //put count of each  4 bits into those  4 bits
	bitmap = (bitmap & M4) + ((bitmap >> 4) & M4);   //put count of each  8 bits into those  8 bits
	bitmap = (bitmap & M8) + ((bitmap >> 8) & M8);   //put count of each 16 bits into those 16 bits
	bitmap = (bitmap & M16) + ((bitmap >> 16) & M16);   //put count of each 32 bits into those 32 bits
	bitmap = (bitmap & M32) + ((bitmap >> 32) & M32);   //put count of each 64 bits into those 64 bits
	return (int)bitmap; 
	/*explanation uri blass 
	M1=010101...01
	last place is place 0 because it multiply 2^0
	before last place is place 1 and we have places 0-63
	let look at 2 consecutive places of bitmap(place 2n and place 2n+1(n>=0))
	place 2n of M1 is always 1 and place 2n+1 of M1 is always 0
	bitmap&M1=            can get 1 only in place 2n(not in place 2n+1) and only if bitmap has 1 in place 2n
	(bitmap>>1)&M1=       can get 1 only in place 2n(not in place 2n+1) and only if bitmap has 1 in place 2n+1 that is pushed by >>1 to place 2n
	so practically the sum count the 1's in place 2n and 2n+1 and put it in places 2n if the sum is 0 or 1 then it stay in place 2n 
	otherwise it go to place 2n+1 and we have 01+01=10 in places 2n 2n+1 that means 2 when I translate the 2 bits to one number in base 10
	so I understand the comment for the first line of bitmap=
	second line is result of similiar reasons when M2=00110011.. and basically bitmap can get only 00 01 10 in 2 consecutive places and the sum is practically
	the sum of 1's in 4 consecutive places.

	*/
}
Content of protos.h

Code: Select all

#pragma once
unsigned int  bitCnt(BitMap);//added in step 31
void          dataInit(); //added in step 17
BOOLTYPE    doCommand(const char *buf);
void        readCommands();
void info();//added in step 23
lines that I added to board.cpp

Code: Select all

#include "defines.h"//step 31
#include "protos.h" //step 31
Note that I needed both of this lines and protos was not enough because protos practically use content of defines.h so every file that include protos.h has to include defines.h
Uri Blass
Posts: 10297
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 32:
adding material values and using them in a function

added to globals.h

Code: Select all

// Value of material, in centipawns:(added in step 32 and did not add KING_VALUE because I consider king to have no value impossible to capture it)
//I also change the value of pieces
extern const int PAWN_VALUE = 100;
extern const int KNIGHT_VALUE = 320;
extern const int BISHOP_VALUE = 330;
extern const int ROOK_VALUE = 500;
extern const int QUEEN_VALUE = 940;
added to extglobals.h

Code: Select all

extern const int PAWN_VALUE;//added material values at step 32
extern const int KNIGHT_VALUE;
extern const int BISHOP_VALUE;
extern const int ROOK_VALUE;
extern const int QUEEN_VALUE;
added to board.cpp

Code: Select all

Material = bitCnt(whitePawns) * PAWN_VALUE +//step 32 calculating material
		bitCnt(whiteKnights) * KNIGHT_VALUE +
		bitCnt(whiteBishops) * BISHOP_VALUE +
		bitCnt(whiteRooks) * ROOK_VALUE +
		bitCnt(whiteQueens) * QUEEN_VALUE;
	Material -= (bitCnt(blackPawns) * PAWN_VALUE +
		bitCnt(blackKnights) * KNIGHT_VALUE +
		bitCnt(blackBishops) * BISHOP_VALUE +
		bitCnt(blackRooks) * ROOK_VALUE +
		bitCnt(blackQueens) * QUEEN_VALUE);
Uri Blass
Posts: 10297
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 33 use the function in info of data.cpp and use the info command to print more information including using the function.
Note that I needed data.cpp to include protos.h for it.

Content of data.cpp

Code: Select all

#include "extglobals.h"
#include "protos.h"//added in step 33
#include <iostream>//added in step 23
void dataInit()
{
	int i;
	int rank, file;//added in step 19
	BITSET[0] = 0x1;
	for (i = 1; i < 64; i++)
		BITSET[i] = BITSET[i - 1] << 1;
//     ===========================================================================
//     BOARDINDEX is used to translate [file][rank] to [square],
//  Note that file is from 1..8 and rank from 1..8 (not starting from 0) and we want to translate them to 0-63 added this code in step 19
//     ===========================================================================
	for (rank = 0; rank < 9; rank++)
	{
		for (file = 0; file < 9; file++)
		{
			BOARDINDEX[file][rank] = (rank - 1) * 8 + file - 1;
		}
	}
	board.init();
}
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));
	printf("material value= %d\n", board.Material);//added the last lines in step 33
	printf("White castling rights= %d\n", int(board.castleWhite));
	printf("black castling rights= %d\n", int(board.castleBlack));
	printf("En passant square=%d\n", board.epSquare);
	printf("fifty move count=%d\n", board.fiftyMove);
	printf("number of white pawns=%d\n", bitCnt(board.whitePawns));
	printf("number of black pawns=%d\n", bitCnt(board.blackPawns));
}
Uri Blass
Posts: 10297
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 34:add display function to display the board in board.cpp and board.h

adding the following inside the struct board in board.h

Code: Select all

void display();//added at step 34
adding the following code to board.cpp

Code: Select all

void Board::display()
{
}
adding the following code to command.cpp that practically use the command.
after this step still display does nothing and I will add code in the next step.

Code: Select all

if (!strcmp(buf, "d"))//added this in step 34
	{
		board.display();
		CMD_BUFF_COUNT = '\0';
		return true;
	}
Uri Blass
Posts: 10297
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 35 adding piece names to globals.h and use them in displaying the board

added the following code to globals.h

Code: Select all

const char* PIECENAMES[16] = { "  ","P ","K ","N ","  ","B ","R ","Q ",//added it in step 35
							  "  ","P*","K*","N*","  ","B*","R*","Q*" };
added the following code to extglobals.h

Code: Select all

extern const char* PIECENAMES[];//added this in step 35
added the following code to board.cpp in order to use printf in that file

Code: Select all

#include <iostream>//added in step 35
adding content to the display function in board.cpp and here is the relevant code.

Code: Select all

oid Board::display()//I added this function at step 34 
{
	//step 35 printing information about the board
	int rank, file;
	printf("\n");
	for (rank = 8; rank >= 1; rank--)
	{
		printf("    +---+---+---+---+---+---+---+---+\n");
		printf("%3d %s", rank, "|");
		for (file = 1; file <= 8; file++)
			printf(" %s%s", PIECENAMES[square[BOARDINDEX[file][rank]]], "|");
		printf("\n");
	}

}
Uri Blass
Posts: 10297
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 »

I decided to rewrite all of the project because I think my order is not correct.

I plan to have order that start with include all the relevant files and explain the structure of the program
instead of include a file only when you need it when I think reading it will be easier.

First files that I add are of course general and not only about chess and may be the beginning for many programs that are even not about playing a game.
I will open a new thread later for rewriting generating a new chess project.