-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurn.cpp
More file actions
161 lines (140 loc) · 3.68 KB
/
Copy pathTurn.cpp
File metadata and controls
161 lines (140 loc) · 3.68 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
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Turn {
public:
string get_user_input();
bool valid_cell(int, int, int);
void toggle_flag(char**, int**, int, int, int);
int click_cell(char**, int**, int, int, int);
void recursive_reveal(char**, int**, int, int, int);
~Turn();
private:
};
Turn::~Turn(){
}
/*
* function name: get_user_input
* description: gets the users input
*/
string Turn::get_user_input(){
cout << ">> ";
string user_input;
cin >> user_input;
cin.ignore(256, '\n');
return user_input;
}
/*
* function name: valid_cell
* description: checks if the coordinates are within the board boundaries
*/
bool Turn::valid_cell(int x, int y, int size){
if (x >= 0 and x <= (size-1) and y >= 0 and y <= (size-1)){
return true;
}
return false;
}
/*
* function name: toggle_flag
* description: toggles a cell to display F for flag
*/
void Turn::toggle_flag(char** user_board, int** board, int size, int x, int y){
if (valid_cell(x, y, size)){
if (user_board[x][y] == 'F'){
user_board[x][y] = ' ';
}
else if (user_board[x][y] != ' '){
cout << "Cannot flag here... " << endl;
sleep(1);
}
else {
user_board[x][y] = 'F';
}
}
else {
cout << "Invalid coordinate... " << endl;
sleep(1);
}
}
/*
* function name: click_cell
* description: clicks a cell, triggering a possible chain of revelations across the board
*/
int Turn::click_cell(char** user_board, int** board, int size, int x, int y){
if (valid_cell(x, y, size)){
if (board[x][y] == 9){
return 1;
}
else if (board[x][y] >= 1 and board[x][y] <= 8){
char a = '0' + board[x][y];
user_board[x][y] = a;
return 0;
}
else {
// begin recursion
recursive_reveal(user_board, board, size, x, y);
return 0;
}
}
else {
cout << "Invalid coordinate... " << endl;
sleep(1);
}
return 0;
}
/*
* function name: recursive_reveal
* description: chain of revelations begins recursively
*/
void Turn::recursive_reveal(char** user_board, int** board, int size, int x, int y){
if (user_board[x][y] == 'F'){
return;
}
if (board[x][y] == 0){
user_board[x][y] = '*';
board[x][y] = -1;
}
else if (board[x][y] == -1){
return;
}
else {
char a = '0' + board[x][y];
user_board[x][y] = a;
return;
}
// Recurse on all 8 surrounding cells
// Up left
if (valid_cell(x-1, y-1, size) and (board[x-1][y-1] != 0)){
recursive_reveal(user_board, board, size, x-1, y-1);
}
// Up
if (valid_cell(x, y-1, size)){
recursive_reveal(user_board, board, size, x, y-1);
}
// Up right
if (valid_cell(x+1, y-1, size) and (board[x+1][y-1] != 0)){
recursive_reveal(user_board, board, size, x+1, y-1);
}
// Left
if (valid_cell(x-1, y, size)){
recursive_reveal(user_board, board, size, x-1, y);
}
// Right
if (valid_cell(x+1, y, size)){
recursive_reveal(user_board, board, size, x+1, y);
}
// Down Left
if (valid_cell(x-1, y+1, size) and (board[x-1][y+1] != 0)){
recursive_reveal(user_board, board, size, x-1, y+1);
}
// Down
if (valid_cell(x, y+1, size)){
recursive_reveal(user_board, board, size, x, y+1);
}
// Down right
if (valid_cell(x+1, y+1, size) and (board[x+1][y+1] != 0)){
recursive_reveal(user_board, board, size, x+1, y+1);
}
return;
}