public Form1() { InitializeComponent(); ttt_Button t_button = new ttt_Button(); this.Controls.Add(t_button); t_button.Location = new Point(25, 25); //initialize the grid with buttons int x = 0, y = 0; for (int r = 0; r < 3; r++) { for (int c = 0; r < 3; c++) { grid[r, c] = new ttt_Button(); this.Controls.Add(grid[r, c]); grid[r, c].Location = new Point(x, y); grid[r, c].Click += new EventHandler(button_Click); y += 100; } x += 100; y = 0; } }
private void button_Click(object sender, EventArgs e) { //capture the sender ttt_Button t = (ttt_Button)sender; if (xTurn) { t.BackColor = Color.RoyalBlue; t.Text = "X"; } else { t.BackColor = Color.Gold; t.Text = "O"; } xTurn = !xTurn; if (checkForWin()) { MessageBox.Show("You are a Winner!"); //reset board int x = 0, y = 0; for (int r = 0; r < 3; r++) { for (int c = 0; r < 3; c++) { grid[r, c] = new ttt_Button(); this.Controls.Add(grid[r, c]); grid[r, c].Location = new Point(x, y); grid[r, c].Click += new EventHandler(button_Click); y += 100; } x += 100; y = 0; } } }