-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchessProject.cpp
More file actions
221 lines (183 loc) · 8.69 KB
/
Copy pathchessProject.cpp
File metadata and controls
221 lines (183 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "chessProject.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
bool exit = false;
int menuSelection;
string playerOneName;
string playerTwoName;
while (exit != true){
cout << "\n\n";
cout << "MAIN MENU" << endl;
cout << endl;
cout << "Please select an option (1, 2, 0). Choice: " << endl;
cout << endl;
cout << "1: Rules of chess" << endl;
cout << "2: A game of chess" << endl;
cout << "0: Exit" << endl;
cout << endl;
cin >> menuSelection;
if (menuSelection == 0){
exit = true;
}
else if (menuSelection == 1) {
int chessrules;
while (chessrules != 4){
cout << endl;
cout << "RULES OF CHESS \n" << endl;
cout << "Please choose an option" << endl;
cout << "1. Basic Rules" << endl;
cout << "2. Basic Movement" << endl;
cout << "3. Check" << endl;
cout << "4. Return to MAIN MENU" << endl;
cout << "\nChoice: ";
cin >> chessrules;
if (chessrules == 1) {
cout << endl;
cout << "BASIC RULES" << endl;
cout << "Player one moves first, then players alternate moves." << endl;
cout << "Making a move is required." << endl;
cout << "It is not legal to skip a move, even when having to move is detrimental." << endl;
cout << "Play continues until a king is checkmated or a draw is declared. \n" << endl;
cout << "Checkmate:" << endl;
cout << "If a player's king is placed in check and there is no legal move that player can make to escape check, then the king is said to be checkmated, the game ends, and that player loses." << endl;
cout << "Unlike other pieces, the king is never actually captured or removed from the board because checkmate ends the game. \n" << endl;
cout << "Draws:" << endl;
cout << "The game is automatically a draw if the player to move is not in check but has no legal move." << endl;
cout << "The game is immediately drawn when there is no possibility of checkmate for either side with any series of legal moves." << endl;
cout << "\t King against king" << endl;
cout << "\t King against king and bishop" << endl;
cout << "\t King against king and knight" << endl;
cout << "\t King and bishop against king and bishop, with both bishops on squares of the same color" << endl;
cout << "The game is a draw if a player only has a king left and cannot checkmate within 10 moves." << endl;
}
else if (chessrules == 2) {
cout << endl;
cout << "Basic Movement" << endl;
cout << endl;
cout << "King:" << endl;
cout << "\t The king moves exactly one square horizontally, vertically, or diagonally." << endl;
cout << "\t Once per game, the king is allowed to do a special move called castling. " << endl;
cout << "\t Castling consists of moving the king two squares towards a rook, then placing the rook on the other side of the king, adjacent to it." << endl;
cout << "\t Castling is only permissible if all of the following conditions hold:" << endl;
cout << "\t \t The king and rook involved in castling must not have previously moved" << endl;
cout << "\t \t There must be no pieces between the king and the rook" << endl;
cout << "\t \t The king may not currently be in check, nor may the king pass through or end up in a square that is under attack by an enemy piece" << endl;
cout << "\t \t The rook is permitted to be under attack and to pass over an attacked square" << endl;
cout << "\t \t The king and the rook must be on the same rank." << endl;
cout << endl;
cout << "Rook:" << endl;
cout << "\t A rook moves any number of vacant squares in a horizontal or vertical direction." << endl;
cout << endl;
cout << "Bishop:" << endl;
cout << "\t A bishop moves any number of vacant squares in any diagonal direction." << endl;
cout << endl;
cout << "Queen:" << endl;
cout << "\t The queen moves any number of vacant squares in a horizontal, vertical, or diagonal direction." << endl;
cout << endl;
cout << "Knight:" << endl;
cout << "\t A knight moves to the nearest square not on the same rank, file, or diagonal." << endl;
cout << "\t The knight is not blocked by other pieces: it jumps to the new location." << endl;
cout << endl;
cout << "Pawn:" << endl;
cout << "\t A pawn moves straight forward one square, if that square is vacant." << endl;
cout << "\t If it has not yet moved, a pawn also has the option of moving two squares straight forward, provided both squares are vacant." << endl;
cout << "\t Pawns cannot move backwards." << endl;
cout << "\t A pawn can capture an enemy piece on either of the two squares diagonally in front of the pawn." << endl;
cout << "\t If a player advances a pawn to its eighth rank, the pawn is then promoted (converted) to a queen, rook, bishop, or knight of the same color at the choice of the player." << endl;
}
else if (chessrules == 3) {
cout << endl;
cout << "Check" << endl;
cout << endl;
cout << "A king is in check when it is under attack by at least one enemy piece." << endl;
cout << "A piece unable to move because it would place its own king in check (it is pinned against its own king) may still deliver check to the opposing player." << endl;
cout << "A player may not make any move which places or leaves his king in check." << endl;
cout << "The possible ways to get out of check are:" << endl;
cout << "\t Move the king to a square where it is not threatened" << endl;
cout << "\t Capture the threatening piece (possibly with the king)" << endl;
cout << "\t Block the check by placing a piece between the king and the opponent's threatening piece" << endl;
cout << "If nothing can be done, then the king is checkmated and the game is over." << endl;
}
else if (chessrules == 4){
}
else{
cout <<"Please enter a choice between 1-4. The choice 4 to exit" << endl;
}
}
}
else if (menuSelection == 2){
bool gameFinished = false;
chessBoard newBoard;
newBoard.getBoard();
cout << endl;
cout << "Enter player one's name: ";
cin >> playerOneName;
cout << endl;
cout << "Enter player two's name: ";
cin >> playerTwoName;
cout << endl;
cout << "\t\t\t\t-START-" << endl;
cout << endl;
newBoard.setUpBoard();
cout << "Please enter which piece you would like to move by coordinates." << endl;
cout << "Example: \"Move piece \"1a\" to \"2a\"." << endl;
while (gameFinished != true){
int rowPre;
int columnPre;
int rowAfter;
int columnAfter;
int playerNumber;
bool playerOne = true;
bool playerTwo = true;
bool moveChecker = false;
while (playerOne == true){
playerNumber = 1;
newBoard.getBoard();
string moveStringPre = "";
string moveStringAfter = "";
cout << playerOneName << " move piece ";
cin>> moveStringPre;
cout << "to ";
cin>> moveStringAfter;
moveChecker = newBoard.getMoveSpot(moveStringPre, moveStringAfter,rowPre, columnPre, rowAfter, columnAfter);
if (moveChecker == true){
playerOne = newBoard.movePiece(playerNumber, rowPre, columnPre, rowAfter, columnAfter);
}
}
if (newBoard.check() == 2){
cout << "Player 2 is in check" << endl;
}
else if (newBoard.check() == 4){
cout << "Player 2, checkmate." << endl;
}
while (playerTwo == true){
playerNumber = 2;
newBoard.getBoard();
string moveStringPre = "";
string moveStringAfter = "";
cout << playerTwoName << " move piece ";
cin>> moveStringPre;
cout << " to ";
cin>> moveStringAfter;
moveChecker = newBoard.getMoveSpot(moveStringPre, moveStringAfter, rowPre, columnPre, rowAfter, columnAfter);
if (moveChecker == true){
playerTwo = newBoard.movePiece(playerNumber, rowPre, columnPre, rowAfter, columnAfter);
}
}
if (newBoard.check() == 1){
cout << "Player 1 is in check" << endl;
}
else if (newBoard.check() == 3){
cout << "Player 1, checkmate." << endl;
}
}
newBoard.getBoard();
}
else{
cout << "Please enter a valid choice. Try again." << endl;
}
}
return 0;
}