I've been learning C#, and today I made a simple Tic Tac Toe game. The code is pretty long, and I'm almost 100% positive that this can be done so much better. Please provide some advice, suggestions, and criticism.
byte _clicks = 1;
bool but1clicked, but2clicked, but3clicked, but4clicked, but5clicked, but6clicked, but7clicked, but8clicked, but9clicked = false;
public void ResetGame()
{
but1.Text = null;
but2.Text = null;
but3.Text = null;
but4.Text = null;
but5.Text = null;
but6.Text = null;
but7.Text = null;
but8.Text = null;
but9.Text = null;
but1clicked = false;
but2clicked = false;
but3clicked = false;
but4clicked = false;
but5clicked = false;
but6clicked = false;
but7clicked = false;
but8clicked = false;
but9clicked = false;
}
public void Check()
{
if (but1.Text == "X" && but2.Text == "X" && but3.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but4.Text == "X" && but5.Text == "X" && but6.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but7.Text == "X" && but8.Text == "X" && but9.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but1.Text == "X" && but4.Text == "X" && but7.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but2.Text == "X" && but5.Text == "X" && but8.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but3.Text == "X" && but6.Text == "X" && but9.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but1.Text == "X" && but5.Text == "X" && but9.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
else if (but3.Text == "X" && but5.Text == "X" && but7.Text == "X")
{
MessageBox.Show("Game Over! X Wins!");
ResetGame();
}
if (but1.Text == "O" && but2.Text == "O" && but3.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but4.Text == "O" && but5.Text == "O" && but6.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but7.Text == "O" && but8.Text == "O" && but9.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but1.Text == "O" && but4.Text == "O" && but7.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but2.Text == "O" && but5.Text == "O" && but8.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but3.Text == "O" && but6.Text == "O" && but9.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but1.Text == "O" && but5.Text == "O" && but9.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
else if (but3.Text == "O" && but5.Text == "O" && but7.Text == "O")
{
MessageBox.Show("Game Over! O Wins!");
ResetGame();
}
}
public void Tie()
{
if (but1clicked == true &&
but2clicked == true &&
but3clicked == true &&
but4clicked == true &&
but5clicked == true &&
but6clicked == true &&
but7clicked == true &&
but8clicked == true &&
but9clicked == true)
{
MessageBox.Show("You have tied! The game will now reset!");
ResetGame();
}
}
private void but1_Click(object sender, EventArgs e)
{
if (but1clicked == false)
{
if (Convert.ToBoolean(_clicks % 2))
{
but1.Text = "X";
but1clicked = true;
_clicks++;
}
else
{
but1.Text = "O";
but1clicked = true;
_clicks++;
}
}
else
MessageBox.Show("This box has already been chosen! Please choose another!");
Check();
Tie();
}
There are 9 buttons, just the same as the method but1_click
. I didn't think it was necessary to add them, though.