static void Main(string[] args) { // General initializations to prevent magic numbers int mapwidth = 5; int mapheight = 5; int xstartpos = 2; int ystartpos = 0; // Welcome the player Console.WriteLine("Welcome to a textbased adventure"); Console.WriteLine("Before you can start your journey, you will have to enter your name."); string name = null; string input = null; // Check for the correct name // Refactored from do - while to improve readability by Michiel and Alex while(input != "Y") { if( input == null || input == "N" ) { Console.WriteLine("Please enter your name and press enter:"); name = Console.ReadLine(); } Console.WriteLine("Your name is {0}",name); Console.WriteLine("Is this correct? (y/n)"); input = Console.ReadLine(); input = input.ToUpper(); } do { Game.JustDied = false; // Make the player Player player = new Player(name, 100); //Welcome the player Welcome(ref player); // Initialize the map Map map = new Map(mapwidth, mapheight, xstartpos, ystartpos); Game.Map = map; Game.Player = player; // Put the locations with their items on the map InitMap(ref map); // Start the game Start(ref map, ref player); } while (Game.JustDied); // End the program Quit(); }
static void ShowDirections(Map map, ref List<string> items) { map.AllowedDirections(); if (map.GetNorth() == 1) items.Add(Game.MOVE_NORTH); if (map.GetEast() == 1) items.Add(Game.MOVE_EAST); if (map.GetSouth() == 1) items.Add(Game.MOVE_SOUTH); if (map.GetWest() == 1) items.Add(Game.MOVE_WEST); }
static void Start(ref Map map, ref Player player) { List<string> menuItems = new List<string>(); int choice; // Refactored by Michiel and Alex do { Console.Clear(); if (Game.JustDied) break; map.GetLocation().Description(); if (Game.JustDied) break; choice = ShowMenu(map, ref menuItems); if ( choice != menuItems.Count() ) { if ( validDirections.Contains( menuItems[choice] ) ) { map.Move( menuItems[choice] ); } switch ( menuItems[choice] ) { case ACTION_SEARCH: Console.Clear(); foreach (var item in map.GetLocation().GetItems()) { item.Value.Description(); player.PickupItem(item.Value); } player.ShowInventory(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); // Add code to perform an item pickup break; case ACTION_FIGHT: map.GetLocation().Fight(); break; case ACTION_RUN: // Add code for running here break; default: if(map.GetLocation().quiz) { if (map.GetLocation().quizQuestions.Contains(menuItems[choice])) { if(choice == map.GetLocation().quizAnswer) { map.GetLocation().OnQuizGoodAnswer(); } else { map.GetLocation().OnQuizBadAnswer(); } } } break; } } } // When the choice is equal to the total item it means exit has been chosen. while ( choice < menuItems.Count() - 1); }
// This Method builds the menu static int ShowMenu(Map map, ref List<string> menu) { int choice; string input; menu.Clear(); if (!map.GetLocation().quiz) { ShowDirections(map, ref menu); if (map.GetLocation().CheckForItems()) { bool acquirableitems = false; Dictionary<string, Objects> list = map.GetLocation().GetItems(); Objects[] obj = list.Values.ToArray(); for (int i = 0; i < obj.Count(); i++) { if (obj[i].GetAcquirable()) acquirableitems = true; } if (acquirableitems) menu.Add(ACTION_SEARCH); } if (map.GetLocation().HasEnemy()) { menu.Add(ACTION_FIGHT); //menu.Add( ACTION_RUN ); } } else { foreach (var q in map.GetLocation().quizQuestions) { menu.Add(q); } } menu.Add(ACTION_QUIT); do { for (int i = 0; i < menu.Count(); i++) { Console.WriteLine("{0} - {1}", i + 1, menu[i]); } Console.WriteLine("Please enter your choice: 1 - {0}", menu.Count()); input = Console.ReadLine(); } while (!int.TryParse(input, out choice) || (choice > menu.Count() || choice < 0)); //return choice; return ( choice - 1 ); }
static void InitMap(ref Map map) { map.AddLocation(new StartRoom("Start room"), 2, 0); map.AddLocation(new StartRoomBoxes("Start room boxes"), 1, 0); map.AddLocation(new StartRoomShelf("Start room shelf"), 3, 0); map.AddLocation(new StartRoomDoor("Start room door"), 2, 1); }