I'm trying to make a tic tac toe game, however i am having a little trouble making the computer select a random button.
I have looked online for the answer, but i cant find anything about selecting a random button.
here is part of my code as its quite long.
Dim Player1Score As Integer = 0
Dim Player2Score As Integer = 0
Dim player1win As New MessageDialog("Player One Has Won The Game")
Dim player2win As New MessageDialog("Player Two Has Won The Game")
Private Async Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
Newgamebutton.IsEnabled = True
If PlayerLabel.Text = "X" Then
button1.Content = "X"
PlayerLabel.Text = "O"
textBlock1.Text = "It's Your Turn Player: 1"
button1.IsEnabled = False
Else
button1.Content = "O"
PlayerLabel.Text = "X"
textBlock1.Text = "It's Your Turn Player: 2"
button1.IsEnabled = False
End If
If button9.Content = "X" And button5.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
If button5.Content = "X" And button9.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
End If
If button7.Content = "X" And button4.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button4.Content = "X" And button7.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button3.Content = "X" And button2.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
If button2.Content = "X" And button3.Content = "X" And button1.Content = "X" Then
Player1Score += 1
player1label.Text = "Player 1: " & Player1Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player1win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
Else
If button9.Content = "O" And button5.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button5.Content = "O" And button9.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button7.Content = "O" And button4.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button4.Content = "O" And button7.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button3.Content = "O" And button2.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
If button2.Content = "O" And button3.Content = "O" And button1.Content = "O" Then
Player2Score += 1
Player2labell.Text = "Player 2: " & Player2Score
textBlock1.Text = "It's Your Turn Player: 1"
PlayerLabel.Text = "X"
Await player2win.ShowAsync
button1.IsEnabled = True
button2.IsEnabled = True
button3.IsEnabled = True
button4.IsEnabled = True
button5.IsEnabled = True
button6.IsEnabled = True
button7.IsEnabled = True
button8.IsEnabled = True
button9.IsEnabled = True
button1.Content = ""
button2.Content = ""
button3.Content = ""
button4.Content = ""
button5.Content = ""
button6.Content = ""
button7.Content = ""
button8.Content = ""
button9.Content = ""
End If
End If
End Sub
Aucun commentaire:
Enregistrer un commentaire