Hi everyone,

I know there's a section designated for Mobile Development but, that's really dead and It wouldn't hurt anyone if I posted here - It's a C# related question anyway.

I'm making a Naughts and Crosses game on my Windows Mobile - HTC.

The game works fine, I can put the naughts and crosses in well that works brilliantly.

But....I've made an if statement that checks if someone has won and that if statement never gets executed....

As you can imagine, a typical naughts and crosses grid is 3x3, so that's 9 entries. I've made 9 buttons and named them their respective location. Let's concentrate on the right most, middle button:

private void RightBottom_Click(object sender, RoutedEventArgs e)
        {
            if (RightBottom.Content == null) //Check if it is initially empty
            {
                if (whosGo == "X") //Check if it's X's turn
                {
                    RightBottom.Content = "X"; //If it is X's turn AND the content is null, put X inside the button
                    whosGo = "O"; //Swap it back to O's turn

                    if ((MiddleBottom.Content == "X") && (LeftBottom.Content == "X") || (RightMiddle.Content == "X") && (RightTop.Content == "X") || (MiddleMiddle.Content == "X") && (LeftTop.Content == "X")) //This is where it all happens, this if statement NEVER gets called for some reason. Even when I make sure that the buttons DO contain X. 
                    {
                        PageTitle.Text = "X Wins";

                    }
                }

                else if (whosGo == "O")
                {
                    RightBottom.Content = "O";
                    whosGo = "X";

                    if ((MiddleBottom.Content == "O") && (LeftBottom.Content == "O") || (RightMiddle.Content == "O") && (RightTop.Content == "O") || (MiddleMiddle.Content == "O") && (LeftTop.Content == "O"))
                    {
                        PageTitle.Text = "O Wins";

                    }
                }
            }
        }

I ran the debugger and when the compiler reaches the big if statement, it steps over it like it's not true when I clearly made the buttons within the if in to X...No idea why it's not working, no idea at all. The coding is there clear as day...(oh by the way, Silverlight doesn't like Button.Text...It only allows Button.Content for some reason...)

Any help is appreciated, a lot!

Thank you.

It's weird. I have trying it in a Winform app and it did work fine. Although unlikely, the only thing I see is that you might oversee the precedence of the && over the || operator. (Also the options to win the game are not complete in the if)

The app I used was a Winform with 9 labels and a button. All labels referred to label11_Click event. The code of the form:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void label11_Click(object sender, EventArgs e)
        {
            Label oxLabel = ((Label)sender);
            oxLabel.Text = oxLabel.Text == "X" ? "O" : "X";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*if (
                    (MiddleBottom.Content == "X") && (LeftBottom.Content == "X") 
                 || (RightMiddle.Content == "X") && (RightTop.Content == "X") 
                 || (MiddleMiddle.Content == "X") && (LeftTop.Content == "X")
                ) */
            if ((label32.Text == "X") && (label31.Text == "X")
                || (label23.Text == "X") && (label13.Text == "X")
                || (label22.Text == "X") && (label11.Text == "X"))
            {
                MessageBox.Show("It's true");
            }
            else
            {
                MessageBox.Show("It's false");
            }
            
        }
    }

Hey C#Jaap,

I fixed the problem yesterday. You're going to laugh.

In normal C# apps, my code would work perfectly fine. In Silverlght, I have to make some changes.

First, apply casting. Cast the content of the button in to a string variable and then check for similarity. So something like: , if ((string)cmdRightLeft.Content == "X") && ....

Weird but, it works. Apparently, it wasn't wise to compare with just .Content. A casting was required, hence the (string).

Thanks for you help, input and time!

Much appreciated.

Oh okay, you're where comparing an object to a string or something alike.

You're welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.