public partial class Form1 : Form { bool a, b, x, o; string choose; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (x == true) { button1.Text = "X"; } if (o == true) { button1.Text = "O"; } if (button1.Text == "O") x = true; if (button1.Text == "X") o = true; } private void button2_Click(object sender, EventArgs e) { if (x == true) { button2.Text = "X"; } if (o == true) { button2.Text = "O"; } if (button2.Text == "O") x = true; if (button2.Text == "X") o = true; } private void button3_Click(object sender, EventArgs e) { if (x == true) { if (x == true) { button3.Text = "X"; } if (o == true) { button3.Text = "O"; } if (button3.Text == "O") x = true; if (button3.Text == "X") o = true; } } private void button4_Click(object sender, EventArgs e) { if (x == true) { button4.Text = "X"; } if (o == true) { button4.Text = "O"; } if (button4.Text == "O") x = true; if (button4.Text == "X") o = true; } private void button10_Click(object sender, EventArgs e) { x = true; } private void button11_Click(object sender, EventArgs e) { o = true; } } } 

(!) I apologize in advance for such a cumbersome code, but it is needed to understand the essence of the program, and I do not ask you to do the work for me. (!)

I tried to create a game "X - Tac Toe". I wanted to do this: first, the player chooses who first begins (XO). Then after selecting it clicks on one of 9 buttons and this button becomes the selected character (X - O). Then, on subsequent clicks, the program automatically replaces X with O and vice versa. However, it is precisely with these replacements that it does not work and changes everything to either X or O. It seems that, logically, everything should work correctly. What is the problem? Help me please.

    1 answer 1

    To work correctly, you must translate the x and o flags not only to true , but sometimes false .

    For example:

     if (button1.Text == "O") { x = true; o = false; // o = !(x = true); } if (button1.Text == "X") { o = true; x = false; // o = !(x = false); } 

    and also in other cases

    • It worked! BUT clicked - X, clicked - Oh, clicked - ANYTHING Why ??? - navi1893
    • @ navi1893, According to the rules of the forum, questions should not be reduced to solving or completing tasks for the button in button3_Click extra check - Specter
    • corrected! Now I have assigned it to the label: if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") {MessageBox.Show ("X WINS!"); } But it does not display a MessageBox! Why @Spectre? - navi1893 4:03 pm
    • 2
      no idea debazhte =) - Specter 4:16 pm