Skip to main content
Level 5 - One-dimension arrays and lists 0 of 9 activities solved
  1. RPG inventory Not yet solved
  2. Search for life Not yet solved
  3. Two-dice pig Not yet solved
  4. Random name generator Not yet solved
  5. Cup draw Not yet solved
  6. Feud Not yet solved
  7. Darts Not yet solved
  8. Cut the deck Not yet solved
  9. London underground Not yet solved
Next series Level 6 - Count controlled iterations and scope
Level 5 - One-dimension arrays and lists

RPG inventory

Sign in to hand in your solution and get feedback.

// 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)) { } } }