-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cs
More file actions
147 lines (135 loc) · 4.62 KB
/
Copy pathUser.cs
File metadata and controls
147 lines (135 loc) · 4.62 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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Linq;
using MarsRover.Interfaces;
using MarsRover.Models;
using System.Collections;
namespace MarsRover
{
public class User
{
///<summary>
/// Mars Rover.
///</summary>
private IRover _marsRover;
///<summary>
/// Command Queue.
///</summary>
private Queue _commandList;
///<summary>
/// Stack of executed commands.
///</summary>
private Stack _execList;
///<param name="rover">
/// Mars rover.
///</param>
public User(IRover rover)
{
this._commandList = new Queue();
this._execList = new Stack();
this._marsRover = rover;
}
///<summary>
/// Create the command list.
///</summary>
///<param name="command">
/// Command text.
///</param>
public void Operate(string command)
{
string input = string.Empty;
int distance = 0;
IMoveCommand cmdMove = default;
IRotateCommand cmdRotate = default;
switch (command)
{
case var ch when ch.EndsWith('m'):
input = new string(ch.SkipLast<char>(1).ToArray<char>());
distance = 0;
if (int.TryParse(input, out distance))
{
cmdMove = new MoveForwardCommand(this._marsRover, distance);
this._commandList.Enqueue(cmdMove);
}
break;
case var ch when ch.SequenceEqual<char>(Direction.Left.ToString()):
input = ch;
cmdRotate = new RotateLeftCommand(this._marsRover);
this._commandList.Enqueue(cmdRotate);
break;
case var ch when ch.SequenceEqual<char>(Direction.Right.ToString()):
input = ch;
cmdRotate = new RotateRightCommand(this._marsRover);
this._commandList.Enqueue(cmdRotate);
break;
default:
break;
}
}
///<summary>
/// Execute the command list.
///</summary>
public bool Execute()
{
IMoveCommand moveCommand = default;
IRotateCommand rotateCommand = default;
while (this._commandList.Count != 0)
{
var command = this._commandList.Dequeue();
switch (command.GetType().Name)
{
case "MoveForwardCommand":
moveCommand = (MoveForwardCommand)command;
moveCommand.Execute();
this._execList.Push(moveCommand);
if (!moveCommand.CanExecute())
return moveCommand.CanExecute();
else
break;
case "RotateLeftCommand":
rotateCommand = (RotateLeftCommand)command;
rotateCommand.Execute();
this._execList.Push(rotateCommand);
break;
case "RotateRightCommand":
rotateCommand = (RotateRightCommand)command;
rotateCommand.Execute();
this._execList.Push(rotateCommand);
break;
default:
break;
}
}
return true;
}
///<summary>
/// Revert the last executed command.
///</summary>
public void UndoOperation()
{
IMoveCommand moveCommand = (MoveForwardCommand)this._execList.Pop();
moveCommand.Undo();
}
///<summary>
/// Clear the stack ond command queue list.
///</summary>
public void ClearAll()
{
this._execList.Clear();
this._commandList.Clear();
}
///<summary>
/// Log the location of the Mars Rover.
///</summary>
///<param name="path">Path of the output file where location is logged.</param>
public async Task<string> LogLocationAsync(string path)
{
string location = $"Current location of Mars Rover: {this._marsRover.Location}";
path += "/output.txt";
await File.WriteAllTextAsync(path, location);
Task.WaitAll();
return "Logging Successful.";
}
}
}