Question

Chess game c++ programing using classes, objects, arays

Chess game c++ programing using classes, objects, arays

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Step 1:
The most important parts in the chess program are Board and Pieces. Everything else depends on the existence of those two parts.
There are many different ways to implement the board and pieces in 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 of the strong engines uses an array of larger than 8x8 so that move generation will become speedier.
int board[14][16];
where the 8x8 array lies inside the larger array.
Next, we have to define the pieces so that we can recognize them.
The pieces must have the same type as the board.
In this case we are using integer.

a) const int pawn = 100;

b) const int bishop = 305;

c) const int knight = 300;

d) const int rook = 500;

e) const int queen = 900;

f) const int king = 2000;

Step 2:
Earlier, In chess Bishops and Knights are both equal we cannot assign them same value, so in this case I gave Bishops a value a little bit greater than a knights values.
Next we have to declare 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.

1

void setup (void) {

2

int i, j;

3

    for (i = 0; i < 8; i++){

4

          for (j = 0; j < 8; j++){

5

            board[i][j] = startup[i][j]; //setup starting position

6

      }

7

        }

8 }

Step 3:

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.

01

void printb (void){

02

using namespace std; // this must be here in order to begin using strings.

03

int a, b;

04

string piece;

05

for (a = 7; a > -1; a--){ // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down

06

cout << endl;

07

for (b = 0; b < 8; b++){

08

switch (board[a][b]){

09

case 0:

10

piece = "-";

11

break;

12

case pawn:

13

piece = "P";

14

break;

15

case knight:

16

piece = "N";

17

break;

18

case bishop:

19

piece = "B";

20

break;

21

case rook:

22

piece = "R";

23

break;

24

case queen:

25

piece = "Q";

26

break;

27

case king:

28

piece = "K";

29

break;

30

case -pawn:

31

piece = "p";

32

break;

33

case -knight:

34

piece = "n";

35

break;

36

case -bishop:

37

piece = "b";

38

break;

39

case -rook:

40

piece = "r";

41

break;

42

case -queen:

43

piece = "q";

44

break;

45

case -king:

46

piece = "k";

47

break;

48

}

49

  cout << " " << piece << " ";

50

}

51

}

52

53

cout << endl << endl;

54

}


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).

Step 4:

int main (void) {

1

using namespace std;

2

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

3

cout << "welcome to Chess Game!"<< endl << endl;

4

cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;

5

cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

6

string passd; // this will be the string that contains info from the user

7

setup(); //we must set up the initial position

8

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

9

    getline (cin, passd ); //ask the user to input what he wants the app to do

10

         if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements

11

          break;

12

         }

13

         if (passd.substr(0, 5) == "print")   { // display the board

14

          printb();

15

         }

16

         if (passd.substr(0, 3) == "new")   { // ask for a new game

17

          setup();

18

         }

19

         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

20

          // execute move

21

          // then display new board position

22

          int a, b, c, d;

23

          a = passd[0] - 'a';

24

          b = passd[1] - '1';

25

          c = passd[2] - 'a';

26

          d = passd[3] - '1';

27

//executes the move if its on the board!

28

          board[d][c] = board[b][a];

29

          board[b][a] = 0;

30

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

31

         }

32

        }

33

}



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.

Step 5:

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

1

          // then display new board position

2

          int a, b, c, d;

3

          a = passd[0] - 'a';

4

          b = passd[1] - '1';

5

          c = passd[2] - 'a';

6

          d = passd[3] - '1';

7

//executes the move if its on the board!

8

          board[d][c] = board[b][a];

9

          board[b][a] = 0;

10

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

11

         }


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.

Step 6:

The Whole Combined Code of Chess Program:

001) #include<iostream>

002) #include<string>

003

#include<stdlib>

006

// in this example pieces aer described as integer values

007

// we will make them constants, so that if at any time we want to change their values we can do so here

008

// but will still need to recompile

009

010

const int pawn = 100;

011

const int bishop = 305;

012

const int knight = 300;

013

const int rook = 500;

014

const int queen = 900;

015

const int king = 2000;

017

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

019

//now we need a board to put the pieces on and move around on

020

//the board data type should match the pieces data type

021

// the board in regular chess is always 8x8, but for speedy legal move generator

022

//other programs use larger than 8x8 where an 8x8 real board exists in a larger array ie 12x14

023

// but for simplicity of understanding we will use the simple 8x8

025

int board[8][8];

027

// board [rank] [file];

028

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

030

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

032

// the startup constant contains the standard starting position of all chess games (not variants)

033

//each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks

034

// for black pieces we use -piecetype. (negative)

036

void setup (void) {

037

int i, j;

038

    for (i = 0; i < 8; i++){

039

          for (j = 0; j < 8; j++){

040

            board[i][j] = startup[i][j]; //setup starting position

041

      }

042

        }

044

}

046

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

048

// next we need a function that will display the board some way either graphics or text

049

// in this case we will print to the screen a text version of the boards contents

051

//it is standard in FEN notations and other text of a chess board to express each piece by it's first letter

052

// except the knight which uses 'N'

054

// the black pieces are lower case while the white pieces are upper case

055

// otherwise it is impossible to distinguish black pieces from white pieces

057

void printb (void){

058

using namespace std; // this must be here in order to begin using strings.

059

int a, b;

060

string piece;

061

for (a = 7; a > -1; a--){ // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down

062

cout << endl;

063

for (b = 0; b < 8; b++){

064

switch (board[a][b]){

065

case 0:

066

piece = "-";

067

break;

068

case pawn:

069

piece = "P";

070

break;

071

case knight:

072

piece = "N";

073

break;

074

case bishop:

075

piece = "B";

076

break;

077

case rook:

078

piece = "R";

079

break;

080

case queen:

081

piece = "Q";

082

break;

083

case king:

084

piece = "K";

085

break;

086

case -pawn:

087

piece = "p";

088

break;

089

case -knight:

090

piece = "n";

091

break;

092

case -bishop:

093

piece = "b";

094

break;

095

case -rook:

096

piece = "r";

097

break;

098

case -queen:

099

piece = "q";

100

break;

101

case -king:

102

piece = "k";

103

break;

104

}

105

  cout << " " << piece << " ";

106

}

107

}

109

cout << endl << endl;

110

}

113

// every program in win32 console must have a main

116

int main (void) {

118

using namespace std;

120

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

122

cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555" << endl << endl;

123

cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl;

124

cout << "commands: exit = quit, abort = quit, print = displays the board," << endl << "new = new game" << endl << endl;

126

string passd; // this will be the string that contains info from the user

127

setup(); //we must set up the initial position

129

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

131

    getline (cin, passd ); //ask the user to input what he wants the app to do

132

         if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort" || passd.substr(0, 4) == "quit")   { //test //for quit or exit statements

133

          break;

134

         }

135

         if (passd.substr(0, 5) == "print")   { // display the board

136

          printb();

137

         }

138

         if (passd.substr(0, 3) == "new")   { // ask for a new game

139

          setup();

140

         }

141

         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

142

          // execute move

143

          // then display new board position

145

          int a, b, c, d;

148

          a = passd[0] - 'a';

149

          b = passd[1] - '1';

150

          c = passd[2] - 'a';

151

          d = passd[3] - '1';

153

//executes the move if its on the board!

154

          board[d][c] = board[b][a];

155

          board[b][a] = 0;

156

157

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

158

         }

159

        }

160

}

Add a comment
Know the answer?
Add Answer to:
Chess game c++ programing using classes, objects, arays
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT