/
Rover.cs
224 lines (205 loc) · 7.49 KB
/
Rover.cs
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
using System.Text;
namespace ConsoleApp1
{
class Rover
{
public Rover(string _roverName, int _xAxis, int _yAxis, Directions _direction, RoverCommand? _initRoverCmd = null)
{
// Lets make sure we cant spawn the rovers outside the grid area.
if (_xAxis > Config.GridSizeXMax || _xAxis < Config.GridSizeXMin || _yAxis > Config.GridSizeYMax || _yAxis < Config.GridSizeYMin)
{
throw new ArgumentException("You cannot spawn a rover outside the boundries");
}
RoverName = _roverName;
xAxis = _xAxis;
yAxis = _yAxis;
Direction = _direction;
RoverCommand = _initRoverCmd;
}
public string RoverName { get; set; }
public int xAxis { get; set; }
public int yAxis { get; set; }
public enum Directions : sbyte
{
N = 0,
E = 1,
S = 2,
W = 3
}
public Directions Direction { get; set; }
public RoverCommand? RoverCommand { get; set; }
public bool Move()
{
// the conditionals inside the switch statement are to tell the execution process its about to leave the boundry.
switch (this.Direction)
{
case Directions.N:
if (!CheckForCollision(this.xAxis, this.yAxis + 1))
{
this.yAxis += 1;
return true;
}
break;
case Directions.S:
if (!CheckForCollision(this.xAxis, this.yAxis - 1))
{
this.yAxis -= 1;
return true;
}
break;
case Directions.E:
if (!CheckForCollision(this.xAxis + 1, this.yAxis))
{
this.xAxis += 1;
return true;
}
break;
case Directions.W:
if (!CheckForCollision(this.xAxis - 1, this.yAxis))
{
this.xAxis -= 1;
return true;
}
break;
}
return false;
}
public void RotateRight()
{
if (this.Direction == Directions.W)
{
this.Direction = Directions.N;
}
else
{
this.Direction = this.Direction + 1;
}
}
public void RotateLeft()
{
if (this.Direction == Directions.N)
{
this.Direction = Directions.W;
}
else
{
this.Direction = this.Direction - 1;
}
}
public bool BatchExecute(string? args = null)
{
if (RoverCommand == null || RoverCommand.BatchCommands == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"No Batch Commands Found for {this.RoverName} No movements will be executed");
Console.ResetColor();
return false;
}
if (!RoverCommand.CheckCommandsAreValid())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid Commands Detected! Please only a L R or M. No movements will be executed");
Console.ResetColor();
return false;
}
Console.WriteLine($"Commencing Batch Commands for {this.RoverName}..");
foreach (var Command in this.RoverCommand.BatchCommands)
{
if (Char.ToUpper(Command) == 'M')
{
if (!this.Move())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You're trying to navigate the rover into another rover or run outside the boundry, please dont do that!");
Console.WriteLine("Discarding any remainder commands");
Console.ResetColor();
break;
}
}
else if (Char.ToUpper(Command) == 'L')
{
this.RotateLeft();
}
else if (Char.ToUpper(Command) == 'R')
{
this.RotateRight();
}
if (args == "animate")
{
this.Render();
Console.WriteLine(" ");// Blank Line
Thread.Sleep(500);
}
}
Console.WriteLine($"{this.RoverName}: Resting Place: {this.xAxis}, {this.yAxis}, {this.Direction}");
return true;
}
public Boolean CheckForCollision(int DesiredxAxis, int DesiredyAxis)
{
//these two are to check if the rover will run out of bounds
if (DesiredxAxis < Config.GridSizeXMin || DesiredxAxis > Config.GridSizeXMax)
return true;
if (DesiredyAxis < Config.GridSizeYMin || DesiredyAxis > Config.GridSizeYMax)
return true;
// this is to see if it will collide with another rover
if (Config.RoverList.Any(x => x.xAxis == DesiredxAxis && x.yAxis == DesiredyAxis))
{
return true;
}
return false;
}
public void AnnounceCurrentPosition()
{
Console.WriteLine($"{RoverName}: Current Location: {xAxis}, {yAxis}, {Direction}");
}
public void Render()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
for (var yAxis = Config.GridSizeYMax; yAxis >= Config.GridSizeYMin; yAxis--)
{
// this is for X axis
var RowString = new StringBuilder("", Config.GridSizeXCount);
for (var xAxis = Config.GridSizeXMin; xAxis <= Config.GridSizeXMax; xAxis++)
{
//current rover position
if (this.xAxis == xAxis && this.yAxis == yAxis)
{
RowString.Append(this.RoverIcon() + " ");
}
//list other rovers
else if (Config.RoverList.Any(x => x.xAxis == xAxis && x.yAxis == yAxis))
{
RowString.Append("\u263B ");
}
//The mars wilderness
else
{
RowString.Append("\u2592 ");
}
}
Console.WriteLine(RowString);
}
Console.ResetColor();
}
public string RoverIcon()
{
string DirectionCharacter = "";
switch (this.Direction)
{
case Directions.N:
DirectionCharacter = "\u25b2";
break;
case Directions.S:
DirectionCharacter = "\u25bc";
break;
case Directions.E:
DirectionCharacter = "\u25ba";
break;
case Directions.W:
DirectionCharacter = "\u25c4";
break;
}
return DirectionCharacter;
}
}
}