-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntStep.cpp
More file actions
223 lines (200 loc) · 8.3 KB
/
Copy pathAntStep.cpp
File metadata and controls
223 lines (200 loc) · 8.3 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
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
/****************************************************************************************************
* * Program name: CS162 Project1
* * Author: Taekyoung Kim
* * Date: 01/10/2019
* * Description: This class function track ant's steps checking the board color and direction.
* * Then, show the steps step by step with black with #, ant with *.
***************************************************************************************************/
#include "AntStep.h"
#include <iostream>
//constructor
AntStep::AntStep(char **a, int r, int c, int s, int sizeOfBR, int sizeOfBC, Direction di, BColor co){
arr = a;
rowAnt = r;
colAnt = c;
numOfSteps = s;
sizeOfBoardRow = sizeOfBR;
sizeOfBoardCol = sizeOfBC;
direct = di;
color =co;
}
//Destructor
AntStep::~AntStep() = default;
//Accessors to get the direction.
Direction AntStep::getDirection(){
return direct;
}
//Modifier for the direction.
void AntStep::setDirection(Direction dir){
direct = dir;
}
//Accessors to get the board color.
BColor AntStep::getBColor(){
return color;
}
//Modifier for the board color
void AntStep::setBColor(BColor co){
color = co;
}
//Accessors to get the ant location in the Row.
int AntStep::getRowLocate(){
return rowAnt;
}
//Modifier for the ant location in the Row.
void AntStep::setRowLocate(int rowA){
rowAnt = rowA;
}
//Accessors to get the ant location in the column.
int AntStep::getColLocate(){
return colAnt;
}
//Modifier for the ant location in the column.
void AntStep::setColLocate(int colA){
colAnt = colA;
}
//Function to track the ant location and direction and the board color.
void AntStep::antLocate() {
for (int i = 0; i < numOfSteps; i++) {
int preRowSteps[numOfSteps-1];
int preColSteps[numOfSteps-1];
preRowSteps[i] = getRowLocate();
preColSteps[i] = getColLocate();
//Track the cell that ant stayed and return to the original color.
if (i > 0) {
if (getBColor() == W_ANT) {
arr[preRowSteps[i-1]][preColSteps[i-1]] = ' ';
setBColor(WHITE);
}
else if (getBColor() == B_ANT) {
arr[preRowSteps[i-1]][preColSteps[i-1]] = '#';
setBColor(BLACK);
}
}
//Show the current step number, current location, and direction.
std::cout<<std::endl;
std::cout<<i+1<<": the Ant step number"<<std::endl;
std::cout<<"Current Ant Direction: "<<getDirection()<<std::endl;
std::cout <<"Current Ant location ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
//When the cell color is white.
if (arr[getRowLocate()][getColLocate()] == ' ') {
arr[getRowLocate()][getColLocate()] = '*';
std::cout << "The cell color is White." << std::endl;
std::cout<<std::endl;
//Change the board color to black with ant so that we can track the cell.
setBColor(B_ANT);
//Go to East when the direction is north if the cell color is white.
if (getDirection() == NORTH) {
setDirection(EAST);
if (getColLocate() < sizeOfBoardCol-1) {
setColLocate(getColLocate() + 1);
}
else {
setColLocate(0);
}
std::cout <<"New Ant location when North ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when North: "<<getDirection()<<std::endl;
}
//Go to West when the direction is South if the cell color is white.
else if (getDirection() == SOUTH) {
setDirection(WEST);
if (getColLocate() > 0) {
setColLocate(getColLocate() - 1);
}
else {
setColLocate(sizeOfBoardCol-1);
}
std::cout <<"New Ant location when South: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction When South: "<<getDirection()<<std::endl;
}
//Go to South when the direction is East if the cell color is white.
else if (getDirection() == EAST) {
setDirection(SOUTH);
if (getRowLocate() < sizeOfBoardRow -1) {
setRowLocate(getRowLocate()+ 1);
}
else {
setRowLocate(0);
}
std::cout <<"New Ant location when East: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when East: "<<getDirection()<<std::endl;
}
//Go to North when the direction is West if the cell color is white.
else if(getDirection() == WEST) {
setDirection(NORTH);
if (getRowLocate() > 0) {
setRowLocate(getRowLocate() - 1);
}
else {
setRowLocate(sizeOfBoardRow-1);
}
std::cout <<"New Ant location when West: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when West: "<<getDirection()<<std::endl;
}
}
//When the cell color is black.
else if (arr[getRowLocate()][getColLocate()]== '#') {
arr[getRowLocate()][getColLocate()] = '*';
std::cout << "The cell color is Black." << std::endl;
std::cout<<std::endl;
//Change the board color to white with ant so that we can track the cell.
setBColor(W_ANT);
//Go to West when the direction is North if the cell color is Black.
if (getDirection() == NORTH) {
setDirection(WEST);
if (getColLocate() > 0) {
setColLocate(getColLocate() - 1);
}
else {
setColLocate(sizeOfBoardCol-1);
}
std::cout <<"New Ant location when North: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when North: "<<getDirection()<<std::endl;
}
//Go to Eest when the direction is South if the cell color is Black.
else if (getDirection() == SOUTH) {
setDirection(EAST);
if (getColLocate() < sizeOfBoardCol -1) {
setColLocate(getColLocate() + 1);
}
else {
setColLocate(0);
}
std::cout <<"New Ant location when South: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when South: "<<getDirection()<<std::endl;
}
//Go to North when the direction is East if the cell color is Black.
else if (getDirection() == EAST) {
setDirection(NORTH);
if (getRowLocate() > 0) {
setRowLocate(getRowLocate()-1);
}
else {
setRowLocate(sizeOfBoardRow-1);
}
std::cout <<"New Ant location when East: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when East: "<<getDirection()<<std::endl;
}
//Go to South when the direction is West if the cell color is Black.
else if (getDirection() == WEST) {
setDirection(SOUTH);
if (getRowLocate() < sizeOfBoardRow -1) {
setRowLocate(getRowLocate() + 1);
}
else {
setRowLocate(0);
}
std::cout <<"New Ant location when West: ["<<getRowLocate()<<" , " <<getColLocate()<<"]"<<std::endl;
std::cout<<"New Ant Direction when West: "<<getDirection()<<std::endl;
}
}
//To print the ant's step in the screen.
std::cout<<"Below is the board to show the Ant steps: " <<std::endl;
std::cout<<std::endl;
for (int row =0; row <sizeOfBoardRow; row++) {
for (int col=0; col <sizeOfBoardCol; col++) {
std::cout<<arr[row][col]<<" ";
}
std::cout<<std::endl;
}
}
}