Chess game c++ programing using classes, objects, arays
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 |
} |
Tic tac toe game C++ programing using classes, objects, arays
Our project will be a chess game. There will be 2 players and the game will have a pause and start function. Each of chess characters will have its own correct moves for example, the pawn can only move forward and attack on angles not sraight. The game appearance will be 2 players, a board of 8 8, and soldiers. There is only one condition you have to play with another one there is no 1 player mode
Create a simple commented C++ HANGMAN game using CLASSES using the following specifications: - The game must keep track of the player misses - It must ONLY use 10 three letter words - It does NOT need a graphical implementation
A computer chess game and a human chess champion are evenly matched. They play fourteen games. Find probabilities for the following events. (Round all answers to four decimal places.) (a) They each win seven games. P = (b) The human chess champion wins ten games. P = (c) The computer wins at least ten games. P =
public class Game{ public void players() { System.out.println(“1 or more”); } } public class Chess extends Game { public void boardSize() { System.out.println(“8 by 8”); } public void players() { System.out.println(“2”); } } public class ThreeDChess extends Chess{ public void boardSize() {System.out.println(“7 levels”); } public void hasMoveableBoard() { System.out.println(“true”); } } Assume that these statements have already executed: Game g = new Game(); Chess c = new Chess(); ThreeDChess t = new ThreeDChess(); Object og = g; Game gc =...
*************************************** -----FOR C++ PROGRAMING------ *************************************** QUESTION 1 : a) A name that is used for different functions is said to be _____________ b) _______________ make it possible for classes and functions to receive not only data values to be stored or operated on via parameters but also to receive the type of data via a parameter. c) The process of constructing a function from a function template or a class from a class template is called ___________ d) Iterators are :...
Question 7 At the beginning of a game of chess, the back row consists of two (identical) rooks on the corners, then two knights next to them, then two bishops, and finally a king and a queen in the center. Bobby Fischer, an American chess legend, popularized the idea of randomly shuffled starting positions. In how many distinct ways can the eight pieces above be aligned? (You do not need to worry about color of the squares, or any other...
Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman drawing, it just needs the following: - Ten 3-letter words - Keep track of everytime the player misses a letter - Simple and commented code
Looking for some quick and useful notes on this: • Using Classes and Objects o Creating Objects: understand how to create objects, assign them, invoke methods on them and re-assign references. o The String Class: understanding and using strings and String methods o Packages: use import statement when needed o Wrapper Classes: use Wrapper classes to parse strings. • Writing classes o Anatomy of a Class: understand the concept of a class, define its instance variables, constructor and methods. o...
Game in C++. Need to make use of object-oriented techniques, i.e. implementation of classes and objects. Given a file containing a list of 4-letter words, the program needs to make the user guess. Each word difference is a minus one point. Each match is plus 4 points. String in capital or lower case should not pose a problem. The nearest match condition is the least number of difference between the two words. For example: (file.txt) JAZZ BUZZ FUZZ FIZZ QUIZ...