-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.cpp
More file actions
182 lines (171 loc) · 5.16 KB
/
Copy pathRobot.cpp
File metadata and controls
182 lines (171 loc) · 5.16 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
/*
@file Robot.cpp
@name Chris Steigerwald CSS 342A
Description: the Robot.cpp classes implements the Robot.h. Included are
constructors, public and private methods.
Assumptions: that user knows how to enter data correctly (there is no
error checking) and that amount of unique routes does not exceed
the number of characters that can be held in a string, and that
user will not enter coordinates sufficiently far apart that cause a
stack overflow.
*/
#include "Robot.h"
/*
****************** Class Constructors ***************************
*/
// Robot class default constructor
Robot::Robot()
{
startX = 0;
startY = 0;
goalX = 0;
goalY = 0;
count = 0;
ptr = &path;
ptrCount = &count;
CheckInput(startX, startY, goalX, goalY, ptrCount, answer, ptr);
} // end constructor
// Robot class override constructor
Robot::Robot(int x1, int y1, int x2, int y2)
{
startX = x1;
startY = y1;
goalX = x2;
goalY = y2;
count = 0;
ptr = &path;
ptrCount = &count;
CheckInput(startX, startY, goalX, goalY, ptrCount, answer, ptr);
} // end constructor
// Robot class destructor
Robot::~Robot()
{
} // end destructor
/*
******************** Class: Private Methods *********************
*/
// Validates input, if start and stop coordinates are equal returns 0 paths, else
// if start and stop coordinates are not equal passes all parameters to FindPaths()
void Robot::CheckInput(int startX, int startY, int goalX, int goalY, int *ptrCount, string &ans, string *ptr )
{
if ( (startX == goalX) && (startY == goalY) )
{
*ptrCount = 0;
*ptr = "Start and Goal are the same!";
}
else
return FindPaths(startX, startY, goalX, goalY, ptrCount, ans, ptr);
} // end CheckInput
// Simulates moving Robot North and returns to FindPaths()
void Robot::RobotNorth(int x1, int y1, int x2, int y2, int *ptrCount, string &ans, string *ptr)
{
FindPaths(x1, y1 + 1, x2, y2, ptrCount, ans + "N", ptr);
} // end RobotNorth
// Simulates moving Robot South and returns to FindPaths()
void Robot::RobotSouth(int x1, int y1, int x2, int y2, int *ptrCount, string &ans, string *ptr)
{
return FindPaths(x1, y1 - 1, x2, y2, ptrCount, ans + "S", ptr);
} // end RobotSouth
// Simulates moving Robot East and returns to FindPaths()
void Robot::RobotEast(int x1, int y1, int x2, int y2, int *ptrCount, string &ans, string *ptr)
{
return FindPaths(x1 + 1, y1, x2, y2, ptrCount, ans + "E", ptr);
} // end RobotEast
// Simulates moving Robot West and returns to FindPaths()
void Robot::RobotWest(int x1, int y1, int x2, int y2, int *ptrCount, string &ans, string *ptr)
{
return FindPaths((x1 - 1), y1, x2, y2, ptrCount, ans + "W", ptr);
} // end RobotWest
// FindPaths() is a recursive function that solves how many unique paths are
// between the start coordinates and goal coordinates. It makes calls to:
// RobotNorth to simulate moving North, RobotSouth to simulate moving South
// RobotEast to simulate moving East, and RobotWest to simulate moving West.
// Each time the function finds a unique path the counter is updated through
// *ptrCount (pointer to class int count) and the directions are saved using
// *ptr (pointer to class string path)
void Robot::FindPaths(int x1, int y1, int x2, int y2, int *ptrCount, string &ans, string *ptr)
{
// if unique path is found update class variables: counter and path
if(x1 == x2 && y1 == y2)
{
*ptrCount += 1;
// adding new line character to ans string
ans = ans + "\n";
// using pointer to fill paths string in main
*ptr += ans;
}
// if Robot is South of Target
if (y1 < y2)
{
if (x1 < x2)
{
// move East
RobotEast(x1, y1, x2, y2, ptrCount, ans, ptr);
}
else if (x1 > x2)
{
// move West
RobotWest(x1, y1, x2, y2, ptrCount, ans, ptr);
}
// move North
return RobotNorth(x1, y1, x2, y2, ptrCount, ans, ptr);
} // end if (y1 < y2)
// if Robot is South of Target
else if (y1 > y2)
{
if (x1 < x2)
{
// move East
RobotEast(x1, y1, x2, y2, ptrCount, ans, ptr);
}
else if (x1 > x2)
{
// move West
RobotWest(x1, y1, x2, y2, ptrCount, ans, ptr);
}
// move South
return RobotSouth(x1, y1, x2, y2, ptrCount, ans, ptr);
} // end else if (y1 > y2)
// if Robot is West of Target
else if ( x1 < x2)
{
if (y1 < y2)
{
// move North
RobotNorth(x1, y1, x2, y2, ptrCount, ans, ptr);
}
else if (y2 > y2)
{
// move South
return RobotSouth(x1, y1, x2, y2, ptrCount, ans, ptr);
}
// move East
return RobotEast(x1, y1, x2, y2, ptrCount, ans, ptr);
} // end else if ( x1 < x2)
// if Robot is East of Target
else if (x1 > x2)
{
if (y1 < y2)
{
// move North
return RobotNorth(x1, y1, x2, y2, ptrCount, ans, ptr);
}
else if (y1 > y2)
{
// move South
return RobotSouth(x1, y1, x2, y2, ptrCount, ans, ptr);
}
// move West
RobotWest(x1, y1, x2, y2, ptrCount, ans, ptr);
} // else if (x1 > x2)
} // FindPaths
/*
*************** Class: Public / Friend Method *****************
*/
// overloaded << friend function for printing a robot object to console
std::ostream & operator<<(std::ostream & os, Robot & rb)
{
os << "Number of Paths: " << rb.count << endl;
os << rb.path << endl;
return os;
} // end operator<<