-
Notifications
You must be signed in to change notification settings - Fork 0
/
NineAlmonds.cpp
412 lines (361 loc) · 10.6 KB
/
NineAlmonds.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
#include "stdafx.h"
#include "NineAlmonds.h"
#include "game_board.h"
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include "functions.h"
using namespace std;
NineAlmonds::NineAlmonds() : GameBase(5, 5) {
if (!GameBase::loaded_from_file) {
cout << "NineAlmonds constructor" << endl;
for (int i = 0; i < width*height; ++i)
{
board.push_back(gamepiece("empty", piece_color::no_color, " "));
}
for (int x = 3; x >= 1; x--)
{
for (int y = 1; y <= 3; y++) {
int index = width*x + y;
board[index] = almond;
}
}
}
}
void NineAlmonds::print() {
cout << *this << endl;
}
//creates starting position
void NineAlmonds::print_starting_board() {
//empty vector of empty gamepieces- essentially clears the board
for (int i = 0; i < width*height; ++i)
{
board.push_back(gamepiece("empty", piece_color::no_color, " "));
}
for (int x = 3; x >= 1; x--)
{
for (int y = 1; y <= 3; y++) {
int index = width*x + y;
board[index] = almond;
}
}
}
ostream& NineAlmonds::print_current_board(ostream &o) const {
//print out the board
int spacing = longest_piece;
ostringstream board_disp;
board_disp << left;
cout << endl;
for (int row = (height); row > 0; --row)
{
board_disp << row - 1;
for (int column = 0; column < width; ++column)
{
board_disp.width(spacing);
board_disp << board[(width* (column)+(row - 1))].piece_display;
}
board_disp << '\n';
}
//prints column axis numbers
board_disp << " ";
for (int i = 0; i < width; i++) {
board_disp.width(spacing);
board_disp << i;
}
o << board_disp.str() << endl;
return o;
}
//ostream operator for printing out board
ostream & operator<< (ostream &o,
const NineAlmonds &p)
{
return p.print_current_board(o);
}
// function done()
bool NineAlmonds::done() {
//for every position in vector gameboard, check if empty
bool check = true;
for (int i = 0; i < width*height; i++) {
if ((board[i].piece_display == "A") && (i != 12 )) {
check = false;
}
}
if (board[12].piece_display == "A" && check == true) {
cout << "Done is true" << endl;
return true;
}
else {
return false;
}
}
//function stalemate()
bool NineAlmonds::stalemate() {
// if game is done, return false
if (NineAlmonds::done()) {
return false;
}
// if there are valid moves, return false, there is a stalemate if a piece exists
// that is not adjacent to any other pieces
bool stalemate_check = true;
for (int i = 0; i < width*height; i++) {
if (board[i].piece_display == "A") {
int y = i % width;
int x = (i - y)/width;
int width_low, width_high, height_low, height_high;
for_bounds_helper(x, y, width, height, width_low, width_high, height_low, height_high);
for (int search_width = width_low; search_width < width_high; ++search_width) {
for (int search_height = height_low; search_height < height_high; ++search_height) {
if ((x == search_width) && (y == search_height)) {
continue; //skip the input coordinate without using an else statement, to avoid nested clutter
}
int curr_linear_coord = (search_width*width) + search_height;
if (board[curr_linear_coord].piece_display == "A") {
int delta_x = search_width - x;
int delta_y = search_height - y;
int check_x = x + (2*delta_x);
int check_y = y + (2*delta_y);
if (check_x >= 0 && check_x < width && check_y >= 0 && check_y < height) {
int check_lin_coord = (check_x * width) + check_y;
if(board[check_lin_coord].piece_display == " ") {
stalemate_check = false;
}
}
}
}
}
}
}
return stalemate_check;
}
bool NineAlmonds::stale_move(int x1, int y1, int x2, int y2) {
// pass the first square's parameters and second square's
int midRow = (y1 + y2) / 2;
int midCol = (x1 + x2) / 2;
//check if an almond is adjacent
if (board[y2 * width + x2].piece_display == "A") {
return false;
}
if (board[midRow * width + midCol].piece_display != "A") {
return false;
}
if (board[y1 * width + x1].piece_display != "A") {
return false;
}
//check adjacent and other separated postions
if (abs(y1 - y2) != 0 && abs(x1 - x2) != 0) {
if (abs(y1 - y2) != 2 || abs(x1 - x2) != 2) {
return false;
}
}
if (abs(y1 - y2) > 2 || abs(x1 - x2) > 2 || abs(x1 - x2) == 1 || abs(y1 - y2) == 1) {
return false;
}
return true;
}
// make a valid move function for checking valid moves and repeat logic from turn()
int NineAlmonds::valid_move(unsigned int &x, unsigned int &y, unsigned int &w, unsigned int &z) {
// cases are 1. within board range 2.
// pass the first square's parameters
unsigned int first_square = width*x + y;
unsigned int second_square = width*w + z;
int midpoint = (first_square + second_square) / 2;
if (x < 5 && y < 5 && w < 5 && z < 5) {
if (board[second_square].piece_display == " " && board[first_square].piece_display == "A") {
if (board[midpoint].piece_display == "A") {
if (second_square == (first_square + 2)) {
return (int)result::success;
}
else if (second_square == (first_square - 2)) {
return (int)result::success;
}
else if (second_square == (first_square + 8)) {
return (int)result::success;
}
else if (second_square == (first_square - 8)) {
return (int)result::success;
}
else if (second_square == (first_square + 10)) {
return (int)result::success;
}
else if (second_square == (first_square - 10)) {
return (int)result::success;
}
else if (second_square == (first_square + 12)) {
return (int)result::success;
}
else if (second_square == (first_square - 12)) {
return (int)result::success;
}
}
else {
return (int)result::no_valid_move;
}
}
else {
return (int)result::no_valid_move;
}
}
else {
//square is out of board range
return (int)result::square_out;
}
return (int)result::no_valid_move;
}
int NineAlmonds::prompt(unsigned int &horizontal, unsigned int &vertical) {
string input;
string comma = ",";
unsigned int a = 0, b = 0;
while (true) {
cout << "Please make a valid move 'x,y' or enter 'quit'" << endl;
//use getline
//Sometimes a blank line is entered, if so, getline again
getline(cin, input);
if (input.empty()) {
getline(cin, input);
}
size_t found = input.find(comma);
if (input == "quit") {
return (int)result::user_quit;
}
string space = " ";
replace(input.begin(), input.end(), ',', ' ');
istringstream strm(input);
if (strm >> a) {
if (strm >> b) {
//assign stream inputs to passed parameters
horizontal = a;
vertical = b;
return (int)result::success;
}
}
}
// not all control paths return a value in order to force user to enter a valid input
}
int NineAlmonds::turn() {
// a and b are starting coordinates and d and e are ending coordinates
unsigned int a = 0, b = 0, c = 0, d = 0;
unsigned int &f_x = a, &f_y = b, &s_x = c, &s_y = d;
vector<int> move_list;
bool move_ok = false;
// FIRST MOVE
// check if valid
while (!move_ok) {
//call prompt the first time
int prompt_return = prompt(f_x, f_y);
// if return is 0 then it is successful
if (prompt_return == 0) {
std::cout << "You entered: " << f_x << " " << f_y << endl;
}
else if (prompt_return == (int)result::user_quit) {
return (int)result::user_quit;
}
// call prompt second time for ending coordinates
prompt_return = prompt(s_x, s_y);
if (prompt_return == 0) {
std::cout << "You entered: " << s_x << " " << s_y << endl;
}
else if (prompt_return == (int)result::user_quit) {
return (int)result::user_quit;
}
if (prompt_return != (int)result::user_quit) {
int first_square = f_x*width + f_y;
int second_square = s_x*width + s_y;
int check_valid = (valid_move(f_x, f_y, s_x, s_y));
int midpoint = (first_square + second_square) / 2;
if (check_valid == (int)result::success) {
move_list.push_back(a), move_list.push_back(b), move_list.push_back(c), move_list.push_back(d);
board[first_square] = empty;
board[second_square] = almond;
board[midpoint] = empty;
std::cout << "Turn Count: " << turn_counter << endl;
if (stalemate()) {
return (int)result::stalemate;
}
move_ok = true;
}
}
else {
cout << "Invalid coordinates, please try again" << endl;
}
}
std::cout << *this << endl;
std::cout << '\n' << flush;
//print statement for moves
std::cout << f_x << "," << f_y << " " << "to " << s_x << "," << s_y << endl;
while (true) {
std::cout << "Continue to move this piece y/n or quit to end game" << endl;
string response;
//std::cin >> response;
getline(std::cin, response);
if (response == "n") {
return (int)result::switch_piece;
}
else if (response == "quit") {
return (int)result::user_quit;
}
else if (response == "y") {
break;
}
}
// set to second square
f_x = s_x;
f_y = s_y;
//MOVE CONTINUATION
bool move_continuation = false;
while (!move_continuation) {
//if (response == "y") {
std::cout << "Enter coordinates for second square" << endl;
bool check_move = false;
while (!check_move) {
//prompt for second coordinates
int prompt_return1 = prompt(s_x, s_y);
if (prompt_return1 == 0) {
std::cout << "You entered: " << s_x << " " << s_y << endl;
}
else if (prompt_return1 == (int)result::user_quit) {
return (int)result::user_quit;
}
int check_valid = (valid_move(f_x, f_y, s_x, s_y));
if (check_valid == (int)result::success) {
std::cout << "Valid Move " << endl;
int first_square = f_x*width + f_y;
int second_square = s_x*width + s_y;
int midpoint = (first_square + second_square) / 2;
board[first_square] = empty;
board[midpoint] = empty;
board[second_square] = almond;
std::cout << *this << endl;
check_move = true;
//print out move list
move_list.push_back(s_x), move_list.push_back(s_y);
for (unsigned int i = 0; i < move_list.size() - 2; i = i + 2) {
std::cout << move_list[i] << ", " << move_list[i + 1] << " to " << flush;
}
std::cout << move_list[move_list.size() - 2] << ", " << move_list[move_list.size() - 1] << endl;
std::cout << "Turn Count: " << turn_counter << endl;
f_x = s_x, f_y = s_y;
if (stalemate()) {
return (int)result::stalemate;
}
}
else if ((check_valid == -7) || (check_valid == -8)) {
std::cout << "Error: Invalid Coordinates" << endl;
}
}
std::cout << "Continue to move this piece y/n or quit to end game" << endl;
string response2;
std::cin >> response2;
if (response2 == "quit") {
return (int)result::user_quit;
}
else if (response2 == "n") {
move_continuation = true;
turn_counter++;
}
//}
}
std::cout << "User chooses another piece" << endl;
return (int)result::switch_piece;
}