예제 #1
0
 public void AddRoomObject(RoomObject objectToAdd)
 {
     RoomObjectsList.Add(objectToAdd);
 }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Allyson's Escape Room");
            Console.WriteLine("Hit Enter to continue...");
            Console.ReadLine();
            Console.Clear();
            Console.WriteLine("You wake up in an unfamiliar bedroom, on the floor. There is a bed, a dresser, a desk and a closet. The closet is on the opposite wall.");
            string        userResponse          = "";
            string        currentCommand        = "";
            string        primaryObjectName     = "";
            List <string> secondaryObjectsNames = new List <string>();

            string[] commandsList = { "inspect", "use", "help", "quit" };

            List <RoomObject>     roomObjects       = new List <RoomObject>();
            RoomObjectsCollection objectsCollection = new RoomObjectsCollection();

            InitializeRoomObjects(objectsCollection);

            while (userResponse != "quit")
            {
                Console.WriteLine("What would you like to do?");
                userResponse = Console.ReadLine().ToLower();
                string[] responseArray = userResponse.Split(' ');
                if (responseArray.Length > 0)
                {
                    if (IsValidCommand(responseArray[0], commandsList))
                    {
                        currentCommand = responseArray[0];
                        if (currentCommand == "help")
                        {
                            string availableCommands = "Available Commands: " + string.Join(", ", commandsList);
                            Console.WriteLine(availableCommands);
                            continue;
                        }
                        if (currentCommand == "quit")
                        {
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine("I don't know how to do that.");
                        continue;
                    }
                }

                if (currentCommand == "inspect")
                {
                    primaryObjectName = responseArray[1];
                }
                if (currentCommand == "use")
                {
                    primaryObjectName     = responseArray[responseArray.Length - 1];
                    secondaryObjectsNames = ParseSecondaryObjects(responseArray);
                }
                bool objectFound = false;

                RoomObject primaryObject = objectsCollection.FindRoomObject(primaryObjectName);

                if (primaryObject == null)
                {
                    Console.WriteLine("No " + primaryObjectName + " exist to " + currentCommand + ".");
                    continue;
                }

                objectFound = true;
                if (currentCommand == "inspect")
                {
                    if (primaryObject.ParentName == "")
                    {
                        primaryObject.Inspect();
                        continue;
                    }
                    else
                    {
                        RoomObject parentObject = objectsCollection.FindRoomObject(primaryObject.ParentName);
                        if (parentObject != null && parentObject.HasBeenInspected == true)
                        {
                            primaryObject.Inspect();
                            continue;
                        }
                        else
                        {
                            objectFound = false;
                        }
                    }
                }
                else if (currentCommand == "use")
                {
                    RoomObject secondaryObject = null;

                    if (secondaryObjectsNames.Count() > 0)
                    {
                        secondaryObject = objectsCollection.FindRoomObject(secondaryObjectsNames[0]);
                    }


                    if (primaryObject == null)
                    {
                        Console.WriteLine("You must use that ON something.");
                        continue;
                    }

                    if (secondaryObject == null)
                    {
                        Console.WriteLine("You can't use that on that object.");
                        continue;
                    }

                    if (primaryObject.ParentName == "")
                    {
                        if (secondaryObject.ParentName == "")
                        {
                            primaryObject.Use(secondaryObject);
                        }

                        else
                        {
                            RoomObject parentObject = objectsCollection.FindRoomObject(secondaryObject.ParentName);
                            if (parentObject.HasBeenInspected)
                            {
                                primaryObject.Use(secondaryObject);
                            }

                            else
                            {
                                Console.WriteLine("You don't see a " + secondaryObject.Name);
                                continue;
                            }
                        }
                    }

                    else
                    {
                        RoomObject parentObject = objectsCollection.FindRoomObject(primaryObject.ParentName);
                        if (parentObject.HasBeenInspected)
                        {
                            if (secondaryObject.ParentName == "")
                            {
                                primaryObject.Use(secondaryObject);
                            }

                            else
                            {
                                RoomObject secondarParentObject = objectsCollection.FindRoomObject(secondaryObject.ParentName);
                                if (secondarParentObject.HasBeenInspected)
                                {
                                    primaryObject.Use(secondaryObject);
                                }

                                else
                                {
                                    Console.WriteLine("You don't see a " + secondaryObject.Name);
                                    continue;
                                }
                            }
                        }

                        else
                        {
                            Console.WriteLine("You don't see a " + primaryObject.Name);
                            continue;
                        }
                    }
                }
                if (objectFound == false)
                {
                    Console.WriteLine("You don't see that object");
                }
            }
        }