samedi 26 novembre 2016

Button, if statements and bool causing issues - can't get my head around it :(

Had an issue that I've been trying to work out for the best part of 2 hours now - and im guessing its something simple...

working on a tic tac toe game & got the majority of it working, adding "X" into the boxes on button click ect. However, when I check to see if there are 3 in a row, it doesn't seem to be registering.

                bool winner = false;


            if ((button1.Text == button2.Text) && (button2.Text == button3.Text) && (button1.Equals("X")))
            {
                winner = true;
                if (winner)
                {
                    MessageBox.Show("Awesome");
                }

This does not seem to work at all. However, if I make just 1 change (adding ! before button1.Equals("X")))) as shown below

                bool winner = false;


            if ((button1.Text == button2.Text) && (button2.Text == button3.Text) && (!button1.Equals("X")))
            {
                winner = true;
                if (winner)
                {
                    MessageBox.Show("Awesome");
                }

It works perfectly (in terms of giving me the MessageBox.Show ("Awesome");, which happens before I can play the game as, all the button.text' are blank.

Just incase it makes a difference, I have assigned each button clicked a value in another method (Button_Click, object sender ect...)

if (turn) {
            clickedBtn.Text = "X";
        }
        else
        {
            clickedBtn.Text = "O";
        }

also - all buttons created are dynamic.

Any ideas what could be the problem?

Thanks!

Aucun commentaire:

Enregistrer un commentaire