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.