Question

making a rock paper scissors game in C# for college. I want to show message box when the player enters an invalid command (not rock, paper or scissors) . I tryed this but cant get it to work..

        //if player enters wrong command they will 
        //this feedback
        if (textBoxAttack.Text != "rock" || textBoxAttack.Text != "rock" || textBoxAttack.Text != "paper" || textBoxAttack.Text != "Paper" || textBoxAttack.Text != "scissors" || textBoxAttack.Text != "Scissors")
        {
            MessageBox.Show("Not a valid attack"
                           + "\nPlease Enter one of the Following:"
                           + "\nrock"
                           + "\npaper"
                           + "\nscissors");
            textBoxAttack.Text = "";
        }

It works if i just type in one command (eg: if (textBoxAttack.Text != "rock") )

any pointers? Thanks.

Était-ce utile?

La solution 2

Your boolean logic is wrong, because it will always not equal to one of the values, and thus will always be true. Change || to &&, and it should work.

Autres conseils

You need && instead of ||.

However, i would prefer this concise and readable approach:

string[] allowed = { "rock", "paper", "scissors" };
if (!allowed.Contains(textBoxAttack.Text, StringComparer.CurrentCultureIgnoreCase))
{ 
    string msg = string.Format("Not a valid attack{0}Please Enter one of the Following:{0}{1}"
        , Environment.NewLine, string.Join(Environment.NewLine, allowed));
    MessageBox.Show(msg);
    textBoxAttack.Text = "";
}

Use an array, it makes your work easier.Also you can use ToLower method to make a case-insensitive check.

var values = new [] { "rock", "paper", "scissors" };

if(!values.Contains(textBoxAttack.Text.ToLower()) 
{
    // invalid value
}
string text = textBoxAttack.Text.ToLowerInvariant();
if (text.Text != "rock" && text.Text != "paper" && text.Text != "scissors")
{
...
}

Use a dropdownlist/radiobuttons instead.

if (textBoxAttack.Text == "rock" || textBoxAttack.Text == "Paper" || textBoxAttack.Text == "Scissors")
{
//..Do What do you want
}
else
{
MessageBox.Show("Not a valid attack"
                           + "\nPlease Enter one of the Following:"
                           + "\nrock"
                           + "\npaper"
                           + "\nscissors");
            textBoxAttack.Text = "";
}

Use Combo Box or 3 Buttons for this kind of problem.

Try to do the same but for example adding ToLower() at the end of each text attribute. This way you will only need to check 3 (rock, paper or scissors) becauuse the iput will be lower case ;)

And for the messageBox try something like this:

string[] message = { "Not a valid attack", "Please Enter one of the Following:", "rock", "paper", "scissors" };

if (textBoxAttack.Text.ToLower() != "rock" && textBoxAttack.Text.ToLower() != "paper" && textBoxAttack.Text.ToLower() != "scissors")
    {
       MessageBox.Show(string.Join("\n", message));
       textBoxAttack.Text = "";
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top