-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandParser.cs
85 lines (73 loc) · 2.91 KB
/
CommandParser.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
using System;
using ConcurrencyUtilities;
namespace ZorkServer
{
// Can't use an ActiveObjectInputOutput as there are multiple output channels
public class CommandParser: ActiveObjectInput<string>
{
Channel<string[]> _changeRoomCommand;
Channel<string[]> _useItemCommand;
Channel<string[]> _takeItemCommand;
Channel<string[]> _invalidCommand;
// Input channel: command
// Manual output channels: changeRoomCommand, useItemCommand, takeItemCommand, invalidCommand
public CommandParser(Channel<string> command, Channel<string[]> changeRoomCommand,
Channel<string[]> useItemCommand, Channel<string[]> takeItemCommand,
Channel<string[]> invalidCommand): base(command) {
_changeRoomCommand = changeRoomCommand;
_useItemCommand = useItemCommand;
_takeItemCommand = takeItemCommand;
_invalidCommand = invalidCommand;
}
static bool WordsContain(string[] words, string searchString) {
return Array.IndexOf(words, searchString) >= 0;
}
static bool IsATravelVerb(string word) {
return WordsContain(new string[] { "go", "run", "walk", "travel", "move" }, word);
}
static bool IsADirection(string word) {
return WordsContain(new string[] { "up", "down", "left", "right", "north", "south", "east", "west" }, word);
}
static bool IsDirectionCommand(string[] words) {
return ( words.Length == 1 && IsADirection(words[0]) ||
words.Length == 2 && IsADirection(words[1]) && IsATravelVerb(words[0]) );
}
public static string DirectionFromDirectionCommand(string[] words) {
foreach (string word in words) {
if (IsADirection(word))
return word;
}
throw new ArgumentException("No direction found in words");
}
static bool IsUseItemCommand(string[] words) {
return ( words.Length == 2 && words[0] == "use" ||
words.Length == 4 && words[0] == "use" && WordsContain(new string[] { "in", "with" }, words[2]) );
}
public static string[] ItemsFromUseItemCommand(string[] words) {
if (words.Length == 4)
return new string[] { words[1], words[3] };
return new string[] { words[1] };
}
static bool IsTakeItemCommand(string[] words) {
return (words.Length == 2 && words[0] == "take");
}
public static string ItemFromTakeItemCommand(string[] words) {
return words[1];
}
/// <summary>
/// Automatically process commands that appear in the command channel
/// </summary>
/// <param name="command">The command string to parse.</param>
protected override void Process(string command) {
string[] words = command.ToLower().Split(' ');
if (IsDirectionCommand(words))
_changeRoomCommand.Put(new string[] { DirectionFromDirectionCommand(words) });
else if (IsUseItemCommand(words))
_useItemCommand.Put(ItemsFromUseItemCommand(words));
else if (IsTakeItemCommand(words))
_takeItemCommand.Put(new string[] { ItemFromTakeItemCommand(words) });
else
_invalidCommand.Put(words);
}
}
}