示例#1
0
        public void RunGameLoop()
        {
            List <string> verbsRequiringObjects = new List <string>()
            {
                "go", "read", "drop", "take", "throw"
            };

            while (true)
            {
                Console.Write("\nWhat do you want to do? ");
                string response = Console.ReadLine().ToLower();
                Console.WriteLine();

                // Split the user's input into a verb and another word
                string[] words = response.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length > 2)
                {
                    Console.WriteLine("The most I can cope with is two words at a time!");
                    continue;
                }

                string theVerb           = words[0];
                string theObject         = "";
                bool   verbNeedsAnObject = verbsRequiringObjects.Contains(theVerb);
                if (verbNeedsAnObject && words.Length == 1)
                {
                    Console.WriteLine("{0} what or where?", theVerb);
                    continue;
                }
                if (!verbNeedsAnObject && words.Length == 2)
                {
                    Console.WriteLine("I don't know how to do that.");
                    continue;
                }
                if (verbNeedsAnObject)
                {
                    theObject = words[1];
                }

                try
                {
                    switch (theVerb)
                    {
                    case "die":
                    case "exit":
                    case "quit": return;         // leave the game.

                    case "go":
                        theFSM.DoTransition(theObject);
                        describeSituation();
                        break;

                    case "help":
                        Console.WriteLine("Valid verbs are go, drop, take, look, help, throw, read, quit, ");
                        Console.WriteLine("and perhaps some others.   Some verbs must be followed by another");
                        Console.WriteLine("object word to make sense, e.g.  'go west', or 'read book'.");
                        break;

                    case "drop":
                        thePlayer.RemoveThing(theObject);
                        theFSM.CurrentState.AddThing(theObject);
                        Console.WriteLine("OK.");
                        break;

                    case "take":
                        theFSM.CurrentState.RemoveThing(theObject);
                        thePlayer.AddThing(theObject);
                        Console.WriteLine("OK.");
                        break;

                    case "look":
                        describeSituation();
                        break;

                    case "read":

                        if (theObject == "book" || theObject == "ipad")
                        {
                            if (thePlayer.HasThing(theObject) || theFSM.CurrentState.HasThing(theObject))
                            {
                                if (theObject == "book")
                                {
                                    Console.WriteLine("The title is 'Think Sharply with C#'. It looks boring!");
                                }
                                else
                                {
                                    Console.WriteLine("The iPad is locked, and you don't know the PIN code.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("There is no {0} here to read.", theObject);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Nobody can read a {0}.", theObject);
                        }
                        break;

                    default:
                        Console.WriteLine("Huh?");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }