public static void GameDescription() { GFXText.PrintTxt(-1, 3, 0, 20, "\"You are lost in a forest at night and there is a thunderstorm approaching.\"", false, true); System.Threading.Thread.Sleep(Globals.SleepTime); Console.Clear(); GFXText.PrintTxt(-1, 3, 0, 20, "\"Finally you find a wooden sign that reads...\"", false, true); System.Threading.Thread.Sleep(Globals.SleepTime); GFXText.PrintTxt(-1, 6, 0, 20, "Welcome to De Morgans Mansion", false, true); System.Threading.Thread.Sleep(Globals.SleepTime); Console.CursorVisible = true; Console.Clear(); }
// Look in the current Room // Look with no arguments returns both descriptions of the room instantly, and a list of current 'room inventory' // Does not list usable items in the room, only items the player is able to pickup public string Look() { Console.Clear(); GFXText.PrintTxt(Globals.RoomNameXPos, Globals.RoomNameYPos, 0, 0, LoadGame.rooms[CurRoom].Name, true, false); //GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].name, Globals.RoomNameXPos, Globals.RoomNameYPos, false); GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].Description, Globals.RoomDescriptionXPos, Globals.RoomDescriptionYPos, false); GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].Description2, Globals.RoomDescription2XPos, Globals.RoomDescription2YPos, false); Console.Write("\n\n"); int lineCheck = 0; // Linecheck is needed to break lines between found items, since PrintTextWithHighlights() is used for (int i = 0; i < LoadGame.rooms[CurRoom].roomItems.Count; i++) { if (LoadGame.rooms[CurRoom].roomItems[i].Pickup) { // Message to print when an item is found ===== REVISE TEXT? GFXText.PrintTextWithHighlights($"In this room there's also a [{LoadGame.rooms[CurRoom].roomItems[i].Name}].", 2, 12 + lineCheck, false); lineCheck++; } } return(null); //return $" return a message to the Game Handler"; }
static void Main(string[] args) { string tmpstring = ""; //used to fix user input // the following are used in the input check var cmdList = new List <string>() { "GO", "GET", "DROP", "USE", "ON", "LOOK", "SHOW" }; var dirList = new List <string>() { "FORWARD", "BACK", "LEFT", "RIGHT", "NORTH", "EAST", "SOUTH", "WEST" }; // Remaking itemList to clean up unused(?) vars var itemList = new List <string>() { "KEY", "NOTE", "FIREPLACE", "BRONZEPIECE", "MATCHES", "TORCH", "FLAMINGTORCH", "BRAZIER", "CHANDELIER", "THRONE", "PAINTING", "DOOR", "CHAIN", "IVY", "REMAINS", "HAND", "WINDOW", "PANTRY", "BREAD", "BLOOD", "LOCKER", "BED", "SILVERPIECE", "GOLDPIECE", "MEDALLION", "PANEL" }; // I can search in this dictionary when parsing the input Dictionary <Action, List <string> > myCmds = new Dictionary <Action, List <string> >() { { Action.GO, dirList }, { Action.GET, itemList }, { Action.DROP, itemList }, { Action.USE, itemList }, { Action.ON, itemList }, { Action.LOOK, itemList }, //{ Action.INSPECT, itemList }, { Action.SHOW, itemList } }; System.Media.SoundPlayer soundplayer = new System.Media.SoundPlayer("Soundtrack/soundtrack.wav"); // Play epic soundtrack soundplayer.PlayLooping(); // Load the Game and create Rooms, items, etc. LoadGame.Init(); // The handler will create the player and operate all the actions var handler = new GameHandler(); // Welcome message and first room description Console.Write("\nWhat is your name, traveller? "); string playName = Console.ReadLine(); string input = ""; handler.InitPlayer(playName); Console.WriteLine("\nWelcome, {0}! An adventure awaits you!\n\n", playName); System.Threading.Thread.Sleep(Globals.SleepTime); Console.Clear(); GFXText.PrintTxt(Globals.RoomNameXPos, Globals.RoomNameYPos, Globals.TextTrail, Globals.TextDelay, LoadGame.rooms[RNames.Entrance].Name, false, false); //GFXText.PrintTextWithHighlights(LoadGame.rooms[RNames.Entrance].name, Globals.RoomNameXPos, Globals.RoomNameYPos, true); GFXText.PrintTextWithHighlights(LoadGame.rooms[RNames.Entrance].Description, Globals.RoomDescriptionXPos, Globals.RoomDescriptionYPos, true); Console.Write("\n\n"); LoadGame.rooms[RNames.Entrance].Visited = true; // end of description while (true) { input = ""; Console.Write("\nEnter a command or type [H] for list of commands\n>"); while (input == "") { input = Console.ReadLine(); } // fix for program crash if command starts with a single space while (input[0] == ' ') { for (int i = 1; i < input.Length; i++) { tmpstring += input[i].ToString(); } input = tmpstring; tmpstring = ""; } // end fix var argums = input.Split(' '); switch (argums.Length) { case 1: // The user has only typed a command with no enough arguments // Or he has typed H-for Help if (argums[0].ToUpper() == "H") { // Show help text Console.WriteLine(" --- Command List ---"); Console.WriteLine("GO <dir> - to move to the specified direction: FORWARD, BACK, LEFT, RIGHT" + "\nGET <item> - to collect an item and move it in the backpack" + "\nDROP <item> - to leave an item from the backpack in the current room" + "\nUSE <object> - to use an item or object" + "\nUSE <object> ON <object> - to use an item with another item" + "\nLOOK - to look around for additional hints and find loose items in the room" + "\nLOOK <object> - to get additional information about an item or object" + //"\nINSPECT object/Door - show a description of the object/door" + "\nSHOW - Lists all items in your backpack" + "\nQ or Quit - to Quit the game"); } else if (argums[0].ToUpper() == "Q" || argums[0].ToUpper() == "QUIT") { Console.WriteLine($"Thanks for playing {playName}! Welcome back!"); return; } else if (argums[0].ToUpper() == "SHOW") { handler.InvokeAction(argums); } else if (argums[0].ToUpper() == "LOOK") { handler.InvokeAction(argums); } else { Console.WriteLine("I beg your pardon? "); } break; case 2: // The user has typed a command and an argument // Note: convert the String in the mapping Enum of type Action // Currently needed to make sure program does not crash while trying to parse Enum that does not exist if (!cmdList.Contains(argums[0].ToUpper())) { Console.WriteLine("Invalid command."); break; } Action aKey = (Action)Enum.Parse(typeof(Action), argums[0].ToUpper()); // if (!cmdList.Contains(argums[0].ToUpper())) if (!myCmds.ContainsKey(aKey) || !myCmds[aKey].Contains(argums[1].ToUpper())) { // Error user has typed an invalid command // repeat the while loop Console.WriteLine("Unrecognized command. What do you wanna do?"); } else { // Command + dir/item are valid. Due to the Dict check, I know for sure that // if arg0 is GO/Other cmd then arg1 is a direction respective item/door // the 2nd and LAST argument is recognized. Now start the right Action! handler.InvokeAction(argums); } break; case 3: // The user has typed a command with 3 words. Invalid! Console.WriteLine("Unrecognized command. What do you wanna do?"); break; case 4: // the user has typed a command with 4 words. The only command which is valid // is: USE item1 ON item2/door if (!cmdList.Contains(argums[0].ToUpper())) { Console.WriteLine("Invalid command."); break; } Action aKey1 = (Action)Enum.Parse(typeof(Action), argums[0].ToUpper()); if (aKey1 != Action.USE || argums[2].ToUpper() != "ON" || !myCmds[aKey1].Contains(argums[1].ToUpper()) || !myCmds[aKey1].Contains(argums[3].ToUpper())) // was ToLower(), always returned error message { // Error user has typed an invalid command // repeat the while loop Console.WriteLine("Unrecognized command. What do you wanna do?"); } else { // The user has typed exactly USE item1 ON item2/door handler.InvokeAction(argums); } break; default: Console.WriteLine("Unrecognized command. What do you wanna do?"); break; } } }
// Methods // Take in the Direction and returns a Message to GameHandler // to help the player making next move // public string Go(Dir dir) { // Note (int) because dir is enum. DStatus doorStatus = LoadGame.rooms[CurRoom].exitDoors[(int)dir].Status; // Check first if I can move any direction (if there is a door or a wall) // I need the following regardless the position N,E, W,E. // if (doorStatus == DStatus.WALL) { msg = "I am sorry, but you hit a Wall!"; } else if (doorStatus == DStatus.Closed) { msg = "You find a door which is Locked. What would you like to do?"; } // If the players tries to move through an open door, the following will take place else { Console.Clear(); // NOTE - A bit confusing CurRoom as index and then assigned ;-)) CurRoom = LoadGame.rooms[CurRoom].exitDoors[(int)dir].LeadsToRoom; // Reads from list of rooms to find out where to go //ENDROOM if (LoadGame.rooms[CurRoom].EndPoint) { // Player has completed the game Console.Clear(); //string msg1 = "The medallion starts to glow and suddenly you start to levitate."; //string msg2 = "Congratulations you have solved the mystery and will now be set free."; string[] msg = { "You decide to walk through the newly opened doorway, entering the blinding light.", "You're finally able to leave this cursed mansion.", "Congratulations " + Name + ", you solved the mystery!" }; for (int i = 0; i < msg.Count(); i++) { GFXText.PrintTxt(-1, 5 + i * 2, Globals.TextTrail, Globals.TextDelay, msg[i], false, false); System.Threading.Thread.Sleep(Globals.SleepTime); } Console.ReadKey(); Environment.Exit(0); } //END OF ENDROOM if (LoadGame.rooms[CurRoom].Visited) // If the new room already visited (player backtracking), text will be printed instantly { GFXText.PrintTxt(Globals.RoomNameXPos, Globals.RoomNameYPos, 0, 0, LoadGame.rooms[CurRoom].Name, true, false); //GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].name, Globals.RoomNameXPos, Globals.RoomNameYPos, false); GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].Description, Globals.RoomDescriptionXPos, Globals.RoomDescriptionYPos, false); } else { GFXText.PrintTxt(Globals.RoomNameXPos, Globals.RoomNameYPos, Globals.TextTrail, Globals.TextDelay * 5, LoadGame.rooms[CurRoom].Name, false, false); //GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].name, Globals.RoomNameXPos, Globals.RoomNameYPos, true); // If the new room is not visited, text will be printed slowly GFXText.PrintTextWithHighlights(LoadGame.rooms[CurRoom].Description, Globals.RoomDescriptionXPos, Globals.RoomDescriptionYPos, true); LoadGame.rooms[CurRoom].Visited = true; } return(null); } return(msg); }
// Use() is overloaded // Use(arg) is currently used to let the user interact with objects (items) in the game // For example the first block lets the user "use" the chain and a key drops (spawns) in the room public void Use(INames item) { // Logic for using items in rooms var roomItems = LoadGame.rooms[CurRoom].roomItems; for (int i = 0; i < roomItems.Count(); i++) { if (roomItems[i].Name.ToUpper() == item.ToString() && !roomItems[i].IsUsed) { // BLOCK CHAIN if (roomItems[i].Name.ToUpper() == "CHAIN") { roomItems[i].IsUsed = true; var key = new Item("Key", "A rusty bronze key.", INames.EMPTY, ItemPos.Room, true); roomItems.Add(key); Console.Clear(); GFXText.PrintTextWithHighlights("A [key] falls down.", 2, 2, true); Console.Write("\n\n"); return; } // END OF BLOCK CHAIN // BLOCK REMAINS if (roomItems[i].Name.ToUpper() == "REMAINS") { roomItems[i].IsUsed = true; //var hand = new Item("Hand", "It smells really foul. Carved into the hand is a number: 42.", INames.EMPTY, ItemPos.Inventory, true); // If there's space in players inventory, add it there. Else add it to room inventory if (inventory.Count() < bagSize) { inventory.Add(new Item("Hand", "It smells really foul. Carved into the hand is a message: 42 is next to 21", INames.EMPTY, ItemPos.Inventory, true)); } else { roomItems.Add(new Item("Hand", "It smells really foul. Carved into the hand is a message: 42 is next to 21.", INames.EMPTY, ItemPos.Room, true)); } Console.Clear(); GFXText.PrintTextWithHighlights("Among the remains you find and pick up an [hand].", 2, 2, true); Console.Write("\n\n"); return; } // END OF BLOCK REMAINS // BLOCK PANTRY if (roomItems[i].Name.ToUpper() == "PANTRY") { roomItems[i].IsUsed = true; //var bread = new Item("Bread", "A fresh loaf of bread. Looks and smells really good.", INames.EMPTY, ItemPos.Inventory, true); // I collect the bread if I have space in the inventory, otherwise I leave it in the room. if (inventory.Count() < bagSize) { // roomItems[i].IsUsed = true; inventory.Add(new Item("Bread", "A fresh loaf of bread. Looks and smells really good.", INames.EMPTY, ItemPos.Inventory, true)); Console.Clear(); GFXText.PrintTextWithHighlights("You open the pantry and find a loaf of [bread] which you pick up.", 2, 2, true); } else { if (roomItems.FirstOrDefault(c => c.Name.ToUpper() == "BREAD") == null) // the bread has not yet been placed in the room { roomItems.Add(new Item("Bread", "A fresh loaf of bread. Looks and smells really good.", INames.EMPTY, ItemPos.Room, true)); } Console.Clear(); GFXText.PrintTextWithHighlights("You open the pantry and find a loaf of [bread] which you might pick up.", 2, 2, true); } Console.Write("\n\n"); return; } // END OF BLOCK PANTRY // BLOCK THRONE if (roomItems[i].Name.ToUpper() == "THRONE") { Console.Clear(); GFXText.PrintTxt(-1, 5, Globals.TextTrail, Globals.TextDelay, "For some reason you decide to drink the vile contents of the toilet...", true, false); System.Threading.Thread.Sleep(Globals.SleepTime); GFXText.PrintTxt(-1, 10, Globals.TextTrail, Globals.TextDelay, "You die...", false, false); Console.ReadKey(); Environment.Exit(0); } // END OF BLOCK THRONE // BLOCK IVY if (roomItems[i].Name.ToUpper() == "IVY") { Console.Clear(); GFXText.PrintTxt(-1, 5, Globals.TextTrail, Globals.TextDelay, "You touch the poison ivy and suddenly become paralyzed...", true, false); System.Threading.Thread.Sleep(Globals.SleepTime); GFXText.PrintTxt(-1, 10, Globals.TextTrail, Globals.TextDelay, "You die...", false, false); Console.ReadKey(); Environment.Exit(0); } // END OF BLOCK IVY // Add more blocks of code here for more item uses } else if (roomItems[i].Name.ToUpper() == item.ToString() && roomItems[i].IsUsed) { Console.WriteLine("\n{0} already used...", item); return; } } // Logic for using items in inventory for (int i = 0; i < inventory.Count; i++) { if (inventory[i].Name.ToUpper() == item.ToString().ToUpper()) { // BLOCK HAND if (item.ToString().ToUpper() == "HAND") { Console.Clear(); GFXText.PrintTxt(-1, 5, Globals.TextTrail, Globals.TextDelay, "For some reason you decide to consume the half-rotten hand...", true, false); System.Threading.Thread.Sleep(Globals.SleepTime); GFXText.PrintTxt(-1, 10, Globals.TextTrail, Globals.TextDelay, "You die...", false, false); Console.ReadKey(); Environment.Exit(0); } // END OF BLOCK HAND // BLOCK BREAD if (item.ToString().ToUpper() == "BREAD") { Console.Clear(); GFXText.PrintTextWithHighlights("The bread tastes delicious. Argh, but what is this? Inside you find a [goldpiece]!", Globals.RoomDescriptionXPos, Globals.RoomDescriptionYPos, true); var goldpiece = new Item("Goldpiece", "A golden piece of something bigger. What can it be?", INames.EMPTY, ItemPos.Inventory, true); inventory.Remove(inventory[i]); // REMOVE OLD ITEM inventory.Add(goldpiece); // ADD NEW ITEM return; } // END OF BLOCK BREAD // Player tries to combine the pieces. If the player got all pieces a medallion is created and the game is completed. if (item == INames.BRONZEPIECE || item == INames.SILVERPIECE || item == INames.GOLDPIECE) { // if (inventory.Contains(new Item("Bronzepiece", "", false)) && inventory.Contains(new Item("Silverpiece", "", false)) && inventory.Contains(new Item("Goldpiece", "", false))) if ((inventory.FirstOrDefault(c => c.Name.ToUpper() == "BRONZEPIECE")) != null && (inventory.FirstOrDefault(c => c.Name.ToUpper() == "SILVERPIECE")) != null && (inventory.FirstOrDefault(c => c.Name.ToUpper() == "GOLDPIECE")) != null) { Item tmpItem = inventory.FirstOrDefault(x => x.Name == "Bronzepiece"); if (tmpItem != null) { inventory.Remove(tmpItem); } tmpItem = inventory.FirstOrDefault(x => x.Name == "Silverpiece"); if (tmpItem != null) { inventory.Remove(tmpItem); } tmpItem = inventory.FirstOrDefault(x => x.Name == "Goldpiece"); if (tmpItem != null) { inventory.Remove(tmpItem); } tmpItem = null; var medallion = new Item("Medallion", "A magnificent medallion. Looks ancient and infused with magic.", INames.EMPTY, ItemPos.Room, true); inventory.Add(medallion); Console.Clear(); GFXText.PrintTextWithHighlights("You combine the pieces into a magnificent medallion.", 2, 2, true); Console.Write("\n\n"); return; } else { Console.Clear(); GFXText.PrintTextWithHighlights("You are missing a piece.", 2, 2, true); return; } } } } var breadInRoom = LoadGame.rooms[CurRoom].roomItems.FirstOrDefault(c => c.Name.ToUpper() == "BREAD"); if (breadInRoom != null) // I have exposed the bread, but I could not USE it because I had no space in the inventory { Console.Clear(); GFXText.PrintTextWithHighlights("Try to pickup [Bread] before using it.\n\n", 2, 2, true); } else { Console.WriteLine("Cannot use {0}", item); } // End of logic for using items in inventory }