public Ship newShip(bool keepLooping) { Console.Clear(); Menus.WelcomeScreen(); string model = ""; do { try { Console.WriteLine($"\nWallet: {this.GetWallet()} credits"); string[] modelNames = { "Volkswagon Creeper", "Church Van", "Ford F150", "Cadallac Escalade" }; Console.Write($"\nSelect the type of ship you want to purchase:\n" + $"\n1. {modelNames[0]}\t\t1,000 credits" + $"\n2. {modelNames[1]}\t\t\t2,000 credits" + $"\n3. {modelNames[2]}\t\t\t10,000 credits" + $"\n4. {modelNames[3]}\t\t20,000 credits\n\nC: Cancel\n\n>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 4).Contains(selection.GetSelection())) { model = modelNames[selection.GetSelection() - 1]; Ship newShip = new Ship(model); if (this.GetWallet() >= newShip.GetPrice()) { keepLooping = false; } else { Console.Clear(); throw new Exception("\nYou don't have enough credits to purchase the selected model."); } } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); return(new Ship(model)); }
// Executes the trading decisions private static void Trade(Items[] tradingGoods, Character myCharacter, string[] TradeMenu) { string buyMenu = TradeMenu[0]; string sellMenu = TradeMenu[1]; bool keepLooping = true; do { Console.Write("\nSelect from the following options:\n\n1. Buy\n2. Sell\n\nC: Cancel\n\n>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); try { if (selection.GetSelection() == 0) { break; } else if (selection.GetSelection() == 1) { myCharacter.BuyGoods(buyMenu, tradingGoods); } else if (selection.GetSelection() == 2) { myCharacter.SellGoods(sellMenu, tradingGoods); } else { throw new Exception("\nInvalid Entry"); } keepLooping = false; } catch (Exception ex) { Console.Clear(); Console.WriteLine(ex.Message); } } while (keepLooping); }
// Allows the user to buy fuel public void Refuel(bool keepLooping) { // Adjusts the fuel price based on the planet the user is currently on double fuelPrice = 100; Console.Clear(); Menus.WelcomeScreen(); do { try { double tank = this.GetShip().GetTankCapacity(); double fuelLevel = this.GetFuel(); Console.WriteLine($"\nFuel Level:\t\t{fuelLevel} tons" + $"\nTank Capacity:\t\t{tank} tons" + $"\nFuel Price:\t\t{fuelPrice} credits per ton"); Console.Write("\nHow many tons of fuel do you wish to buy?\n\nC: Cancel\n>>>>"); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (selection.GetSelection() > 0 && selection.GetSelection() <= tank - fuelLevel) { if (this.GetWallet() >= selection.GetSelection() * fuelPrice) { // Updating fuel level this.SetFuel(selection.GetSelection()); // Updating the user's wallet this.SetWallet(-selection.GetSelection() * fuelPrice); keepLooping = false; } else { throw new Exception("\nYou don't have enough credits to buy that much fuel. Enter a smaller amount."); } } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); }
// Updates user location and travel time public void Travel(List <SolarSystem> universe) { // Establishing starting coordinates double[] from = this.GetLocation().GetCoordinates(); // Creating destination coordinates double[] to = new double[2]; double distance = 0; double travelTime = 0; double W = this.GetShip().GetSpeed(); List <Planet> destinationList = new List <Planet>(); bool keepLooping = true; do { try { // Creating list of planets that are reachable with the current fuel level destinationList = ReachablePlanets(universe); // building a menu list for planets that can be traveled to string menuList = "\n"; if (destinationList.Count() != 0) { for (int i = 0; i < destinationList.Count(); i++) { menuList += $"{i + 1}. {destinationList[i].GetName()}"; if (((i + 1) % 5 != 0) || (i == 0)) { menuList += "\t"; } else { menuList += "\n"; } } Console.Write($"\nWhere would you like to travel to:\n" + menuList + "\n\nC: Cancel\n\n>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, destinationList.Count()).Contains(selection.GetSelection())) { // initializing destination coordinates to = destinationList[selection.GetSelection() - 1].GetCoordinates(); Planet destination = destinationList[selection.GetSelection() - 1]; // Calculating the distance and time traveled distance = GetDistance(from, to); travelTime = GetTimeElapsed(distance, this.GetShip().GetSpeed()); // Updates the user's travel time this.SetTravelTime(travelTime); // Checks to total time elapsed if (this.GetTravelTime() >= 40) { Environment.Exit(-1); } // Update the fuel level. 1 fuel is used per lightyear of distance. this.SetFuel(-distance); // Updates the user's location this.SetLocation(destination); keepLooping = false; } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } else { Console.WriteLine("\nNo planets are reachable with the current fuel level, Buy more fuel."); keepLooping = false; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); }
// Sell goods public void SellGoods(string sellMenu, Items[] tradingGoods) { bool keepLooping = true; do { try { Console.Write(sellMenu); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 10).Contains(selection.GetSelection())) { // Counts how many of selected items does the user have in his cargo int count = 0; foreach (Items items in this.GetCargo()) { if (items.GetName() == tradingGoods[selection.GetSelection() - 1].GetName()) { count++; } } if (count > 0) { Console.Write("\nHow many units would you like to sell?\n\nC: Cancel\n\n>>> "); MenuSelection quantity = new MenuSelection(Console.ReadLine().Trim()); if (quantity.GetSelection() == 0) { break; } if (quantity.GetSelection() <= count) { // removes the sold item from the user's cargo for (int i = 0; i < quantity.GetSelection(); i++) { this.RemoveCargo(tradingGoods[selection.GetSelection() - 1]); } // Updates the user's wallet. 10% sales tax is added. this.SetWallet(tradingGoods[selection.GetSelection() - 1].GetPrice() * quantity.GetSelection() * this.GetLocation().GetMultiplier() * 1.1); keepLooping = false; } else { int number = 0; foreach (Items items in this.GetCargo()) { if (items.GetName() == tradingGoods[selection.GetSelection() - 1].GetName()) { number++; } } throw new Exception($"\nYou have only {number} {tradingGoods[selection.GetSelection() - 1].GetName()} in your cargo bay."); } } else { keepLooping = false; throw new Exception("\nYou don't have any of the selected goods in the cargo bay."); } } else { throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.Clear(); Console.WriteLine(ex.Message); } } while (keepLooping); }
public void BuyGoods(string buyMenu, Items[] tradingGoods) { bool keepLooping = true; do { try { if (this.GetCargo().Count() != this.GetShip().GetCargoCapacity()) { Console.Write(buyMenu); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 10).Contains(selection.GetSelection())) { Console.Write("\nHow many units would you like to buy?\n\nC: Cancel\n\n>>> "); MenuSelection quantity = new MenuSelection(Console.ReadLine().Trim()); // Checks if there is enough room in the ship's cargo bay if (quantity.GetSelection() == 0) { break; } else if (this.GetCargo().Count() + quantity.GetSelection() <= this.GetShip().GetCargoCapacity()) { if (this.GetWallet() >= quantity.GetSelection() * tradingGoods[selection.GetSelection() - 1].GetPrice()) { // Adds an item to the player's cargo for (int i = 0; i < quantity.GetSelection(); i++) { this.AddCargo(tradingGoods[selection.GetSelection() - 1]); } // Updates the user's wallet this.SetWallet(-quantity.GetSelection() * tradingGoods[selection.GetSelection() - 1].GetPrice() * this.GetLocation().GetMultiplier()); keepLooping = false; } else { throw new Exception("\nYou don't have enough credit to purchase this good."); } } else { throw new Exception("\nThere isn't enough room in the ship's cargo bay for the selected quantity. Select fewer items."); } } else { throw new Exception("\nInvalid Entry"); } } else { keepLooping = false; throw new Exception("\nThe ship's cargo bay is full. Sell some goods before you attempt to purchase more."); } } catch (Exception ex) { Console.Clear(); Console.WriteLine(ex.Message); } } while (keepLooping); }
public Character() { bool keepLooping = true; // enter user's name do { try { Console.Write("\nType your name\n\n>>> "); string input = Console.ReadLine().Trim(); if (input != "") { this.name = input; keepLooping = false; Console.Clear(); } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); //CharacterType keepLooping = true; do { try { Menus.WelcomeScreen(); string[] classArray = { "Traveler", "Hauler", "Merchant" }; Console.Write($"Select your Class, {this.name}:\n\n1. {classArray[0]}\n2. {classArray[1]}\n3. {classArray[2]}\n\n>>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (Enumerable.Range(1, 3).Contains(selection.GetSelection())) { this.classType = classArray[selection.GetSelection() - 1]; keepLooping = false; Console.Clear(); } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); // mark starting point as Earth double[] startingPoint = { 0.0, 0.0 }; this.location = new Planet("Earth", startingPoint, 1); // give starting credits to the player this.wallet = 5000; // Set travel time to zero at the beginning of the game this.travelTime = 0; Menus.WelcomeScreen(); Console.WriteLine($"\nHello {this.GetName()}. Your character has been created and awarded with 5,000 credits to start the game."); Console.WriteLine("Your objective is to travel to as many planets as possible, trading items for as profit."); Console.WriteLine("Your success as an intersellar trader will be measured by your money, total fuel, and distance travelled..."); Console.WriteLine("\nPress Enter to Continue"); Console.ReadLine(); Console.Clear(); // prompt the user to purchase a ship this.ship = newShip(true); Console.Clear(); // Updates user's wallet. SetWallet(-ship.GetPrice()); Console.Clear(); Refuel(true); Console.Clear(); }
private static void ShowActionMenu(Character myCharacter, List <SolarSystem> universe, Items[] tradingGoods, string[] TradeMenu) { bool keepLooping = true; do { bool commandNotExecuted = true; do { Menus.WelcomeScreen(); try { Console.Write("\nSelect from the following options:\n\n1. Status\n2. Trade\n3. Travel to...\n4. Refuel\n5. Change ship\n6. Quit game\n\n>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (Enumerable.Range(1, 6).Contains(selection.GetSelection())) { switch (selection.GetSelection()) { case 1: Console.Clear(); Menus.WelcomeScreen(); myCharacter.ShowStatus(); break; case 2: Console.Clear(); Menus.WelcomeScreen(); Trade(tradingGoods, myCharacter, TradeMenu); break; case 3: Console.Clear(); Menus.WelcomeScreen(); myCharacter.Travel(universe); break; case 4: Console.Clear(); Menus.WelcomeScreen(); myCharacter.Refuel(true); break; case 5: Console.Clear(); Menus.WelcomeScreen(); myCharacter.newShip(true); break; case 6: Console.Clear(); Init i = new Init(); i.Run(); break; } commandNotExecuted = false; } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (commandNotExecuted); Console.WriteLine("\nCommand successfully executed. Press Enter to Continue."); Console.ReadLine(); Console.Clear(); } while (keepLooping); }