Tic Tac Toe vs Computer Programming
Hi there I am trying to make a program that allows the user to play tic tac toe with the user but i am hitting an obstacle :confused:.When ever I debug and play my program game it sometimes marks O more than once i have used the Turn1 as a timer I always use the Turn1.stop to stop that timer but it still hasn't fixed my code.Please can some one help!!!!
Mizzeeboy
Here's the Code
HTML Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Button1.Text = "X"
turns.Text = turns.Text + 1
Button1.Enabled = False
Turn1.Start()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Button2.Text = "X"
turns.Text = turns.Text + 1
Button2.Enabled = False
Turn1.Start()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Button3.Text = "X"
turns.Text = turns.Text + 1
Button3.Enabled = False
Turn1.Start()
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Button4.Text = "X"
turns.Text = turns.Text + 1
Button4.Enabled = False
Turn1.Start()
End Sub
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Button5.Text = "X"
turns.Text = turns.Text + 1
Button5.Enabled = False
Turn1.Start()
End Sub
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
Button6.Text = "X"
turns.Text = turns.Text + 1
Button6.Enabled = False
Turn1.Start()
End Sub
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Button7.Text = "X"
turns.Text = turns.Text + 1
Button7.Enabled = False
Turn1.Start()
End Sub
Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
Button8.Text = "X"
turns.Text = turns.Text + 1
Button8.Enabled = False
Turn1.Start()
End Sub
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Button9.Text = "X"
turns.Text = turns.Text + 1
Button9.Enabled = False
Turn1.Start()
End Sub
Private Sub Turn1_Tick(sender As System.Object, e As System.EventArgs) Handles Turn1.Tick
'This is Turn 1 code By the Mizzeeboy
If Button1.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "X" And Button1.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button8.Text = "O"
Button8.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "X" And Button2.Enabled = True And Button1.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button1.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button5.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button1.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button6.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button1.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button1.Enabled = True And Button8.Enabled = True And Button9.Enabled = True Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button8.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button1.Enabled = True And Button9.Enabled = True Then
Button2.Text = "O"
Button2.Enabled = False
Turn1.Stop()
ElseIf Button9.Text = "X" And Button2.Enabled = True And Button3.Enabled = True And Button4.Enabled = True And Button5.Enabled = True And Button6.Enabled = True And Button7.Enabled = True And Button8.Enabled = True And Button1.Enabled = True Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
End If
'The Defending codes are here
'This is Horizontally
If Button1.Text = "" And Button2.Text = "X" And Button3.Text = "X" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button2.Text = "" And Button3.Text = "X" Then
Button2.Text = "O"
Button2.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "" And Button5.Text = "X" And Button6.Text = "X" Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "X" And Button5.Text = "" And Button6.Text = "X" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "" Then
Button6.Text = "O"
Button6.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "" And Button8.Text = "X" And Button9.Text = "X" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "X" And Button8.Text = "" And Button9.Text = "X" Then
Button8.Text = "O"
Button8.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
End If
'This is Vertically
If Button1.Text = "" And Button4.Text = "X" And Button7.Text = "X" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button4.Text = "" And Button7.Text = "X" Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "" And Button5.Text = "X" And Button8.Text = "X" Then
Button2.Text = "O"
Button2.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "X" And Button5.Text = "" And Button8.Text = "X" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "" Then
Button8.Text = "O"
Button8.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "" And Button6.Text = "X" And Button9.Text = "X" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "X" And Button6.Text = "" And Button9.Text = "X" Then
Button6.Text = "O"
Button6.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
End If
'Diagonally
If Button1.Text = "" And Button5.Text = "X" And Button9.Text = "X" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button5.Text = "" And Button9.Text = "X" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "" And Button5.Text = "X" And Button7.Text = "X" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "X" And Button5.Text = "" And Button7.Text = "X" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
End If
'The Wining codes are here
'This is Horizontally
If Button1.Text = "" And Button2.Text = "O" And Button3.Text = "O" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button2.Text = "" And Button3.Text = "O" Then
Button2.Text = "O"
Button2.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
elseIf Button4.Text = "" And Button5.Text = "O" And Button6.Text = "O" Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "O" And Button5.Text = "" And Button6.Text = "O" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "" Then
Button6.Text = "O"
Button6.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "" And Button8.Text = "O" And Button9.Text = "O" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "O" And Button8.Text = "" And Button9.Text = "O" Then
Button8.Text = "O"
Button8.Enabled = False
Turn1.Stop()
ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
End If
'This is Vertically
If Button1.Text = "" And Button4.Text = "O" And Button7.Text = "O" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button4.Text = "" And Button7.Text = "O" Then
Button4.Text = "O"
Button4.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "" And Button5.Text = "O" And Button8.Text = "O" Then
Button2.Text = "O"
Button2.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "O" And Button5.Text = "" And Button8.Text = "O" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "" Then
Button8.Text = "O"
Button8.Enabled = False
Turn1.Stop()
elseIf Button3.Text = "" And Button6.Text = "O" And Button9.Text = "O" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "O" And Button6.Text = "" And Button9.Text = "O" Then
Button6.Text = "O"
Button6.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
End If
'Diagonally
If Button1.Text = "" And Button5.Text = "O" And Button9.Text = "O" Then
Button1.Text = "O"
Button1.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button5.Text = "" And Button9.Text = "O" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "" Then
Button9.Text = "O"
Button9.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "" And Button5.Text = "O" And Button7.Text = "O" Then
Button3.Text = "O"
Button3.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "O" And Button5.Text = "" And Button7.Text = "O" Then
Button5.Text = "O"
Button5.Enabled = False
Turn1.Stop()
ElseIf Button3.Text = "O" And Button5.Text = "O" And Button7.Text = "" Then
Button7.Text = "O"
Button7.Enabled = False
Turn1.Stop()
End If
End Sub
End Class
Re: Tic Tac Toe vs Computer Programming
Why are you using a timer to change turns? Its best to use a boolean variable to control the turns between the player and CPU. Or even player vs player. Also note timers are very inaccurate and inconsistant. And gets worse the more timers you use at once so it should never be used for games
Re: Tic Tac Toe vs Computer Programming
ok then ill keep that in mind thanks
Re: Tic Tac Toe vs Computer Programming
Also another no no is something like this:
turns.Text = turns.Text + 1
Instead you should use an actual variable declared as an Integer up in the General Declarations (before the Subs / Functions are even called up on top in the code window):
vb.net Code:
Dim Turn_Number as Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button.Click
Button.Text = "X"
Turn_Number += 1
Turns.Text = Convert.ToString(Turn_Number)
Button.Enabled = False
End Sub
Re: Tic Tac Toe vs Computer Programming
Hellow, can you show us the Image.? :D