-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cs
More file actions
87 lines (80 loc) · 2.62 KB
/
Command.cs
File metadata and controls
87 lines (80 loc) · 2.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
using System;
using System.Collections.Generic;
using Main;
class Command
{
Dictionary<char, int> AllowedCharactersAndParams = new Dictionary<char, int>()
{
{'C',2},
{'L',4},
{'R',4},
{'B',3},
{'Q',0}
};
public char CommandIdentifier
{
get
{
return cCommandIdentifier;
}
}
private char cCommandIdentifier;
public object[] CommandParameters
{
get
{
return oaCommandParameters;
}
}
private object[] oaCommandParameters;
public Command(string sCommand)
{
try
{
string[] sCommandSplit = sCommand.Split(' ');
if (sCommandSplit[0].Length != 1)
{
throw new Exception("Invalid Entry");
}
cCommandIdentifier = Convert.ToChar(sCommandSplit[0]);
if (!AllowedCharactersAndParams.ContainsKey(cCommandIdentifier))
{
throw new Exception("Invalid command");
}
oaCommandParameters = new object[sCommandSplit.Length - 1];
for (int i = 0; i < sCommandSplit.Length - 1; i++)
{
if (i == 2 && cCommandIdentifier == 'B')
{
if (sCommandSplit[i].Length != 1)
{
throw new Exception("Invalid color for Bucket");
}
else
{
oaCommandParameters[i] = Convert.ToChar(sCommandSplit[i + 1]);
continue;
}
}
int iPartialParameter = 0;
try
{
iPartialParameter = Convert.ToInt32(sCommandSplit[i + 1]);
}
catch (Exception)
{
throw new Exception("Parameters are not numbers");
}
oaCommandParameters[i] = iPartialParameter;
}
if (AllowedCharactersAndParams[CommandIdentifier] != CommandParameters.Length)
{
throw new Exception("Invalid number of parameters for the command entered");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}