-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctsForMain.cpp
266 lines (232 loc) · 8.33 KB
/
functsForMain.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// Created by tjensen on 6/9/2020.
//
#include "functsForMain.h"
/**
* Creates the board to start the game.
* @param board a 2-D vector of Pieces pointers to represent the board
*/
void InitializeBoard(vector<vector <Pieces*>> &board) {
//Like it says, this sets up the board at the beginning of the game.
for (vector<Pieces*> &row : board) {
row.resize(7);
//row.assign(7, new Pieces()); <- I have no idea why this doesn't work for filling the board with Pieces.
}
for (vector<Pieces*> &row : board) {
for (Pieces* &piece : row) {
piece = new Pieces();
}
}
//for accessing specific elements, first .at is the y axis and second .at is the x axis
//Defenders first
delete board[3][2];
board[3][2] = new Defenders();
delete board[2][3];
board[2][3] = new Defenders();
delete board[3][4];
board[3][4] = new Defenders();
delete board[4][3];
board[4][3] = new Defenders();
delete board[3][3];
board[3][3] = new King();
//Attackers
delete board[3][0];
board[3][0] = new Attackers();
delete board[3][1];
board[3][1] = new Attackers();
delete board[3][5];
board[3][5] = new Attackers();
delete board[3][6];
board[3][6] = new Attackers();
delete board[0][3];
board[0][3] = new Attackers();
delete board[1][3];
board[1][3] = new Attackers();
delete board[5][3];
board[5][3] = new Attackers();
delete board[6][3];
board[6][3] = new Attackers();
DisplayBoard(board);
}
/**
* Displays the board to cout.
* @param board the board to display.
*/
void DisplayBoard(vector<vector <Pieces*>> &board) {
short yAxis = 0;
//display coordinate key above the board
cout << " 0 1 2 3 4 5 6 " << endl;
//this loop puts a coordinate key on either side of the board
for (vector<Pieces*> &row : board) {
cout << yAxis;
for (Pieces* &piece : row) {
cout << piece->GetName();
}
cout << yAxis << endl;
yAxis += 1;
}
//display coordinate key below the board
cout << " 0 1 2 3 4 5 6 " << endl;
cout << endl;
}
/**
* Function for movement along the x-axis.
* @param board a reference to the board.
* @param done tracks whether the game is finished
* @param isAttacker indicates which player's turn it is. true = Attacker's turn, false = Defender's turn
*/
void MoveXAxis(vector<vector <Pieces*>> &board, bool &done, bool &isAttacker) {
short x;
short x2;
short y;
short i;
//the ready boolean tells the game to pass the turn to the next player
bool ready = false;
Pieces* temp;
while (!ready) {
cout << "Enter the coordinates of the piece you wish to move.\nX-value:" << endl;
cin >> x;
if (x >= 7 || x < 0) continue; //restarts the loop
cout << "Y-value:" << endl;
cin >> y;
if (y >= 7 || y < 0) continue; //restarts the loop
if (isAttacker) {
if (board.at(y).at(x)->GetName() != " A ") {
cout << "Invalid piece. Please try again." << endl;
continue;
}
}
else {
if ((board.at(y).at(x)->GetName() != " D ") && (board.at(y).at(x)->GetName() != " K ")) {
cout << "Invalid piece. Please try again." << endl;
continue;
}
}
cout << board.at(y).at(x)->GetName() << " selected. " << endl;
cout << "Enter the X-value this piece is moving to, or type '7' to reset your turn:" << endl;
cin >> x2;
//The next two loops check if there's a piece in the way
if (x2 < x) {
for (i = x2; i < x; ++i) {
if (board.at(y).at(i)->GetName() != " - ") {
cout << "You cannot move through another piece. Please make another choice." << endl;
cin >> x2;
break;
}
}
}
else if (x2 < 7) {
for (i = x + 1; i < (x2 + 1); ++i) {
if (board.at(y).at(i)->GetName() != " - ") {
cout << "You cannot move through another piece. Please make another choice."<< endl;
cin >> x2;
break;
}
}
}
ready = (x2 != 7); //if the user entered 7, restart the process without passing the turn
}
temp = board.at(y).at(x2);
board.at(y).at(x2) = board.at(y).at(x);
board.at(y).at(x) = temp;
//is this part needed since temp will soon move out of scope?
temp = nullptr;
delete temp;
CheckIfCaptured(board, y, x2, done);
DisplayBoard(board);
}
/**
* Function for movement along the y-axis.
* @param board a reference to the board.
* @param done tracks whether the game is finished
* @param turnTracker indicates which player's turn it is. true = Attacker's turn, false = Defender's turn
*/
void MoveYAxis(vector<vector<Pieces *>> &board, bool &done, bool &isAttacker) {
short x;
short y;
short y2;
short i;
bool ready = false;
Pieces* temp;
while (!ready) {
cout << "Enter the coordinates of the piece you wish to move.\nX-value:" << endl;
cin >> x;
if (x >= 7 || x < 0) continue; //restarts the loop
cout << "Y-value:" << endl;
cin >> y;
if (y >= 7 || y < 0) continue; //restarts the loop
if (isAttacker) {
if (board.at(y).at(x)->GetName() != " A ") {
cout << "Invalid piece. Please try again." << endl;
continue;
}
}
else {
if ((board.at(y).at(x)->GetName() != " D ") && (board.at(y).at(x)->GetName() != " K ")) {
cout << "Invalid piece. Please try again." << endl;
continue;
}
}
cout << board.at(y).at(x)->GetName() << " selected. " << endl;
cout << "Enter the Y-value this piece is moving to, or type '7' to reset your turn:" << endl;
cin >> y2;
//check if there's a piece in the way
if (y2 < y) {
for (i = y2; i < y; ++i) {
if (board.at(i).at(x)->GetName() != " - ") {
cout << "You cannot move through another piece. Please make another choice." << endl;
cin >> y2;
break;
}
}
}
else if (y2 < 7) {
for (i = y + 1; i < (y2 + 1); ++i) {
if (board.at(i).at(x)->GetName() != " - ") {
cout << "You cannot move through another piece. Please make another choice."<< endl;
cin >> y2;
break;
}
}
}
//remember that if a person inputs 7, their turn restarts
ready = (y2 != 7);
}
temp = board.at(y2).at(x);
board.at(y2).at(x) = board.at(y).at(x);
board.at(y).at(x) = temp;
//is this part needed?
temp = nullptr;
delete temp;
CheckIfCaptured(board, y2, x, done);
DisplayBoard(board);
}
/**
* Checks if pieces around the moved piece have been captured.
* @param board reference to the board object.
* @param y the y-coordinate of the moved piece.
* @param x the x-coordinate of the moved piece.
* @param done a boolean to check if the game is done.
*/
void CheckIfCaptured(vector<vector<Pieces *>> &board, short y, short x, bool &done) {
//after movement, checks if any of the surrounding pieces have been captured.
short i;
//checks for capture along the x-axis of the moved piece, being careful to ignore numbers beyond board's indices.
for (i = x - 1; i <= (x + 1); ++i) {
if ((i < 0) || (i > 6)) {
continue;
}
else { //This is not a recursive call, it invokes the member function of the same name.
board.at(y).at(i)->CheckIfCaptured(board, y, i, done);
}
}
//check for capture along the piece's y-axis.
for (i = y - 1; i <= (y + 1); ++i) {
if ((i < 0) || (i > 6)) {
continue;
}
else {
board.at(i).at(x)->CheckIfCaptured(board, i, x, done);
}
}
}