Exemple #1
0
        /// <summary>
        /// Determine the type of each of the source elements and fill the tokens collection
        /// </summary>
        /// <param name="elements">Array of split elements</param>
        void Tokenise(string[] elements)
        {
            tokens = new List <Token>(elements.Length);

            int position = 0;

            foreach (string el in elements)
            {
                // ensure the word is in lower case and has no space
                string element = el.ToLower().Trim();

                if (String.IsNullOrWhiteSpace(element))
                {
                    continue;
                }

                if (WordStore.IsIgnored(element))
                {
                    // token added as an ignored word
                    tokens.Add(new Token(element, TokenType.Ignored, position));
                    IgnoreCount++;
                }
                else
                {
                    // this is a preposition
                    if (WordStore.IsPreposition(element))
                    {
                        tokens.Add(new Token(element, TokenType.Preposition, position));
                    }
                    // this is a direction
                    else if (WordStore.IsDirection(element))
                    {
                        tokens.Add(new Token(element, TokenType.Direction, position));
                    }
                    // this is a command, but only accept the first command found
                    else if (CommandManager.IsCommand(element, position) && Command == default(Token))
                    {
                        Command = new Token(element, TokenType.Command, position);
                        tokens.Add(Command);
                    }
                    else
                    {
                        tokens.Add(new Token(element, TokenType.Unrecognised, position));
                    }
                }

                position++; // TODO: these two vars could be consolidated, but does it look stupid passing WordCount as position?
                WordCount++;
            }
            elements = null;
        }
Exemple #2
0
        protected override Response ProcessInternal(Engine engine, Tokeniser tokens)
        {
            var directionWord = tokens.Direction;

            if (directionWord == null || !WordStore.IsDirection(directionWord))
            {
                return(new Response("Go where?"));
            }

            var direction = WordStore.GetDirection(directionWord);
            var ego       = engine.GameState.Ego;

            var  response = new Response();
            Room newRoom;

            if (direction == Direction.Back)
            {
                if (ego.PreviousRoom != null)
                {
                    newRoom = ego.PreviousRoom;
                }
                else
                {
                    return(new Response("You've not been anywhere yet!"));
                }
            }
            else
            {
                newRoom = ego.CurrentRoom.GetNextRoom(direction);
            }

            if (newRoom == null)
            {
                return(new Response("You try to walk " + directionWord + ", but realise how bad a mistake that wasa when you walk straight into a solid wall. Your nose will hurt for days."));
            }

            if (!newRoom.IsAccessible)
            {
                response.AddMessage("You try, but find that the door is locked.");
            }
            else
            {
                ego.MoveTo(newRoom);
                response.AddMessage(newRoom.Describe());
            }
            response.Merge(engine.RunOccurrences(new GoRoom.Trigger(newRoom)));
            return(response);
        }