#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





MultiQuote





|