Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Public player_turn As Boolean = True
- Public row1 As Integer
- Public row2 As Integer
- Public row3 As Integer
- Public col1 As Integer
- Public col2 As Integer
- Public col3 As Integer
- Public diag1 As Integer
- Public diag2 As Integer
- Public owin As Integer
- Public xwin As Integer
- Public tiecount As Integer
- Public movecounter As Integer
- 'Player indicates who turn it is. True for x . False for O
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- nowplaying.Text = "X"
- End Sub
- Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
- make_move(btn1)
- keep_score(row1, col1, diag1)
- check_win(row1, col1, diag1)
- btn1.Enabled = False
- End Sub
- Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
- make_move(btn2)
- keep_score(row1, col2)
- check_win(row1, col2)
- btn2.Enabled = False
- End Sub
- Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
- make_move(btn3)
- keep_score(row1, col3, diag2)
- check_win(row1, col3, diag2)
- btn3.Enabled = False
- End Sub
- Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
- make_move(btn4)
- keep_score(row2, col1)
- check_win(row2, col1)
- btn4.Enabled = False
- End Sub
- Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
- make_move(btn5)
- keep_score(row2, col2)
- btn5.Enabled = False
- If player_turn = False Then
- diag1 = diag1 + 1
- diag2 = diag2 + 1
- Else
- diag1 = diag1 + 10
- diag2 = diag2 + 10
- End If
- check_win(row2, col2)
- End Sub
- Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
- make_move(btn6)
- keep_score(row2, col3)
- check_win(row2, col3)
- btn6.Enabled = False
- End Sub
- Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
- make_move(btn7)
- keep_score(row3, col1, diag2)
- check_win(row3, col1, diag2)
- btn7.Enabled = False
- End Sub
- Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
- make_move(btn8)
- keep_score(row3, col2)
- check_win(row3, col2)
- btn8.Enabled = False
- End Sub
- Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
- make_move(btn9)
- keep_score(row3, col3, diag1)
- check_win(row3, col3, diag1)
- btn9.Enabled = False
- End Sub
- Private Sub Restart_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Restart_button.Click
- btn1.Text = " "
- btn2.Text = " "
- btn3.Text = " "
- btn4.Text = " "
- btn5.Text = " "
- btn6.Text = " "
- btn7.Text = " "
- btn8.Text = " "
- btn9.Text = " "
- player_turn = True
- nowplaying.Text = "X"
- row1 = 0
- row2 = 0
- row3 = 0
- col1 = 0
- col2 = 0
- col3 = 0
- diag1 = 0
- diag2 = 0
- movecounter = 0
- winlabel.Text = " "
- btn1.Enabled = True
- btn2.Enabled = True
- btn3.Enabled = True
- btn4.Enabled = True
- btn5.Enabled = True
- btn6.Enabled = True
- btn7.Enabled = True
- btn8.Enabled = True
- btn9.Enabled = True
- End Sub
- Function make_move(ByVal btn)
- If player_turn = True Then
- btn.Text = "X"
- nowplaying.Text = "O"
- Else
- btn.Text = "O"
- nowplaying.Text = "X"
- End If
- player_turn = Not player_turn
- movecounter += 1
- Return 0
- End Function
- Function keep_score(ByRef row As Integer, ByRef col As Integer, Optional ByRef diag As Integer = 0) As Integer
- If player_turn = False Then
- row = row + 1
- col = col + 1
- diag = diag + 1
- Else
- row = row + 10
- col = col + 10
- diag = diag + 10
- End If
- Return 0
- End Function
- Function check_win(ByRef row As Integer, ByRef col As Integer, Optional ByRef diag As Integer = 0) As Integer
- If row = 3 Or col = 3 Or diag = 3 Then
- winlabel.Text = "Congratulations! X has won!"
- xwin += 1
- xwin_box.Text = xwin
- disable_buts()
- ElseIf row = 30 Or col = 30 Or diag = 30 Then
- winlabel.Text = "Congratulations! O has won!"
- owin += 1
- owin_box.Text = owin
- disable_buts()
- End If
- If movecounter = 9 Then
- winlabel.Text = "A tie has occured"
- tiecount += 1
- tiebox.Text = tiecount
- End If
- Return 0
- End Function
- Function disable_buts() As Integer
- btn1.Enabled = False
- btn2.Enabled = False
- btn3.Enabled = False
- btn4.Enabled = False
- btn5.Enabled = False
- btn6.Enabled = False
- btn7.Enabled = False
- btn8.Enabled = False
- btn9.Enabled = False
- Return 0
- End Function
- End Class
Add Comment
Please, Sign In to add comment