-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (73 loc) · 2.6 KB
/
Copy pathProgram.cs
File metadata and controls
81 lines (73 loc) · 2.6 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
using System;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
using MarsRover.Models;
namespace MarsRover
{
class Program
{
public static readonly string input;
public static readonly string output;
public static readonly string searchPattern;
private static int maxCommands;
static Program()
{
maxCommands = 5;
input = "/Inputs";
output = "/Outputs";
searchPattern = "*.txt";
}
static void Main(string[] args)
{
string inputPath = Directory.GetCurrentDirectory() + input;
string outputPath = Directory.GetCurrentDirectory() + output;
string[] files = Directory.GetFiles(inputPath, searchPattern);
MarsSpaceRover marsRover = new MarsSpaceRover(100, 0);
User usr = new User(marsRover);
Task operateTask = Task.Run(async () => await OperateCommandsAsync(files, usr));
operateTask.Wait();
Task logLocationTask = operateTask.ContinueWith((task) =>
{
if (task.IsCompleted)
{
Task logTask = Task.Run(async () => {
string log = await usr.LogLocationAsync(outputPath);
Task.WaitAll();
Console.WriteLine(log);
});
logTask.Wait();
}
});
logLocationTask.Wait();
usr.ClearAll();
}
///<summary>
/// Operate on the list of input commands.
///</summary>
///<param name="files">List of input files.</param>
///<param name="usr">The Invoker class.</param>
private static async Task OperateCommandsAsync(string[] files, User usr)
{
bool isHalted = false;
foreach (string input in files)
{
string[] cmdList = await File.ReadAllLinesAsync(input);
Task.WaitAll();
int max = cmdList.Length < maxCommands ? cmdList.Length : maxCommands;
string[] text = cmdList.Take<string>(max).ToArray<string>();
foreach (string txtInput in text)
{
usr.Operate(txtInput);
}
isHalted = !usr.Execute();
if (isHalted)
{
usr.UndoOperation();
break;
}
usr.ClearAll();
}
}
}
}