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.

Dani AI

Generated

Good sleuthing by and useful reminders from . The underlying lesson: treating the visual control as the authoritative game state is brittle. In Silverlight the button’s Content is an object (it can be a string, a UIElement, or null) and that makes plain equality checks fragile and easy to misread in the debugger.

A more robust pattern is to keep a small model for the board and use that to decide wins; update the UI from the model. Example approach:

enum Player { None, X, O }

Player[,] board = new Player[3,3];
Button[,] buttons = /* map of 3x3 buttons */;

void MakeMove(int r, int c, Player p)
{
    if (board[r,c] != Player.None) return;
    board[r,c] = p;
    buttons[r,c].Content = p == Player.None ? "" : p.ToString();
    if (IsWinner(p)) PageTitle.Text = p.ToString() + " Wins";
}

bool IsWinner(Player p)
{
    for (int i = 0; i < 3; i++)
        if (board[i,0] == p && board[i,1] == p && board[i,2] == p) return true;
    for (int j = 0; j < 3; j++)
        if (board[0,j] == p && board[1,j] == p && board[2,j] == p) return true;
    if (board[0,0] == p && board[1,1] == p && board[2,2] == p) return true;
    if (board[0,2] == p && board[1,1] == p && board[2,0] == p) return true;
    return false;
}

Troubleshooting tips:

  • Inspect the runtime type of Content (QuickWatch or Immediate: RightBottom.Content?.GetType().FullName) to see what’s actually stored.
  • If comparing UI content is necessary, prefer as string plus string.Equals(..., StringComparison.Ordinal) or store a simple enum/value in Button.Tag to avoid casting pitfalls.
  • Keep operator precedence clear with parentheses when combining && and || in manual checks.

Keeping logic in a tiny model (enum/array) eliminates fragile UI comparisons, makes win detection straightforward, and avoids subtle Silverlight/content-type surprises.

Recommended Answers

All 3 Replies

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.