-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnt.cpp
427 lines (320 loc) · 9.01 KB
/
Ant.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*********************************************************************
** Author: Karen Berba
** Date: 1/13/19
** Description:
Ant.cpp
Ant Class:
The Ant class keeps track of the ant's location (x and y coordinates) and
direction (N, E, S, W), as well as contains the function to move the ant on
the board. The Ant class also builds the board using a dynamic 2D
array, and contains board functions to display and initialize the board.
*********************************************************************/
#include "Ant.hpp"
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setw;
/*********************************************************************
**
SETTERS
Description:
Setters for xCoord, yCoord, color, rowSize, and columnSize.
Arguments:
x-coordinate integer, y-coordinate integer, row integer, column integer,
space color character, row size integer, and column size integer.
Restrictions:
Only integers except for setColor, which needs two integer values for row and
column, and a character for space color.
Returns:
Does not return anything.
*********************************************************************/
void Ant::setXCoord(int xc) {
xCoord = xc;
}
void Ant::setYCoord(int yc) {
yCoord = yc;
}
void Ant::setColor(int r, int c, char spaceColor) {
matrix[r][c] = spaceColor;
color = matrix[r][c];
}
void Ant::setRows(int r) {
rowSize = r;
}
void Ant::setColumns(int c) {
columnSize = c;
}
/*********************************************************************
**
GETTERS
Description:
Getters for antSign, xCoord, yCoord, color, rowSize, columnSize, antDirection,
and matrix.
Arguments:
No arguments for all except getColor, which takes in two integers for row and column.
Restrictions:
Only restrictions are for getColor, which must be integers.
Returns:
Returns antSign, xCoord, yCoord, color, rowSize, columnSize, antDirection,
and matrix.
*********************************************************************/
char Ant::getAntSign() {
return antSign;
}
int Ant::getXCoord() {
return xCoord;
}
int Ant::getYCoord() {
return yCoord;
}
char Ant::getColor(int r, int c) {
color = matrix[r][c];
return color;
}
int Ant::getRows() {
return rowSize;
}
int Ant::getColumns() {
return columnSize;
}
Direction Ant::getAntDirection() {
return antDirection;
}
char** Ant::getBoard() {
return matrix;
}
/*********************************************************************
**
CONSTRUCTOR
Ant::Ant(int x, int y, int rows, int columns)
Description:
Creates an Ant object. Dynamically allocates memory space for the board (matrix).
Sets the ant's starting coordinates, sign, and initial direction.
Arguments:
x-coordinate, y-coordinate, number of rows, number of columns.
Restrictions:
Only takes in integers.
Returns:
Does not return anything.
*********************************************************************/
Ant::Ant(int x, int y, int rows, int columns) {
// set rows and columns
rowSize = rows;
columnSize = columns;
// dynamically allocating 2D array
/* source(s):
https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
*/
matrix = new char*[rowSize]; // rows
for (int i = 0; i < rowSize; i++) {
matrix[i] = new char[columnSize]; // columns
}
// fill the initial board with white spaces
initialBoard(rowSize, columnSize);
// set ant's starting coordinates
setXCoord(x);
setYCoord(y);
// ant's character sign
antSign = '*';
// set ant's initial direction
antDirection = NORTH;
}
/*********************************************************************
**
void Ant::initialBoard(int numOfRows, int numOfColumns)
Description:
Fills the initial board with white spaces.
Arguments:
Number of rows and number of columns.
Restrictions:
Only takes in integers.
Returns:
Does not return anything.
*********************************************************************/
void Ant::initialBoard(int numOfRows, int numOfColumns) {
// fills the initial board with white spaces
for (int aRow = 0; aRow < numOfRows; aRow++) {
for (int aCol = 0; aCol < numOfColumns; aCol++) {
matrix[aRow][aCol] = ' ';
}
cout << endl;
}
}
/*********************************************************************
**
void Ant::displayBoard()
Description:
Displays the board, and prints '*' for the ant's current location.
Arguments:
No arguments.
Restrictions:
No restrictions.
Returns:
Does not return anything.
*********************************************************************/
/*
source(s):
C++ Early Objects 9th Edition (Gaddis, Walters, Muganda)
Chapter 8.10 - Program 8-22 (page 557)
http://www.cplusplus.com/forum/beginner/81437/
*/
void Ant::displayBoard() {
int rows = getRows();
int columns = getColumns();
// iterates through the board
for (int aRow = 0; aRow < rows; aRow++) {
cout << '|'; // forms outside border
for (int aColumn = 0; aColumn < columns; aColumn++) {
// if ant is located at these coordinates, print '*'
if (xCoord == aRow && yCoord == aColumn) {
cout << setw(1) << '*' << " ";
} else {
// else, just print the current space (e.g. ' ' or '#')
cout << setw(1) << matrix[aRow][aColumn] << " ";
}
}
cout << setw(1) << '|'; // forms outside border
cout << endl;
}
cout << endl;
}
/*********************************************************************
**
void Ant::moveAnt(char** matrix)
Description:
Moves ant based on current direction faced. If at the edge, skips the forward
step, makes another turn and then continues on.
Arguments:
Current direction (enum).
Restrictions:
Only takes in an enum value.
Returns:
Does not return anything.
*********************************************************************/
void Ant::moveAnt(Direction currDirect) {
int sumX;
int sumY;
// moves ant forward based on current direction faced
switch(currDirect) {
case NORTH:
sumX = getXCoord() - 1;
sumY = getYCoord();
// if at the edge, skips the forward step, makes another turn and then continues on
if (sumX < 0) {
sumX += 1; // adds 1 to bring ant back to the board
antDirection = EAST; // changes direction to East
}
// set ant's location
setXCoord(sumX);
setYCoord(sumY);
break;
case EAST:
sumX = getXCoord();
sumY = getYCoord() + 1;
// if at the edge, skips the forward step, makes another turn and then continues on
if (sumY > (columnSize - 1)) {
sumY -= 1; // subtracts 1 to bring ant back to the board
antDirection = WEST; // changes direction to West
}
// set ant's location
setXCoord(sumX);
setYCoord(sumY);
break;
case SOUTH:
sumX = getXCoord() + 1;
sumY = getYCoord();
// if at the edge, skips the forward step, makes another turn and then continues on
if (sumX > (rowSize - 1)) {
sumX -= 1; // subtracts 1 to bring ant back to the board
antDirection = NORTH;
}
// set ant's location
setXCoord(sumX);
setYCoord(sumY);
break;
case WEST:
sumX = getXCoord();
sumY = getYCoord() - 1;
// if at the edge, skips the forward step, makes another turn and then continues on
if (sumY < 0) {
sumY += 1;
antDirection = EAST;
}
// set ant's location
setXCoord(sumX);
setYCoord(sumY);
break;
}
// displays the board after move is made
displayBoard();
}
/*********************************************************************
**
void Ant::whiteTurnRight()
Description:
Changes the direction of the ant to the right.
Arguments:
No arguments.
Restrictions:
No restrictions.
Returns:
Does not return anything.
*********************************************************************/
/*
source(s):
https://stackoverflow.com/questions/3019153/how-do-i-use-an-enum-value-on-a-switch-statement-in-c
*/
void Ant::whiteTurnRight() {
// changes the direction of the ant to the right
switch(antDirection) {
case NORTH:
antDirection = EAST;
break;
case EAST:
antDirection = SOUTH;
break;
case SOUTH:
antDirection = WEST;
break;
case WEST:
antDirection = NORTH;
break;
}
}
/*********************************************************************
**
void Ant::blackTurnLeft()
Description:
Changes the direction of the ant to the left.
Arguments:
No arguments.
Restrictions:
No restrictions.
Returns:
Does not return anything.
*********************************************************************/
/*
source(s):
https://stackoverflow.com/questions/3019153/how-do-i-use-an-enum-value-on-a-switch-statement-in-c
*/
void Ant::blackTurnLeft() {
// changes the direction of the ant to the left
switch(antDirection) {
case NORTH:
antDirection = WEST;
break;
case WEST:
antDirection = SOUTH;
break;
case SOUTH:
antDirection = EAST;
break;
case EAST:
antDirection = NORTH;
break;
}
}