Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically I know this is probably so simple.... But I want to have an if statement that checks if the entered word matches 3 values in an array... if they are then they get added to a list..


Below is the code that i'm talking about and also the code for the rest of the class.. (not the entire project, too big)

C#
public static string PlayerSelectsItem(string word, object[] args)
        {
            if (word ==  "torch", "shield", "sword" )
            {
                GameEngine.Instance.RemoveItemFromRoom(word);
                Inventory.AddItem(word);
                Inventory.PrintItems();
                Console.WriteLine();
                return "MOVE_OR_PICK";  // Don't change action
            }
            else
            {
                Console.WriteLine("The item does not exist");
            }
            return "";

        }







<pre lang="cs">public class RoomThree:Room
    {
        // Attributes
        public static string[] _availableItems = new string[] { &quot;torch&quot;, &quot;shield&quot;, &quot;sword&quot; };


        public static ChatLine[] _lines = new ChatLine[]
        {
           // new ChatLine(&quot;START&quot;, &quot;Welcome to Room 3.. There are doors to the #DIRECTIONS#.&quot;, &quot;move+?&quot;, GameEngine.Instance.PlayerMove),
            new ChatLine(&quot;START&quot;, &quot;Welcome to Room 3. There is a table with weapons and a phone ringing on it. &quot; +
                &quot;I would suggest you answer it&quot;, &quot;answer, answer phone&quot;, AnswerPhone),
            new ChatLine(&quot;TABLE&quot;, &quot;There are items on the table&quot;, &quot;look, look table&quot;, LookTable),
            new ChatLine(&quot;FOUND_ITEMS&quot;, &quot;You looked at the table and there #AVAILABLE_ITEMS#.&quot;, &quot;&quot;, PlayerSelectsItem),
            new ChatLine(&quot;MOVE_OR_PICK&quot;, &quot;Move towards the North door, or pick another item, there #AVAILABLE_ITEMS#&quot;, &quot;pick+?&quot;, PlayerSelectsItem),
            new ChatLine(&quot;MOVE_OR_PICK&quot;, &quot;Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#&quot;, &quot;move+?&quot;, GameEngine.Instance.PlayerMove),
            new ChatLine(&quot;MOVE_OR_PICK&quot;, &quot;Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#&quot;, &quot;drop+?&quot;, PlayerDropsItem),


            new ChatLine(&quot;&quot;, &quot;&quot;, &quot;pick+?, select+?, choose+?&quot;, PlayerSelectsItem),
            new ChatLine(&quot;&quot;, &quot;&quot;, &quot;drop+?&quot;, PlayerDropsItem),
            new ChatLine(&quot;&quot;, &quot;&quot;, &quot;move+?&quot;, GameEngine.Instance.PlayerMove)

        };

        // Constructors

        public RoomThree()
            : base(&quot;ROOM_THREE&quot;, _lines,  &quot;START&quot;, _availableItems)
        {
        }

        // Methods


        private static string AnswerPhone(string word, object[] args)
        {
            Console.WriteLine();
            Console.WriteLine(&quot;Hello human. My name is Sauron. I am the leader of this race. You have became a problem for me,&quot; +
                &quot; and for my race. But I see potential in you. I see a great warrior in you.. So I will offer you this deal....&quot; +
                &quot;If you can get to me, I will offer you a commander role leading my army against the demons. But it will be tough, you&quot; +
                &quot; will have to face very experienced fighting orcs and you will have to find your way to me.. But IF you SURVIVE.&quot; +
                &quot; I will make you my second in command. Now there are weapons on the table in front of you, and an orc in the other room.&quot; +
                &quot; Good luck. You will need it.&quot;);

            Console.WriteLine();

            return &quot;TABLE&quot;;
        }

        private static string LookTable(string word, object[] args)
        {
            Console.WriteLine();

            return &quot;FOUND_ITEMS&quot;;
        }

        public static string PlayerSelectsItem(string word, object[] args)
        {
            if (word ==  &quot;torch&quot;, &quot;shield&quot;, &quot;sword&quot; )
            {
                GameEngine.Instance.RemoveItemFromRoom(word);
                Inventory.AddItem(word);
                Inventory.PrintItems();
                Console.WriteLine();
                return &quot;MOVE_OR_PICK&quot;;  // Don&#39;t change action
            }
            else
            {
                Console.WriteLine(&quot;The item does not exist&quot;);
            }
            return &quot;&quot;;

        }

         public static string PlayerDropsItem(string word, object[] args)
        {
            if (Inventory.HasItem(word))
            {
                Inventory.RemoveItem(word);
                GameEngine.Instance.DropItemInRoom(word);
                Console.WriteLine(&quot;Item has been dropped&quot;);
            }
            else
            {
                Console.WriteLine(&quot;You don&#39;t have that item&quot;);
            }

            return &quot;&quot;;  // Don&#39;t change action
        }
    }</pre>
Posted

C#
if((word.equals("torch"))||(word.equals("shield"))||(word.equals("sword")))


U thought about this ?
 
Share this answer
 
You might change from
Quote:
if (word == "torch", "shield", "sword" )
{

to

C#
string [] goodwords = { "torch", "shield", "sword" };

if (Array.IndexOf(goodwords, word) != -1)
{
 
Share this answer
 
C#
if (word ==  "torch" && word== "shield" && word == "sword" )


or

C#
if (word ==  "torch" || word== "shield" || word == "sword" )
 
Share this answer
 
Comments
[no name] 20-Dec-13 4:36am    
Thank you, beleive it or not i have such a complex project going on, and i cant even do something as simple as that hahaha.. thank you mate.
An@nd Rajan10 20-Dec-13 4:39am    
thank you..
[no name] 20-Dec-13 4:38am    
*lol* please tell me which argument "word" does fit:

(word == "torch" && word== "shield" && word == "sword")

??????
An@nd Rajan10 20-Dec-13 4:41am    
what you mean ?
[no name] 20-Dec-13 4:47am    
Do you think that there is a chance that

(word == "torch" && word== "shield" && word == "sword")

ever becomes true?

If yes please please please tell me this magic word. thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900