Spring naar hoofdinhoud
Level 5 - One-dimension arrays and lists 0 van 9 activiteiten opgelost
  1. RPG inventory Nog niet opgelost
  2. Search for life Nog niet opgelost
  3. Two-dice pig Nog niet opgelost
  4. Random name generator Nog niet opgelost
  5. Cup draw Nog niet opgelost
  6. Feud Nog niet opgelost
  7. Darts Nog niet opgelost
  8. Cut the deck Nog niet opgelost
  9. London underground Nog niet opgelost
Volgende reeks Level 6 - Count controlled iterations and scope
Level 5 - One-dimension arrays and lists

RPG inventory

Meld je aan om je oplossing in te dienen en feedback te krijgen.

// RPG inventory program using System; using System.Collections.Generic; class Submission { // ------------------------- // Subprograms // ------------------------- static void add_item(List<string> inventory) { Console.WriteLine("What item are you adding to your inventory?"); string item = Console.ReadLine(); inventory.Add(item); Console.WriteLine("Item added."); } static void search(List<string> inventory) { Console.WriteLine("What item do you want to see if you have?"); string item = Console.ReadLine(); if (inventory.Contains(item)) { Console.WriteLine("You have the item."); } else { Console.WriteLine($"You do not have a {item}"); } } static void drop_item(List<string> inventory) { Console.WriteLine("What item do you want to drop?"); string item = Console.ReadLine(); // Check item exists if (inventory.Contains(item)) { inventory.Remove(item); Console.WriteLine("Item dropped."); } else { Console.WriteLine("You do not have this item."); } } static void look(List<string> inventory) { Console.WriteLine("Which inventory slot do you want to look at?"); int slot = Convert.ToInt32(Console.ReadLine()); if (slot < inventory.Count) { Console.WriteLine($"You have a {inventory[slot]} in slot {slot}"); } else { Console.WriteLine("Invalid slot."); } } // Returns false when the player chooses to quit. static bool choose_action(List<string> inventory) { Console.WriteLine($"You have {inventory.Count} items in your inventory."); Console.WriteLine("What action do you want to take? (add/look/drop/search/quit)"); string action = Console.ReadLine(); switch (action) { case "add": add_item(inventory); break; case "drop": drop_item(inventory); break; case "look": look(inventory); break; case "search": search(inventory); break; case "quit": return false; } Console.WriteLine(); return true; } // ------------------------- // Main program // ------------------------- public static void Main(string[] args) { List<string> inventory = new List<string>() {"sword", "shield"}; while (choose_action(inventory)) { } } }