Generating a new chess project step after step

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Uri Blass
Posts: 10305
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 10
In this step I add 2 files

1)extglobals.h that include the global variables that I use and I want cpp files that are not the main function to recognize that they are external variables(they are not external only in main.cpp that include globals.h directly and including globals.h in another file cause an error because I cannot include the same file twice in the project).

2)commands.cpp that is the function that read the commands

main file is now very short and here is the code

Code: Select all

#include <stdio.h> 
#include "defines.h"//added this file in step 7  
#include "globals.h"//added this file in step 8 
#include "protos.h"//added this file in step 9
//deleted functions and saved them in command.cpp in step 10
int main()
{
	std::cout << "my program" << std::endl;// do both printing my program and printing a new line in one line of code
	readCommands();//calling a function that is defined in the file(I will later put the function in a different file)
	return 0;
}
code of extglobals.h is here and very similiar to globals.h except having extern

Code: Select all

#pragma once
#include "defines.h"

extern char CMD_BUFF[];
extern int CMD_BUFF_COUNT;
code of command.cpp is here

Code: Select all

#include <iostream>
#include "defines.h"
#include "extglobals.h"
#include "protos.h"
BOOLTYPE doCommand(const char *buf)
{
	//this function now only look at the string of the input and return false only if it is identical to the word exit or quit
	CMD_BUFF_COUNT = '\0';
	if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit")))
	{
		CMD_BUFF_COUNT = '\0';
		return false;
	}
	return true;
}
void readCommands()
{
	int nextc;//next command
	while ((nextc = getc(stdin)) != EOF)//waiting for the user to print something with enter to going into the loop at this point
	{
		//the program finished to calculate getc because the user entered some chars and clicked enter and got an input 
		//nextc is the first char that the user entered
		if (nextc == '\n') //we are at the end of the input that the user entered
		{
			CMD_BUFF[CMD_BUFF_COUNT] = '\0';
			while (CMD_BUFF_COUNT)
				if (!doCommand(CMD_BUFF)) return;
		}
		else
		{
			//we are not at the end of the user input so we put the user input in the buffer in case that we have space
			if (CMD_BUFF_COUNT >= MAX_CMD_BUFF - 1)
			{
				std::cout << "Warning: command buffer full !! " << std::endl;
				CMD_BUFF_COUNT = 0;//we do not have space so we practically delete the user input and start from scratch.
			}
			CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
		}
	}
}
Uri Blass
Posts: 10305
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 11

writing a different output at the beginning that give also the version number that is defined in difines.h
For this purpose I changed both difines.h and main.cpp
The code is almost the same as winglet at this point of time so I put also the name of Stef Luijten that is written in the winglet page.

content of main.cpp

Code: Select all

#include <stdio.h> 
#include "defines.h"  
#include "globals.h" 
#include "protos.h"

int main()
{
	std::cout << "my program" << std::endl;// do both printing my program and printing a new line in one line of code
	std::cout << chess_prog_version << std::endl;//added in step 11 print a new string based on definitions in defines.h  
	printf("my program\n");
	printf(chess_prog_version"\n");//adding in step 11 doing both prints by printf(I prefer printf so I will practically delete std::cout from the code later
	readCommands();//calling a function that is defined in the file(I will later put the function in a different file)
	return 0;
}
content of defines.h

Code: Select all

#pragma once
#define chess_prog_version "analyzer 0.0, Copyright (C) 2018, Uri Blass Stef Luijten"//adding it in step 11
#define MAX_CMD_BUFF 256
typedef int BOOLTYPE;
Uri Blass
Posts: 10305
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 12 deleting std::cout in main.cpp and replacing std::cout by printf in command.cpp

content of main.cpp

Code: Select all

#include <iostream>
#include "defines.h"  
#include "globals.h" 
#include "protos.h"

int main()
{
	//in step 12 I decided that I print less information in main and also use only printf and not std::cout
	//I also deleted #include <stdio.h> that I do not need in step 12.
	printf(chess_prog_version"\n");
	readCommands();//calling a function that is defined in the file(I will later put the function in a different file)
	return 0;
}
content of command.cpp

Code: Select all

#include <iostream>
#include "defines.h"
#include "extglobals.h"
#include "protos.h"
BOOLTYPE doCommand(const char *buf)
{
	//this function now only look at the string of the input and return false only if it is identical to the word exit or quit
	CMD_BUFF_COUNT = '\0';
	if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit")))
	{
		CMD_BUFF_COUNT = '\0';
		return false;
	}
	return true;
}
void readCommands()
{
	int nextc;//next command

	while ((nextc = getc(stdin)) != EOF)//waiting for the user to print something with enter to going into the loop at this point
	{
		//the program finished to calculate getc because the user entered some chars and clicked enter and got an input 
		//nextc is the first char that the user entered
		if (nextc == '\n') //we are at the end of the input that the user entered
		{
			CMD_BUFF[CMD_BUFF_COUNT] = '\0';
			while (CMD_BUFF_COUNT)
				if (!doCommand(CMD_BUFF)) return;
		}
		else
		{
			//we are not at the end of the user input so we put the user input in the buffer in case that we have space
			if (CMD_BUFF_COUNT >= MAX_CMD_BUFF - 1)
			{
				printf("Warning:command buffer full !! \n"); //in step 12 I use printf instead of std::cout
				CMD_BUFF_COUNT = 0;//we do not have space so we practically delete the user input and start from scratch.
			}
			CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
		}
	}
}
Uri Blass
Posts: 10305
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 13:added writing wt> in the beginning that means white to move

change only in command.cpp

content

Code: Select all

#include <iostream>
#include "defines.h"
#include "extglobals.h"
#include "protos.h"
BOOLTYPE doCommand(const char *buf)
{
	//this function now only look at the string of the input and return false only if it is identical to the word exit or quit
	CMD_BUFF_COUNT = '\0';
	if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit")))
	{
		CMD_BUFF_COUNT = '\0';
		return false;
	}
	return true;
}
void readCommands()
{
	int nextc;//next command
	printf("wt> ");//added in step 13 means white to move

	while ((nextc = getc(stdin)) != EOF)//waiting for the user to print something with enter to going into the loop at this point
	{
		//the program finished to calculate getc because the user entered some chars and clicked enter and got an input 
		//nextc is the first char that the user entered
		if (nextc == '\n') //we are at the end of the input that the user entered
		{
			CMD_BUFF[CMD_BUFF_COUNT] = '\0';
			while (CMD_BUFF_COUNT)
				if (!doCommand(CMD_BUFF)) return;
			printf("wt> ");//added in step 13
		}
		else
		{
			//we are not at the end of the user input so we put the user input in the buffer in case that we have space
			if (CMD_BUFF_COUNT >= MAX_CMD_BUFF - 1)
			{
				printf("Warning:command buffer full !! \n"); //in step 12 I use printf instead of std::cout
				CMD_BUFF_COUNT = 0;//we do not have space so we practically delete the user input and start from scratch.
			}
			CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
		}
	}
}
Uri Blass
Posts: 10305
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 14: added help that show commands that are implemented with 2 commands that are still not implemented.

change is only in the function doCommand inside command.cpp and here is the relevant code:

Code: Select all

BOOLTYPE doCommand(const char *buf)
{
	//this function look at the string of the input and return false only if it is identical to the word exit or quit
	//added responding to other commands in step 14
	//     =================================================================
    //  return when command buffer is empty(added in step 14
    //     =================================================================
	if (!strcmp(buf, ""))
	{
		CMD_BUFF_COUNT = '\0';
		return true;
	}
	CMD_BUFF_COUNT = '\0';
	//adding help in step 14
	//     =================================================================
    //  help, h, or ?: show this help
    //     =================================================================
	if ((!strcmp(buf, "help")) || (!strcmp(buf, "h")) || (!strcmp(buf, "?")))
	{
		printf(" help\n ");
		printf("black          :BLACK to move\n ");
		printf("exit           : exit program\n ");
		printf("help,h or ?    : show this help\n ");
		printf("quit           : exit program\n ");
		printf("white          : WHITE to move\n ");
		CMD_BUFF_COUNT = '\0';
		return true;
	}
	if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit")))
	{
		CMD_BUFF_COUNT = '\0';
		return false;
	}
	printf("command not implemented:  %s\n", buf);//added in step 14
	CMD_BUFF_COUNT = '\0';//added in step 14
	return true;
}
Uri Blass
Posts: 10305
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 15:added the following types to defines.h in order to work with 64 bit integers

content of defines.h after step 15:

Code: Select all

#pragma once
#define chess_prog_version "analyzer 0.0, Copyright (C) 2018, Uri Blass Stef Luijten"//adding it in step 11
#define MAX_CMD_BUFF 256
typedef int BOOLTYPE;
//added the following types at step 15
typedef unsigned long long U64;
typedef unsigned long long BitMap;
Uri Blass
Posts: 10305
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 16 added board.h to include structure of the board(I will add later functions to the struct but now I add only variables) and added board.h also for the global variables

content of board.h

Code: Select all

#pragma once
#include "defines.h"

struct Board
{
	BitMap whiteKing, whiteQueens, whiteRooks, whiteBishops, whiteKnights, whitePawns;
	BitMap blackKing, blackQueens, blackRooks, blackBishops, blackKnights, blackPawns;
	BitMap whitePieces, blackPieces, occupiedSquares;

	unsigned char nextMove;        // WHITE_MOVE or BLACK_MOVE
	unsigned char castleWhite;     // White's castle status, CANCASTLEOO = 1, CANCASTLEOOO = 2
	unsigned char castleBlack;     // Black's castle status, CANCASTLEOO = 1, CANCASTLEOOO = 2
	int epSquare;                  // En-passant target square after double pawn move
	int fiftyMove;                 // Moves since the last pawn move or capture

	 // additional variables:
	int Material;                  // incrementally updated, total material on board,
								   // in centipawns, from white's side of view

	int square[64];                // incrementally updated, this array is usefull if we want to
									  // probe what kind of piece is on a particular square.
	BOOLTYPE viewRotated;          // only used for displaying the board. TRUE or FALSE.

};
content of globals.h that now include board.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;
Uri Blass
Posts: 10305
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 17:
added data.cpp source file to init the board that now include only an empty function dataInit(); I will fill it later.
I call dataInit() in main.cpp before reading the commands and define dataInit() also in protos.h that has all the functions.

I have the following content in data.cpp

Code: Select all

void dataInit()
{

}
I have the following content in main.cpp

Code: Select all

#include <iostream>
#include "defines.h"  
#include "globals.h" 
#include "protos.h"

int main()
{
	printf(chess_prog_version"\n");
	dataInit();//added in step 17 
	readCommands();
	return 0;
}
I have the following content in protos.h

Code: Select all

#pragma once
void          dataInit(); //added in step 17
BOOLTYPE    doCommand(const char *buf);
void        readCommands();
Uri Blass
Posts: 10305
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 18 we want to add to the dataInit initializing of bitset array that should give
BITSET[0]=1, BITSET[k+1]=2*BITSET[k] for 0<=k<=62 so we need to change 3 files

We change extglobals.h data.cpp and 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


contents of extglobals.h

Code: Select all

#pragma once
#include "defines.h"

extern char CMD_BUFF[];
extern int CMD_BUFF_COUNT;
extern BitMap BITSET[];//added at step 18
content of data.cpp

Code: Select all

#include "extglobals.h"
void dataInit()
{
	int i;
	BITSET[0] = 0x1;
	for (i = 1; i < 64; i++)
		BITSET[i] = BITSET[i - 1] << 1;
}
Uri Blass
Posts: 10305
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 19
decided to add to board init function to structure of board and use it and also add boardindex to dataInit().
init is only the empty function at this stage but had to change many files for that purpose.

added the file board.cpp

content of the file

Code: Select all

//I added this file at step 19
#include "board.h"
void Board::init()
{
}
content of data.cpp that now has to include board.h because I use board.init from that file.

Code: Select all

#include "extglobals.h"
#include "board.h"//added in step 19
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();
}
content of board.h(added only void init() inside the struct

Code: Select all

#pragma once
#include "defines.h"

struct Board
{
	BitMap whiteKing, whiteQueens, whiteRooks, whiteBishops, whiteKnights, whitePawns;
	BitMap blackKing, blackQueens, blackRooks, blackBishops, blackKnights, blackPawns;
	BitMap whitePieces, blackPieces, occupiedSquares;

	unsigned char nextMove;        // WHITE_MOVE or BLACK_MOVE
	unsigned char castleWhite;     // White's castle status, CANCASTLEOO = 1, CANCASTLEOOO = 2
	unsigned char castleBlack;     // Black's castle status, CANCASTLEOO = 1, CANCASTLEOOO = 2
	int epSquare;                  // En-passant target square after double pawn move
	int fiftyMove;                 // Moves since the last pawn move or capture

	 // additional variables:
	int Material;                  // incrementally updated, total material on board,
								   // in centipawns, from white's side of view

	int square[64];                // incrementally updated, this array is usefull if we want to
									  // probe what kind of piece is on a particular square.
	BOOLTYPE viewRotated;          // only used for displaying the board. TRUE or FALSE.

	void init();//added at step 19

};
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
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