namespace Pizza { internal class PizzaGame { private const int CustomerMapSize = 4; private readonly CustomerMap _customerMap = new CustomerMap(CustomerMapSize); /// /// Starts game. Main coordinator for pizza game. /// It is responsible for showing information, getting data from user and starting to delivery pizza. /// public void Play() { ShowHeader(); string playerName = GetPlayerName(); ShowIntroduction(playerName); ShowMap(); if (AskForMoreDirections()) { ShowMoreDirections(playerName); var playerUnderstands = AskIfPlayerUnderstand(); if (!playerUnderstands) { return; } } StartDelivery(playerName); EndDelivery(playerName); } /// /// Starts with pizza delivering to customers. /// Every 5 deliveries it is asking user whether want to continue in delivering. /// /// Player name which was filled by user. private void StartDelivery(string playerName) { var numberOfDeliveredPizzas = 0; while (true) { numberOfDeliveredPizzas++; string deliverPizzaToCustomer = GetRandomCustomer(); WriteEmptyLine(); Console.WriteLine($"HELLO {playerName}'S PIZZA. THIS IS {deliverPizzaToCustomer}."); Console.WriteLine("\tPLEASE SEND A PIZZA."); DeliverPizzaByPlayer(playerName, deliverPizzaToCustomer); if (numberOfDeliveredPizzas % 5 == 0) { bool playerWantToDeliveryMorePizzas = AskQuestionWithYesNoResponse("DO YOU WANT TO DELIVER MORE PIZZAS?"); if (!playerWantToDeliveryMorePizzas) { WriteEmptyLine(); break; } } } } /// /// Gets random customer for which pizza should be delivered. /// /// Customer name. private string GetRandomCustomer() { int randomPositionOnX = Random.Shared.Next(0, CustomerMapSize); int randomPositionOnY = Random.Shared.Next(0, CustomerMapSize); return _customerMap.GetCustomerOnPosition(randomPositionOnX, randomPositionOnY); } /// /// Delivers pizza to customer by player. It verifies whether player was delivering pizza to correct customer. /// /// Player name which was filled by user. /// Customer name which order pizza. private void DeliverPizzaByPlayer(string playerName, string deliverPizzaToCustomer) { while (true) { string userInput = GetPlayerInput($"\tDRIVER TO {playerName}: WHERE DOES {deliverPizzaToCustomer} LIVE?"); var deliveredToCustomer = GetCustomerFromPlayerInput(userInput); if (string.IsNullOrEmpty(deliveredToCustomer)) { deliveredToCustomer = "UNKNOWN CUSTOMER"; } if (deliveredToCustomer.Equals(deliverPizzaToCustomer)) { Console.WriteLine($"HELLO {playerName}. THIS IS {deliverPizzaToCustomer}, THANKS FOR THE PIZZA."); break; } Console.WriteLine($"THIS IS {deliveredToCustomer}. I DID NOT ORDER A PIZZA."); Console.WriteLine($"I LIVE AT {userInput}"); } } /// /// Gets customer name by user input with customer coordinations. /// /// Input from users - it should represent customer coordination separated by ','. /// If coordinations are correct and customer exists then returns true otherwise false. private string GetCustomerFromPlayerInput(string userInput) { var pizzaIsDeliveredToPosition = userInput? .Split(',') .Select(i => int.TryParse(i, out var customerPosition) ? (customerPosition - 1) : -1) .Where(i => i != -1) .ToArray() ?? Array.Empty(); if (pizzaIsDeliveredToPosition.Length != 2) { return string.Empty; } return _customerMap.GetCustomerOnPosition(pizzaIsDeliveredToPosition[0], pizzaIsDeliveredToPosition[1]); } /// /// Shows game header in console. /// private void ShowHeader() { Console.WriteLine("PIZZA".PadLeft(22)); Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); WriteEmptyLine(3); Console.WriteLine("PIZZA DELIVERY GAME"); WriteEmptyLine(); } /// /// Asks user for name which will be used in game. /// /// Player name. private string GetPlayerName() { return GetPlayerInput("WHAT IS YOUR FIRST NAME:"); } /// /// Shows game introduction in console /// /// Player name which was filled by user. private void ShowIntroduction(string playerName) { Console.WriteLine($"HI, {playerName}. IN THIS GAME YOU ARE TO TAKE ORDERS"); Console.WriteLine("FOR PIZZAS. THEN YOU ARE TO TELL A DELIVERY BOY"); Console.WriteLine("WHERE TO DELIVER THE ORDERED PIZZAS."); WriteEmptyLine(2); } /// /// Shows customers map in console. In this method is used overridden method 'ToString' for getting text representation of customers map. /// private void ShowMap() { Console.WriteLine("MAP OF THE CITY OF HYATTSVILLE"); WriteEmptyLine(); Console.WriteLine(_customerMap.ToString()); Console.WriteLine("THE OUTPUT IS A MAP OF THE HOMES WHERE"); Console.WriteLine("YOU ARE TO SEND PIZZAS."); WriteEmptyLine(); Console.WriteLine("YOUR JOB IS TO GIVE A TRUCK DRIVER"); Console.WriteLine("THE LOCATION OR COORDINATES OF THE"); Console.WriteLine("HOME ORDERING THE PIZZA."); WriteEmptyLine(); } /// /// Asks user if needs more directions. /// /// True if user need more directions otherwise false. private bool AskForMoreDirections() { var playerNeedsMoreDirections = AskQuestionWithYesNoResponse("DO YOU NEED MORE DIRECTIONS?"); WriteEmptyLine(); return playerNeedsMoreDirections; } /// /// Shows more directions. /// /// Player name which was filled by user. private void ShowMoreDirections(string playerName) { Console.WriteLine("SOMEBODY WILL ASK FOR A PIZZA TO BE"); Console.WriteLine("DELIVERED. THEN A DELIVERY BOY WILL"); Console.WriteLine("ASK YOU FOR THE LOCATION."); Console.WriteLine("\tEXAMPLE:"); Console.WriteLine("THIS IS J. PLEASE SEND A PIZZA."); Console.WriteLine($"DRIVER TO {playerName}. WHERE DOES J LIVE?"); Console.WriteLine("YOUR ANSWER WOULD BE 2,3"); } /// /// Asks user if understands to instructions. /// /// True if user understand otherwise false. private bool AskIfPlayerUnderstand() { var playerUnderstands = AskQuestionWithYesNoResponse("UNDERSTAND?"); if (!playerUnderstands) { Console.WriteLine("THIS JOB IS DEFINITELY TOO DIFFICULT FOR YOU. THANKS ANYWAY"); return false; } WriteEmptyLine(); Console.WriteLine("GOOD. YOU ARE NOW READY TO START TAKING ORDERS."); WriteEmptyLine(); Console.WriteLine("GOOD LUCK!!"); WriteEmptyLine(); return true; } /// /// Shows message about ending delivery in console. /// /// Player name which was filled by user. private void EndDelivery(string playerName) { Console.WriteLine($"O.K. {playerName}, SEE YOU LATER!"); WriteEmptyLine(); } /// /// Gets input from user. /// /// Question which is displayed in console. /// User input. private string GetPlayerInput(string question) { Console.Write($"{question} "); while (true) { var userInput = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(userInput)) { return userInput; } } } /// /// Asks user with required resposne 'YES', 'Y, 'NO', 'N'. /// /// Question which is displayed in console. /// True if user write 'YES', 'Y'. False if user write 'NO', 'N'. private static bool AskQuestionWithYesNoResponse(string question) { var possitiveResponse = new string[] { "Y", "YES" }; var negativeResponse = new string[] { "N", "NO" }; var validUserInputs = possitiveResponse.Concat(negativeResponse); Console.Write($"{question} "); string? userInput; while (true) { userInput = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(userInput) && validUserInputs.Contains(userInput.ToUpper())) { break; } Console.Write($"'YES' OR 'NO' PLEASE, NOW THEN, {question} "); } return possitiveResponse.Contains(userInput.ToUpper()); } /// /// Writes empty line in console. /// /// Number of empty lines which will be written in console. Parameter is optional and default value is 1. private void WriteEmptyLine(int numberOfEmptyLines = 1) { for (int i = 0; i < numberOfEmptyLines; i++) { Console.WriteLine(); } } } }