private void playerClick(object sender, EventArgs e) { var button = (Button)sender; // check which button was clicked currentPlayer = Player.X; // set currentPLayer to x button.Text = currentPlayer.ToString(); // change button to x's button.Enabled = false; // make it so they cant click it again button.BackColor = System.Drawing.Color.Cyan; // change background color of the button buttons.Remove(button); // remove button from our list so computer knows not to use it Check(); // check for a winning move Computermoves.Start(); // starts times for computer }
private void Computermove(object sender, EventArgs e) { if (buttons.Count > 0) // only try to click something if there are buttons to click! { int index = rand.Next(buttons.Count); // create rand number between the available buttons buttons[index].Enabled = false; currentPlayer = Player.O; // set player to O buttons[index].Text = currentPlayer.ToString(); // set the text to O on button buttons[index].BackColor = System.Drawing.Color.DarkSalmon; // change background color buttons.RemoveAt(index); // remove this button from list Check(); // check for a winning move Computermoves.Stop(); // stops computer timer } }
private void Check() { //if player wins if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X" || button4.Text == "X" && button5.Text == "X" && button6.Text == "X" || button7.Text == "X" && button9.Text == "X" && button8.Text == "X" || button1.Text == "X" && button4.Text == "X" && button7.Text == "X" || button2.Text == "X" && button5.Text == "X" && button8.Text == "X" || button3.Text == "X" && button6.Text == "X" && button9.Text == "X" || button1.Text == "X" && button5.Text == "X" && button9.Text == "X" || button3.Text == "X" && button5.Text == "X" && button7.Text == "X") { Computermoves.Stop(); MessageBox.Show("You won!"); playerWins++; PlayerWins1.Text = "Player Wins: " + playerWins; resetGame(); } //if comp wins if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O" || button4.Text == "O" && button5.Text == "O" && button6.Text == "O" || button7.Text == "O" && button9.Text == "O" && button8.Text == "O" || button1.Text == "O" && button4.Text == "O" && button7.Text == "O" || button2.Text == "O" && button5.Text == "O" && button8.Text == "O" || button3.Text == "O" && button6.Text == "O" && button9.Text == "O" || button1.Text == "O" && button5.Text == "O" && button9.Text == "O" || button3.Text == "O" && button5.Text == "O" && button7.Text == "O") { Computermoves.Stop(); MessageBox.Show("Computer won!"); computerWins++; Computerwins1.Text = "Computer Wins: " + computerWins; resetGame(); } }