#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.
}
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
#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.
*/
}
#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
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
// 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;
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;
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.
#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));
}
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.
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");
}
}
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.