I have made a tic tac toe game and I am producing another version with less code. Anyway… here is my code… I’ve taken some of the code in between the do while statement but yeah…
var rc = new Random();
do
{
storeRI = rc.Next(1, 9);
if (storeRI == 1 & button1.Text == "")
{
button1.Text = "O";
Turn = 1;
TestWin();
break;
}
else if (storeRI == 2 & button2.Text == "")
{
button2.Text = "O";
Turn = 1;
TestWin();
break;
}
} while (Turn == 2 & button1.Text == "" | button2.Text == "" | button3.Text == "" | button3.Text == "" | button4.Text == "" | button5.Text == "" | button6.Text == "" | button7.Text == "" | button8.Text == "" | button9.Text == "");
}
else
{
MessageBox.Show("It's a draw");
}
I mainly want to focus on this block of code… to shorten down… 🙂
} while (Turn == 2 & button1.Text == "" | button2.Text == "" | button3.Text == "" | button3.Text == "" | button4.Text == "" | button5.Text == "" | button6.Text == "" | button7.Text == "" | button8.Text == "" | button9.Text == "");
Create a List of buttons from your individual buttons and then change it to
In general with 9 buttons they should probably be in an array or list for simpler access:
then you can access it as
buttons[0]instead ofbutton1, etc.I would do this in your Form initializer rather than having a function that does this on-demand.