• (2 Pages)
  • +
  • 1
  • 2

Chess Board and Moving Pieces in C++ How to make a simple chess program Rate Topic: ***** 2 Votes

#1 Deepglue555  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 13-April 09

Posted 11 May 2009 - 02:04 AM

#include<iostream>
#include<string>
#include<stdlib>


// in this example pieces aer described as integer values 
// we will make them constants, so that if at any time we want to change their values we can do so here
// but will still need to recompile

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;

// an alternative would be to use string constants or another data type

//now we need a board to put the pieces on and move around on
//the board data type should match the pieces data type
// the board in regular chess is always 8x8, but for speedy legal move generator
//other programs use larger than 8x8 where an 8x8 real board exists in a larger array ie 12x14
// but for simplicity of understanding we will use the simple 8x8

int board[8][8];

// board [rank] [file];
// where rank = 0 - 7 (1 to 8 on a real chess board) and file = 0 - 7 (a - h)

const startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};

// the startup constant contains the standard starting position of all chess games (not variants)
//each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks
// for black pieces we use -piecetype. (negative)

void setup (void) {
int i, j;
	for (i = 0; i < 8; i++){
		  for (j = 0; j < 8; j++){
			board[i][j] = startup[i][j]; //setup starting position
	  }
		}

}

//the two for loops run through all the iteratins of the 8x8 array and copy the starting position to the real board.

// next we need a function that will display the board some way either graphics or text
// in this case we will print to the screen a text version of the boards contents

//it is standard in FEN notations and other text of a chess board to express each piece by it's first letter
// except the knight which uses 'N'

// the black pieces are lower case while the white pieces are upper case
// otherwise it is impossible to distinguish black pieces from white pieces

void printb (void){
using namespace std; // this must be here in order to begin using strings.
int a, b;
string piece;
for (a = 7; a > -1; a--){  // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down
cout << endl;
 for (b = 0; b < 8; b++){
 switch (board[a][b]){
 case 0:
 piece = "-";
 break;
 case pawn:
 piece = "P";
 break;
 case knight:
 piece = "N";
 break;
 case bishop:
 piece = "B";
 break;
 case rook:
 piece = "R";
 break;
 case queen:
 piece = "Q";
 break;
 case king:
 piece = "K";
 break;
 case -pawn:
 piece = "p";
 break;
 case -knight:
 piece = "n";
 break;
 case -bishop:
 piece = "b";
 break;
 case -rook:
 piece = "r";
 break;
 case -queen:
 piece = "q";
 break;
 case -king:
 piece = "k";
 break;
 }
  cout << " " << piece << " ";
 }
}

 cout << endl << endl;
}


// every program in win32 console must have a main


int main (void) {

using namespace std;

//we need to tell the user about the program  ..  and how to use it

cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555" << endl << endl;
cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;
cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

string passd; // this will be the string that contains info from the user
setup(); //we must set up the initial position

while (1){ // a while loop that always loops; except when a break; statement occurs

	getline (cin, passd );  //ask the user to input what he wants the app to do
		 if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements
		  break;
		 }
		 if (passd.substr(0, 5) == "print")   { // display the board
		  printb();
		 }
		 if (passd.substr(0, 3) == "new")   { // ask for a new game
		  setup();
		 }
		 if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8")   { // this statement makes sure both squares are on the chess board when executing //a move
		  // execute move
		  // then display new board position

		  int a, b, c, d;


		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = 0;

		  printb(); //prints out to the screen the updated position after moving the pieces
		 }
		}
}
 


Hello Welcome to my tutorial!

The two most important parts of a chess program, is the Board and Pieces


Everything else depends on the existence
of those two parts.

There are several ways of implementing the board and pieces in a computer language,
but for the sake of simplicity and ease of understanding we will use an [8]x[8] board with integers.
int board[8][8];
many strong engines use a array of larger than 8x8 so that legal move generation will be speedier.
int board[14][16];
where the 8x8 array lies inside the larger array.

Next we must have the pieces defined somehow so that we can recognize them.
The pieces must have the same type as the board.
in this case we are using integer.

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;



Although in early chess Bishops and Knights are both equal we cannot define them the same value and expect the compiler to distinguish the two, so in this case I gave Bishops a value a little bit greater than a knights values.

Next we need a const array to hold the starting position of a chess game.
This will come in handy when the user wants to restart a new game.
Now that we have already stated the piece value as conststants, we can call them by their name instead of writting in the values.
const startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};


Now that is done.
We need a function that sets up the board for the start of a game.
void setup (void) {
int i, j;
	for (i = 0; i < 8; i++){
		  for (j = 0; j < 8; j++){
			board[i][j] = startup[i][j]; //setup starting position
	  }
		}

}



It is a good idea to put function prototypes before int main() {...}
a function must come before or after int main but never inside int main.

Now a good idea is to have a function that display the pieces on the board that the user can understand and follow.

void printb (void){
using namespace std; // this must be here in order to begin using strings.
int a, b;
string piece;
for (a = 7; a > -1; a--){  // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down
cout << endl;
 for (b = 0; b < 8; b++){
 switch (board[a][b]){
 case 0:
 piece = "-";
 break;
 case pawn:
 piece = "P";
 break;
 case knight:
 piece = "N";
 break;
 case bishop:
 piece = "B";
 break;
 case rook:
 piece = "R";
 break;
 case queen:
 piece = "Q";
 break;
 case king:
 piece = "K";
 break;
 case -pawn:
 piece = "p";
 break;
 case -knight:
 piece = "n";
 break;
 case -bishop:
 piece = "b";
 break;
 case -rook:
 piece = "r";
 break;
 case -queen:
 piece = "q";
 break;
 case -king:
 piece = "k";
 break;
 }
  cout << " " << piece << " ";
 }
}

 cout << endl << endl;
}


The above code will print out the board for whites perspective.
To change to blacks perspective just change a to count up 0 - 7 and b to count down 7 - 0.
It is common in FEN notation and other text notations to have white uppercase first letter of each piece and black as lowercase. Except that N = knight. (N = white knight and n = black knight).


int main (void) {

using namespace std;

//we need to tell the user about the program  ..  and how to use it

cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555" << endl << endl;
cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;
cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

string passd; // this will be the string that contains info from the user
setup(); //we must set up the initial position

while (1){ // a while loop that always loops; except when a break; statement occurs

	getline (cin, passd );  //ask the user to input what he wants the app to do
		 if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements
		  break;
		 }
		 if (passd.substr(0, 5) == "print")   { // display the board
		  printb();
		 }
		 if (passd.substr(0, 3) == "new")   { // ask for a new game
		  setup();
		 }
		 if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8")   { // this statement makes sure both squares are on the chess board when executing //a move
		  // execute move
		  // then display new board position

		  int a, b, c, d;


		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = 0;

		  printb(); //prints out to the screen the updated position after moving the pieces
		 }
		}
}


Now that we have int main, what should it do?
well here it starts with using namespace std; so we can now use strings, cout, cin and getline.

Next comes the cout << statements to tell the user the name of the program, the author and how to use it.
Next i declared the string for passing info to the program from the user. I named it passd.

Next we call setup(); which sets up the board before play starts. (if we didn't our board would be filled with some strange integes most of which would be nonsense and not understood ).

Next i setup a while(1) loop that would ultimately go forever, if it weren't for a break statement that is activated when the user types quit, abort or exit.

Next i used the string passd to get info from the user. getline (cin, passd );
it is then run through a set of tests to see if it matches a statement for a predetermined action.
Ie user inputs 'print' will now print the contents of the board, user inputs 'exit' and the program will end.

if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8")   { // this statement makes sure both squares are on the chess board when executing //a move
		  // execute move
		  // then display new board position

		  int a, b, c, d;


		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = 0;

		  printb(); //prints out to the screen the updated position after moving the pieces
		 }



The above statement make sure that the four characters are correct for 4 letter algebraic coordinates if they are
then the move is performed on the board then the updated position is shown on the screen.
Then it will repeat all over for more input from the user.

Part 2 will contain checking for legal moves.

-deepglue555

This post has been edited by Deepglue555: 13 May 2009 - 08:15 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Chess Board and Moving Pieces in C++

#2 slashx24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 09

Posted 18 May 2009 - 08:22 PM

Wow, this is great. I've been wanting to make a chess game for ages. When will we see part 2? :D
Was This Post Helpful? 0
  • +
  • -

#3 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 22 May 2009 - 12:14 PM

Hi
[quote name='Deepglue555' date='11 May, 2009 - 01:04 AM' post='637420']

Interesting post, many thanks. Chess game computing is a small time interest of mine (I am just a beginner/hobby programmer). I need to study your code more but initially I saved it as a C++ file & tried to compile & run it in DevC++4.992 but have these error messages;

\D stdlib: No such file or directory.
30 ISO C++ forbids declaration of `startup' with no type

So how do I resolve these errors? (has the first one something to do with #include<stdlib> ?? ).

I look forward to your reply and also to part 2.

Thanks & best wishes
Was This Post Helpful? 0
  • +
  • -

#4 Deepglue555  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 13-April 09

Posted 24 May 2009 - 05:57 AM

[quote name='rpd' date='22 May, 2009 - 11:14 AM' post='650413']
Hi

View PostDeepglue555, on 11 May, 2009 - 01:04 AM, said:

Interesting post, many thanks. Chess game computing is a small time interest of mine (I am just a beginner/hobby programmer). I need to study your code more but initially I saved it as a C++ file & tried to compile & run it in DevC++4.992 but have these error messages;

\D stdlib: No such file or directory.
30 ISO C++ forbids declaration of `startup' with no type

So how do I resolve these errors? (has the first one something to do with #include<stdlib> ?? ).

I look forward to your reply and also to part 2.

Thanks & best wishes



ah yes i ran into this myself after i posted it
simply change
const startup[8][8] = ....
to
const int startup[8][8] =
..and delete #
include<stdlib>
it doesnt need stdlib i was getting ahead of myself when i put that in.

then it should compile no problem under Dev++

borland assumes type int (integer) when not specified for constants
whereas Dev++ requires it explicitly.

and thanks.
Part 2 is a little delayed but might be coming soon.
Was This Post Helpful? 0
  • +
  • -

#5 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 24 May 2009 - 01:59 PM

[quote name='Deepglue555' date='24 May, 2009 - 04:57 AM' post='651661']
Hi again

Thanks for your reply (I had found out about the 2 fixes needed to get your code to compile & run & which works fine now - but thanks for confirming it!).

I am playing around with this code a little & specifically want to use my initial board array code with the rest of your code to update the board.
I would like to use my board array code to substitute for your startup code (if you see what i mean!?)
At present I cannot get it to work so I am grateful for your help.


Here is my board array code
char board [8] [8]= {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},  
			  };

for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
cout << board [i] [j] << " ";
}
cout <<endl;
}


So far I have used it like this:
int board [8] [8];

const int startup[8][8] =  {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},  
			  };
 // the startup constant contains the standard starting position of all chess games (not variants)
//each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks
// for black pieces we use lowercase  & -piecetype. (negative)

void setup (void) {
int i, j;
	for (i = 0; i < 8; i++){
		  for (j = 0; j < 8; j++){
			board[i][j] = startup[i][j]; //setup starting position
	  }
		}
}
//the two for loops run through all the iteratins of the 8x8 array and copy the starting position 
//to the real board.
// next we need a function that will display the board some way either graphics or text
// in this case we will print to the screen a text version of the boards contents

//it is standard in FEN notations and other text of a chess board to express each piece 
//by it's first letter
// except the knight which uses 'N'
// the black pieces are lower case while the white pieces are upper case
// otherwise it is impossible to distinguish black pieces from white pieces
//================================================================================
void printb (void){

int a, b;
string piece;
for (a = 7; a > -1; a--){  
// we must iterate the ranks down from 7 to 0 otherwise the board will be upside down
cout << endl;
for (b = 0; b < 8; b++){
switch (board[a][b]){
case 0:
piece = "-";
break;
case pawn:
piece = "P";
break;
case knight:
piece = "N";
break;
case bishop:
piece = "B";
break;
case rook:
piece = "R";
break;
case queen:
piece = "Q";
break;
case king:
piece = "K";
break;
case -pawn:
piece = "p";
break;
case -knight:
piece = "n";
break;
case -bishop:
piece = "b";
break;
case -rook:
piece = "r";
break;
case -queen:
piece = "q";
break;
case -king:
piece = "k";
break;
}
  cout << " " << piece << " ";
}
}

cout << endl << endl;
}  


I put this with int main() and the rest of your code & the file compiles and runs, but not unsurprisingly the board does not display properly ( if I use command print, the board does not display at all & if I input a first move, e2e4, I get a few - - - - appear!

I wonder if you can help? I would like to try to figure out if I can integrate my initial board array code together with your your board update code.

I look forward to your reply, (& to part 2)
best wishes
Was This Post Helpful? 0
  • +
  • -

#6 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 25 May 2009 - 01:07 AM

[quote name='rpd' date='24 May, 2009 - 12:59 PM' post='651951']
[quote name='Deepglue555' date='24 May, 2009 - 04:57 AM' post='651661']
Hi once again for my next update!

I have edited my latest code (with my board array & your start up code) & now I get the print command to work & print the board to console. The board displays also after I input the first move (e2e4) but the board seems to be upside down & the move has affected a black pawn & has added an extra black pawn! (mmm.... some progress! LOL!!). I show my code here & am grateful if you can suggest how I can fix this (meanwhile I will continue to try myself!):
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

//Deepglue555 code to describe pieces & board:
//=========
// in this example pieces are described as integer values 
// we will make them constants, so that if at any time we want to change their values 
//we can do so here
// but will still need to recompile

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;

// an alternative would be to use string constants or another data type
//now we need a board to put the pieces on and move around on
//the board data type should match the pieces data type
// the board in regular chess is always 8x8, but for speedy legal move generator
//other programs use larger than 8x8 where an 8x8 real board exists in a larger array ie 12x14
// but for simplicity of understanding we will use the simple 8x8

int board[8][8];

const int startup[8][8] = {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},  
			  };

// the startup constant contains the standard starting position of all chess games (not variants)
//each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks
// for black pieces we use -piecetype. (negative)

void setup (void) {
int i, j;
 for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
			board[i][j] = startup[i][j]; //setup starting position
	  }
		}
}
//the two for loops run through all the iterations of the 8x8 array and copy the starting position 
//to the real board.
// next we need a function that will display the board some way either graphics or text
// in this case we will print to the screen a text version of the boards contents

//it is standard in FEN notations and other text of a chess board to express each piece 
//by it's first letter
// except the knight which uses 'N'
// the black pieces are lower case while the white pieces are upper case
// otherwise it is impossible to distinguish black pieces from white pieces

void printb (void){

int a, b;
string piece;
for (a = 7; a > -1; a--){  
// we must iterate the ranks down from 7 to 0 otherwise the board will be upside down
cout << endl;
for (b = 0; b < 8; b++){
switch (board[a][b]){
case ' ':
piece = "-";
break;
case 'P':
piece = "P";
break;
case 'N':
piece = "N";
break;
case 'B':
piece = "B";
break;
case 'R':
piece = "R";
break;
case 'Q':
piece = "Q";
break;
case 'K':
piece = "K";
break;
case 'p':
piece = "p";
break;
case 'n':
piece = "n";
break;
case 'b':
piece = "b";
break;
case 'r':
piece = "r";
break;
case 'q':
piece = "q";
break;
case 'k':
piece = "k";
break;
}
  cout << " " << piece << " ";
}
}

cout << endl << endl;
}  
// every program in win32 console must have a main

int main (void) {

//we need to tell the user about the program  ..  and how to use it

cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555" << endl << endl;
cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;
cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

string passd; // this will be the string that contains info from the user
setup(); //we must set up the initial position

while (1){ // a while loop that always loops; except when a break; statement occurs

	getline (cin, passd );  //ask the user to input what he wants the app to do
		 if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements
		  break;
		 }
		 if (passd.substr(0, 5) == "print")   { // display the board
		  printb();
		 }
		 if (passd.substr(0, 3) == "new")   { // ask for a new game
		  setup();
		 }
		 if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8")   { // this statement makes sure both squares are on the chess board when executing //a move
		  // execute move
		  // then display new board position

		  int a, b, c, d;


		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = 0;

		  printb(); //prints out to the screen the updated position after moving the pieces
		 }
		}
}



Here is the console output the above code gives:
<<<<<<<<<<
welcome to simplechess 1.0!
created by Deepglue555

please enter your moves in 4 letter algebraic
ie e2e4 in lower case only
commands: exit = quit, abort = quit, print = displays the board,
new = new game

print

R N B Q K B N R
P P P P P P P P
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
p p p p p p p p
r n b q k b n r

e2e4

R N B Q K B N R
P P P P P P P P
- - - - - - - -
- - - - - - - -
- - - - p - - -
- - - - - - - -
p p p p p p p p
r n b q k b n r
<<<<<<<<<<<<<<<<<<

I look forward to your reply & help, many thanks
Was This Post Helpful? 0
  • +
  • -

#7 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 26 May 2009 - 05:58 AM

[quote name='rpd' date='25 May, 2009 - 12:07 AM' post='652352']
[quote name='rpd' date='24 May, 2009 - 12:59 PM' post='651951']

View PostDeepglue555, on 24 May, 2009 - 04:57 AM, said:

Hi once again for my next update!


Hi - (me again!)

I have solved the above x2 problems & I look forward to developing this further!
Below is the console text output after 1 e2e4 e7e5 :
welcome to simplechess 1.0!
created by Deepglue555 mod by rpd

please enter your moves in 4 letter algebraic
ie e2e4 in lower case only
commands: exit = quit, abort = quit, print = displays the board,
new = new game

e2e4

r n b q k b n r
p p p p p p p p
- - - - - - - -
- - - - - - - -
- - - - P - - -
- - - - - - - -
P P P P - P P P
R N B Q K B N R

e7e5

r n b q k b n r
p p p p - p p p
- - - - - - - -
- - - - p - - -
- - - - P - - -
- - - - - - - -
P P P P - P P P
R N B Q K B N R

Thanks & best wishes
Was This Post Helpful? 0
  • +
  • -

#8 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 04 June 2009 - 01:46 PM

[quote name='Deepglue555' date='24 May, 2009 - 04:57 AM' post='651661']
Hi
I am trying to figure out your code
		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = ' ';


I understand it to be about about taking the input algebraic notation string & then making the move but I don't understand how this happens!
The move notation is 4 letter algebraic (represented by int a,b,c,d eg e2e4.)

Continuing the example of input move e2e4 then it seems to me
a =e2e4[0]-e (ie 2e4[0] ???????!!-I don't understand this!)
b=e2e4[1]-1 (likewise I don't understand this!)
c=e2e4[2]-e (likewise I don't understand this!)

I cannot even try to explain what I think is happening with board[d][c]=board [b][a]!!!

I am most grateful if you could try & explain both these more clearly to me. I am also looking forward to you adding the code for the computer to make a move in reply!

I am grateful for any helpful reply you can give-thanks
Was This Post Helpful? 0
  • +
  • -

#9 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 07 June 2009 - 05:33 AM

[quote name='rpd' date='4 Jun, 2009 - 12:46 PM' post='663648']

View PostDeepglue555, on 24 May, 2009 - 04:57 AM, said:

Hi
I am trying to figure out your code
		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = ' ';


I understand it to be about about taking the input algebraic notation string & then making the move but I don't understand how this happens!
The move notation is 4 letter algebraic (represented by int a,b,c,d eg e2e4.)

Continuing the example of input move e2e4 then it seems to me
a =e2e4[0]-e (ie 2e4[0] ???????!!-I don't understand this!)
b=e2e4[1]-1 (likewise I don't understand this!)
c=e2e4[2]-e (likewise I don't understand this!)

I cannot even try to explain what I think is happening with board[d][c]=board [b][a]!!!

I am most grateful if you could try & explain both these more clearly to me. I am also looking forward to you adding the code for the computer to make a move in reply!

I am grateful for any helpful reply you can give-thanks

Hi
Can anyone explain this to me please?
I am still trying to figure it out by myself but haven't done so yet, so again I am grateful for some helpful reply,
many thanks
Was This Post Helpful? 0
  • +
  • -

#10 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 13 June 2009 - 04:21 AM

Anyone got an answer & or help and reply to my last post?

I also have a new problem I would like help with. This is the Winboard protocol & specifically simple C code a chess engine needs to communicate with Winboard (2 way process between engine & Winboard -ie Winboard accepts input (white side) move and takes move from engine and is then ready for next input move (from white side again) and so on.
See this link for Winboard protocol:
www.tim-mann.org/xboard/engine-intf.html

I am presently studying TSCP engine code which has Winboard capability & am trying to implement this for FirstChess engine code (which doesn't have Winboard function) but am having problems with this!

Are there any C tutorials that would help me understand more how to write such code to help interface commands between programs?

I hope for some helpful replies, thanks
Was This Post Helpful? 0
  • +
  • -

#11 beckam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-June 09

Posted 30 June 2009 - 12:50 PM

Hi, I compile your Code but the following errors stop the program, please can someone tell me as fix the Code?

57 C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected constructor, destructor, or type conversion before "namespace"
57 C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected `,' or `;' before "namespace"
61 C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected namespace-name before ';' token
61 C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h `<type error>' is not a namespace
Was This Post Helpful? 0
  • +
  • -

#12 Deepglue555  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 13-April 09

Posted 13 July 2009 - 01:37 AM

[quote name='rpd' date='7 Jun, 2009 - 04:33 AM' post='665803']

View Postrpd, on 4 Jun, 2009 - 12:46 PM, said:

View PostDeepglue555, on 24 May, 2009 - 04:57 AM, said:

Hi
I am trying to figure out your code
		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = ' ';


I understand it to be about about taking the input algebraic notation string & then making the move but I don't understand how this happens!
The move notation is 4 letter algebraic (represented by int a,b,c,d eg e2e4.)

Continuing the example of input move e2e4 then it seems to me
a =e2e4[0]-e (ie 2e4[0] ???????!!-I don't understand this!)
b=e2e4[1]-1 (likewise I don't understand this!)
c=e2e4[2]-e (likewise I don't understand this!)

I cannot even try to explain what I think is happening with board[d][c]=board [b][a]!!!

I am most grateful if you could try & explain both these more clearly to me. I am also looking forward to you adding the code for the computer to make a move in reply!

I am grateful for any helpful reply you can give-thanks

Hi
Can anyone explain this to me please?
I am still trying to figure it out by myself but haven't done so yet, so again I am grateful for some helpful reply,
many thanks



a = passd[0] - 'a';
b = passd[1] - '1';
c = passd[2] - 'a';
d = passd[3] - '1';


well what this does is

passd[] is a container for string values in ascii
since our move notation is only / always 4 characters thats all we need
the first character will be in passd[0] the second in passd[1] etc all the way to 3


when we subtract passd[0] - 'a' we are doing arithmetic with the ascii values
in this case 'a' = 97 'b' = 98 etc

so if passd[0] = 97 then 97 - 97 = 0
then a = passd[0] - 'a'
makes a = 0;

so the equivalent file on chess board irl would be the A file numerically is represented as 0 (zero)
(i have found in programming its better to start with zero than 1)

//executes the move if its on the board!
board[d][c] = board[b][a];
board[b][a] = ' ';

So if you can Grasp the concept thus far

we now have a = File to move from (0 - 7), b = rank to move from (0 - 7)
c = file to move to (0 - 7), and d rank to move to (0 - 7)

board[d][c] = board[b][a];
^moves the piece from the from square to the 'to' square.

board[b][a] = ' ';
^and this deletes the old piece (otherwise we would now have the same piece on two squares now)

I have actually looked at TSCP a few times, and it is not absolutely simple. so thats what i tried to do here.

I honestly dont know if i could make it simpler than it is ;)
Was This Post Helpful? 0
  • +
  • -

#13 Deepglue555  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 13-April 09

Posted 13 July 2009 - 01:43 AM

View Postrpd, on 13 Jun, 2009 - 03:21 AM, said:

Anyone got an answer & or help and reply to my last post?

I also have a new problem I would like help with. This is the Winboard protocol & specifically simple C code a chess engine needs to communicate with Winboard (2 way process between engine & Winboard -ie Winboard accepts input (white side) move and takes move from engine and is then ready for next input move (from white side again) and so on.
See this link for Winboard protocol:
www.tim-mann.org/xboard/engine-intf.html

I am presently studying TSCP engine code which has Winboard capability & am trying to implement this for FirstChess engine code (which doesn't have Winboard function) but am having problems with this!

Are there any C tutorials that would help me understand more how to write such code to help interface commands between programs?

I hope for some helpful replies, thanks




It depends on the language and IDE you are using

I have built chess programs in Both Visual Basic 6, and Borland C++ 6.0
But I found UCI much simpler and straight forward so i used that.

in UCI there are commands that can be sent and recieved, there are some resources on the Internet shouldn't be hard to find with www.google.ca
Was This Post Helpful? 0
  • +
  • -

#14 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Posted 13 July 2009 - 05:42 AM

Thanks for your reply (& explanations) Deepglue555 (I thought you had vanished as the original post was sometime ago now!).

I need to try to fully understand the moving code with the ascii values input.

As far as the winboard protocol goes I have a better understanding of that now as I received help from Winboard forum. I will later try UCI communication protocol which I hope is more straightforward than Winboard?! I will see!....

If I get stuck with any chess code I will post up my problem here! (I have just installed emacs on my Vista OS & want to add chess.el to it & haven't quite managed that yet!! Oh the joy of chess & computing!!).

Best wishes to you & all here
Was This Post Helpful? 0
  • +
  • -

#15 Deepglue555  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 110
  • Joined: 13-April 09

Posted 16 September 2009 - 04:40 AM

here this version will compile under DEV C++ 4.9.9.2

#include<iostream>
#include<string>


// in this example pieces aer described as integer values 
// we will make them constants, so that if at any time we want to change their values we can do so here
// but will still need to recompile

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;

// an alternative would be to use string constants or another data type

//now we need a board to put the pieces on and move around on
//the board data type should match the pieces data type
// the board in regular chess is always 8x8, but for speedy legal move generator
//other programs use larger than 8x8 where an 8x8 real board exists in a larger array ie 12x14
// but for simplicity of understanding we will use the simple 8x8

int board[8][8];

// board [rank] [file];
// where rank = 0 - 7 (1 to 8 on a real chess board) and file = 0 - 7 (a - h)

const int startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};

// the startup constant contains the standard starting position of all chess games (not variants)
//each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks
// for black pieces we use -piecetype. (negative)

void setup (void) {
int i, j;
	for (i = 0; i < 8; i++){
		  for (j = 0; j < 8; j++){
			board[i][j] = startup[i][j]; //setup starting position
	  }
		}

}

//the two for loops run through all the iteratins of the 8x8 array and copy the starting position to the real board.

// next we need a function that will display the board some way either graphics or text
// in this case we will print to the screen a text version of the boards contents

//it is standard in FEN notations and other text of a chess board to express each piece by it's first letter
// except the knight which uses 'N'

// the black pieces are lower case while the white pieces are upper case
// otherwise it is impossible to distinguish black pieces from white pieces

void printb (void){
using namespace std; // this must be here in order to begin using strings.
int a, b;
string piece;
for (a = 7; a > -1; a--){  // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down
cout << endl;
 for (b = 0; b < 8; b++){
 switch (board[a][b]){
 case 0:
 piece = "-";
 break;
 case pawn:
 piece = "P";
 break;
 case knight:
 piece = "N";
 break;
 case bishop:
 piece = "B";
 break;
 case rook:
 piece = "R";
 break;
 case queen:
 piece = "Q";
 break;
 case king:
 piece = "K";
 break;
 case -pawn:
 piece = "p";
 break;
 case -knight:
 piece = "n";
 break;
 case -bishop:
 piece = "b";
 break;
 case -rook:
 piece = "r";
 break;
 case -queen:
 piece = "q";
 break;
 case -king:
 piece = "k";
 break;
 }
  cout << " " << piece << " ";
 }
}

 cout << endl << endl;
}


// every program in win32 console must have a main


int main (void) {

using namespace std;

//we need to tell the user about the program  ..  and how to use it

cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555" << endl << endl;
cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;
cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

string passd; // this will be the string that contains info from the user
setup(); //we must set up the initial position

while (1){ // a while loop that always loops; except when a break; statement occurs

	getline (cin, passd );  //ask the user to input what he wants the app to do
		 if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements
		  break;
		 }
		 if (passd.substr(0, 5) == "print")   { // display the board
		  printb();
		 }
		 if (passd.substr(0, 3) == "new")   { // ask for a new game
		  setup();
		 }
		 if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8")   { // this statement makes sure both squares are on the chess board when executing //a move
		  // execute move
		  // then display new board position

		  int a, b, c, d;


		  a = passd[0] - 'a';
		  b = passd[1] - '1';
		  c = passd[2] - 'a';
		  d = passd[3] - '1';

//executes the move if its on the board!
		  board[d][c] = board[b][a];
		  board[b][a] = 0;

		  printb(); //prints out to the screen the updated position after moving the pieces
		 }
		}
}



I think ill build part 2 over code from part 1.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2