-
Notifications
You must be signed in to change notification settings - Fork 4
/
Group4Utils.cpp
260 lines (233 loc) · 7.54 KB
/
Group4Utils.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
/******************************************************************************
** Author: Winter 162 400 Group 4
** Date: 02/04/2019
** Description: This is an implementation of utility functions. These functions
** help to validate integers for user selections, generation of
** random numbers, and functions to display and initialize the
** game board and Critters.
******************************************************************************/
#include "Group4Utils.hpp"
#include "Critter.hpp"
#include <random>
#include <iostream>
#include <string>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
using std::stringstream;
/*********************************************************************
** randIntRange(int,int)
** Description: Generates a random number from a uniform distribution
** in the range [min,max] with a static seed.
*********************************************************************/
int randIntRange(int min, int max)
{
static std::random_device rd;
static std::mt19937 mTwist(rd());
std::uniform_int_distribution<> dist(min, max);
return dist(mTwist);
}
/*********************************************************************
** validSelection()
** Description: Validates the user input is the integer 1 or 2.
*********************************************************************/
int validSelection()
{
string selection = "";
int value;
bool cont = true;
//Error output
string error = "Invalid input. Please enter a valid number.";
while (cont)
{
//validating string of user input using string stream
//work cited: https://www.youtube.com/watch?v=S3_jCTb3fm0
getline(cin, selection);
stringstream ss(selection);
//if more than one value inputted, error
if(selection.size()!=1)
{
cout << error << "\n";
}
//if selection is between 1 and 2 validation is success
else if ((ss >> value) && (value == 1 || value ==2))
{
cont = false;
cout << "\n";
}
//if input is empty, error
else if (selection.empty())
{
cout << error << "\n";
}
//fail all other inputs
else
{
cout << error << "\n";
}
}
return value;
}
/*********************************************************************
** validInt()
** Description: Validates the user input is an integer > 0
*********************************************************************/
int validInt()
{
string input = "";
bool cont = true;
int value;
string error = "Invalid input. Please enter an integer greater than 0.";
while (cont)
{
//Get user input
getline(cin, input);
stringstream ss(input);
//Try to put input into an integer variable
ss >> value;
//If input is non integer or <=0, error
if (value <= 0)
{
cout << error << "\n";
}
//If input empty, error
else if (input.empty())
{
cout << error << "\n";
}
//If input contains decimal, error
else if(input.find('.')!=string::npos)
{
cout << error << "\n";
}
else
{
cont = false;
}
}
return value;
}
/*********************************************************************
** validNonNegative()
** Description: Validates the user input is non-negative
*********************************************************************/
int validNonNegative()
{
string input = "";
bool cont = true;
int value = 0;
string error = "Invalid input. Please enter non-negative integer";
while (cont)
{
//Get user input
getline(cin, input);
stringstream ss(input);
//Try to put input into an int variable
ss >> value;
//if input is non integer or <=0, error
if (value < 0)
{
cout << error << "\n";
}
//if input empty, error
else if (input.empty())
{
cout << error << "\n";
}
//if input contains decimal, error
else if(input.find('.')!=string::npos)
{
cout << error << "\n";
}
else
{
return value;
}
}
return value;
}
/******************************************************************************
** void printBoard(Critter***, int size_x, int size_y)
** Description: This function takes a triple-pointer to a critter object in
** order to view the board with ants and doodlebugs. The variable
** size_x represents the number of rows, and size_y represents the
** the number of columns of the board. It prints the board.
******************************************************************************/
void printBoard(Critter*** board, int size_x,int size_y)
{
cout << "\n" << string(size_y + 2, '=') << "\n";
for(int i=0; i < size_x; i++)
{
cout << "|";
for(int j=0; j < size_y; j++)
{
if(board[i][j] == NULL)
{
cout << " ";
}
else if(board[i][j]->getSymbol()=='X')
{
cout << "X";
}
else if(board[i][j]->getSymbol()=='O')
{
cout << "O";
}
}
cout << "|\n";
}
cout << string(size_y + 2, '=') << "\n";
}
/******************************************************************************
** void placeDoodles(Critter***, int&, int, int, int);
** Description: The placeDoodles function adds starting Doodlebugs to the board.
** It takes a triple-pointer to a critter object to add critters
** to the board. Furthermore this function takes a reference to int
** doodCount that serves as a counter for the number of Doodlebugs,
** and int numDoodbugs that represents the number of Doodlebugs to
** be created, and and 2 ints (size_x, size_y) representing the
** side of the board.
******************************************************************************/
void placeDoodles(Critter*** board, int &doodCount, int numDoodbugs, int size_x, int size_y)
{
while (doodCount < numDoodbugs)
{
//Generate random row and column places
int doodRowPlace = randIntRange(0, size_x-1);
int doodColPlace = randIntRange(0, size_y-1);
//If the randomly generated x and y places result in an empty cell, add a Doodlebug
if(board[doodRowPlace][doodColPlace]==NULL)
{
board[doodRowPlace][doodColPlace] = new Doodlebug(doodRowPlace, doodColPlace);
//Update Doodlebug count
doodCount++;
}
}
}
/******************************************************************************
** void placeAnts(Critter***, int&, int, int, int);
** Description: The placeAnts function adds starting Ants to the board. It takes
** a triple-pointer to a critter object to add critters to the
** board. Furthermore this function takes a reference to int
** antCount that serves as a counter for the number of Ants, and
** int numAnts that represents the number of Ants to be created,
** and and 2 ints (size_x, size_y) representing the
** side of the board.
******************************************************************************/
void placeAnts(Critter*** board, int& antCount, int numAnts, int size_x, int size_y)
{
while (antCount < numAnts)
{
//Generate random row and column places
int antRowPlace = randIntRange(0, size_x-1);
int antColPlace = randIntRange(0, size_y-1);
//If the randomly generated x and y places result in an empty cell, add an Ant
if(board[antRowPlace][antColPlace]==NULL)
{
board[antRowPlace][antColPlace] = new Ant(antRowPlace, antColPlace);
//Update Ant count
antCount++;
}
}
}