2
Reply

How to use an array for inventory?

Anthony

Anthony

Aug 23 2008 10:09 AM
4.4k

Okay, more on my text game. I have made every room work just right, so now I just need to add a healthy sprinkling of items to the house. Easy enough, but what if the user wants to check his inventory?
Here is what I have come up with so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
   class Program
   {
         static void Main(string[] args)
         {
            //takes 5 items and turns them into a sentence
            string[] inventory = {"matches", "keys", "a letter", "rat poison", "jerky"};
            Console.WriteLine("You are currently carrying:");
           

               for (int count = 0; count < inventory.Length-1; count++)
               {
                  string text = inventory[count];
                  Console.Write(inventory[count] + ", ");
                  if(count == inventory.Length-2)
                  {
                     Console.Write("and " + inventory[count+1]);
                  }
               }

               Console.ReadLine();

      }
   }
}

Suppose I wanted to drop the keys. I would need to erase "keys" from the array, move everything after keys one space to the left, and reduce Array.Length by one. Any idea how I might do this?


Answers (2)