Permalink
Browse files

Add Word type and Words collection

  • Loading branch information...
bobbymcr committed Dec 1, 2018
1 parent 72fc4d9 commit bce6ddd9896a3fd4b35a2a41d475d74f7902bf19
Showing with 49 additions and 9 deletions.
  1. +3 −3 core/src/SentenceMessage.cs
  2. +4 −2 core/src/SentenceParser.cs
  3. +21 −0 core/src/Word.cs
  4. +17 −0 core/src/Words.cs
  5. +2 −2 core/test/SentenceParserTest.cs
  6. +2 −2 sample/src/Game.cs
@@ -6,14 +6,14 @@ namespace Adventure
{
public sealed class SentenceMessage
{
public SentenceMessage(string verb, string noun)
public SentenceMessage(Word verb, Word noun)
{
this.Verb = verb;
this.Noun = noun;
}

public string Verb { get; }
public Word Verb { get; }

public string Noun { get; }
public Word Noun { get; }
}
}
@@ -9,11 +9,13 @@ namespace Adventure
public sealed class SentenceParser : IDisposable
{
private readonly MessageBus bus;
private readonly Words words;
private readonly IDisposable subscription;

public SentenceParser(MessageBus bus)
public SentenceParser(MessageBus bus, Words words)
{
this.bus = bus;
this.words = words;
this.subscription = this.bus.Subscribe<InputMessage>(m => this.ProcessInput(m.Line));
}

@@ -32,7 +34,7 @@ private void ProcessInput(string line)
noun = parts[1].Trim();
}

this.bus.Send(new SentenceMessage(verb, noun));
this.bus.Send(new SentenceMessage(this.words[verb], this.words[noun]));
}
}
}
@@ -0,0 +1,21 @@
// <copyright file="Word.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
public sealed class Word
{
public Word(string primary, string actual)
{
this.Primary = primary;
this.Actual = actual;
}

public string Primary { get; }

public string Actual { get; }

public override string ToString() => this.Actual;
}
}
@@ -0,0 +1,17 @@
// <copyright file="Words.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Adventure
{
public sealed class Words
{
public Word this[string key]
{
get
{
return new Word(key, key);
}
}
}
}
@@ -25,7 +25,7 @@ public void SendWords(string input, string output)
List<string> sentences = new List<string>();
Action<SentenceMessage> onSentence = m => sentences.Add(m.Verb + ":" + m.Noun);
bus.Subscribe(onSentence);
SentenceParser parser = new SentenceParser(bus);
SentenceParser parser = new SentenceParser(bus, new Words());

bus.Send(new InputMessage(input));

@@ -39,7 +39,7 @@ public void SendAfterDispose()
List<string> sentences = new List<string>();
Action<SentenceMessage> onSentence = m => sentences.Add(m.Verb + ":" + m.Noun);
bus.Subscribe(onSentence);
SentenceParser parser = new SentenceParser(bus);
SentenceParser parser = new SentenceParser(bus, new Words());

parser.Dispose();
bus.Send(new InputMessage("hello"));
@@ -21,8 +21,8 @@ public Game(TextReader reader, TextWriter writer)
public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(cts, m.Verb)))
using (new SentenceParser(this.bus, new Words()))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(cts, m.Verb.Primary)))
{
this.console.Run(cts.Token);
}

0 comments on commit bce6ddd

Please sign in to comment.