This game works out the basic of a Tic Tac Toe game. I am looking to see if this can be redone in any way to make the code more smaller since this is really big.
private string player = "x";
private string cpu = "o";
public Form1()
{
InitializeComponent();
Buttons();
Grid();
}
private void Form1_Load(object sender, EventArgs e)
{
}
Button[] buttons;
private bool turn = true;
public void Game(object sender, EventArgs e)
{
Button clicked = (Button)sender;
if (turn)
{
clicked.Text = player;
}
else {
clicked.Text = cpu;
}
turn = !turn;
clicked.Enabled = false;
winners();
}
public void winnersreset()
{
for (int i = 0; i < buttons.Length; i++) {
buttons[i].Enabled = true;
buttons[i].BackColor = Color.White;
buttons[i].Text = "";
}
}
public bool winners()
{
bool winner = false;
if (buttons[0].Text == player && buttons[1].Text == player && buttons[2].Text == player)
{
buttons[0].BackColor = Color.Blue;
buttons[1].BackColor = Color.Blue;
buttons[2].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[3].Text == player && buttons[4].Text == player && buttons[5].Text == player)
{
buttons[3].BackColor = Color.Blue;
buttons[4].BackColor = Color.Blue;
buttons[5].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[6].Text == player && buttons[7].Text == player && buttons[8].Text == player)
{
buttons[6].BackColor = Color.Blue;
buttons[7].BackColor = Color.Blue;
buttons[8].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[0].Text == player && buttons[4].Text == player && buttons[8].Text == player)
{
buttons[0].BackColor = Color.Blue;
buttons[4].BackColor = Color.Blue;
buttons[8].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[2].Text == player && buttons[4].Text == player && buttons[6].Text == player)
{
buttons[2].BackColor = Color.Blue;
buttons[4].BackColor = Color.Blue;
buttons[6].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[0].Text == player && buttons[3].Text == player && buttons[6].Text == player)
{
buttons[0].BackColor = Color.Blue;
buttons[3].BackColor = Color.Blue;
buttons[6].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
else if (buttons[2].Text == player && buttons[5].Text == player && buttons[8].Text == player)
{
buttons[2].BackColor = Color.Blue;
buttons[5].BackColor = Color.Blue;
buttons[8].BackColor = Color.Blue;
MessageBox.Show("X is Winner");
winnersreset();
winner = true;
}
//O winner//
if (buttons[0].Text == cpu && buttons[1].Text == cpu && buttons[2].Text == cpu)
{
buttons[0].BackColor = Color.Red;
buttons[1].BackColor = Color.Red;
buttons[2].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[3].Text == cpu && buttons[4].Text == cpu && buttons[5].Text == cpu)
{
buttons[3].BackColor = Color.Red;
buttons[4].BackColor = Color.Red;
buttons[5].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[6].Text == cpu && buttons[7].Text == cpu && buttons[8].Text == cpu)
{
buttons[6].BackColor = Color.Red;
buttons[7].BackColor = Color.Red;
buttons[8].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[0].Text == cpu && buttons[4].Text == cpu && buttons[8].Text == cpu)
{
buttons[0].BackColor = Color.Red;
buttons[4].BackColor = Color.Red;
buttons[8].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[2].Text == cpu && buttons[4].Text == cpu && buttons[6].Text == cpu)
{
buttons[2].BackColor = Color.Red;
buttons[4].BackColor = Color.Red;
buttons[6].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[0].Text == cpu && buttons[3].Text == cpu && buttons[6].Text == cpu)
{
buttons[0].BackColor = Color.Red;
buttons[3].BackColor = Color.Red;
buttons[6].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
else if (buttons[2].Text == cpu && buttons[5].Text == cpu && buttons[8].Text == cpu)
{
buttons[2].BackColor = Color.Red;
buttons[5].BackColor = Color.Red;
buttons[8].BackColor = Color.Red;
MessageBox.Show("O is winner");
winnersreset();
winner = true;
}
return winner;
}
public void Grid()
{
this.Size = new System.Drawing.Size(300, 300);
buttons[0].Parent = this;
buttons[0].Location = new Point(0, 10);
buttons[1].Parent = this;
buttons[1].Location = new Point(100, 10);
buttons[2].Parent = this;
buttons[2].Location = new Point(200, 10);
buttons[3].Parent = this;
buttons[3].Location = new Point(0, 60);
buttons[4].Parent = this;
buttons[4].Location = new Point(100, 60);
buttons[5].Parent = this;
buttons[5].Location = new Point(200, 60);
buttons[6].Parent = this;
buttons[6].Location = new Point(0, 110);
buttons[7].Parent = this;
buttons[7].Location = new Point(100, 110);
buttons[8].Parent = this;
buttons[8].Location = new Point(200, 110);
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].Click += new EventHandler(Game);
buttons[i].BackColor = Color.White;
}
}
public void Buttons()
{
buttons = new Button[9];
buttons[0] = new Button();
buttons[1] = new Button();
buttons[2] = new Button();
buttons[3] = new Button();
buttons[4] = new Button();
buttons[5] = new Button();
buttons[6] = new Button();
buttons[7] = new Button();
buttons[8] = new Button();
}