Hello, this is my first thread in DaniWeb..
I'm still a VB.net beginner and I like programming so much.
I had a problem when I was making Tic Tac Toe code, it was just for practicing so I have created a form and added 9 buttons and named them btn1, btn2 .... till btn9.
I've wrote the code to check for X's or O's turn and many other things but the problem was checking the winner at the end.
Public Class Form1
Dim ReturnTurn As Integer = 1
Dim StopButton(8) As Boolean 'Check Click
Dim CheckWinnerReturn As Boolean
Dim CheckWinnerValue As Integer
'' Buttons Variables
Dim b1, b2, b3, b4, b5, b6, b7, b8, b9 As Integer
Sub CheckTurnTrueValidations(ByVal SenderO As Object, ByVal ButtonNo As Integer)
If StopButton(ButtonNo) = False Then
If ReturnTurn Mod 2 Then
SenderO.text = "X"
ReturnTurn += 1
' CheckWinnerReturn = True
StopButton(ButtonNo) = True
Else
SenderO.text = "O"
ReturnTurn += 1
' CheckWinnerReturn = False
StopButton(ButtonNo) = True
End If
Else
MsgBox("Already played :(")
End If
End Sub
Function CheckWinnerValues()
'If CheckWinnerReturn = True Then
If ReturnTurn Mod 2 Then
CheckWinnerValue = 1
Else
CheckWinnerValue = 2
'End If
End If
Return CheckWinnerValue
End Function
Sub CheckWinner()
If b1 = b2 = b3 OrElse b4 = b5 = b6 OrElse b7 = b8 = b9 _
OrElse b1 = b4 = b7 OrElse b2 = b5 = b8 OrElse b3 = b6 = b9 _
OrElse b1 = b5 = b9 OrElse b3 = b5 = b7 Then
If CheckWinnerValue = 1 Then
MsgBox("X wins")
Else
MsgBox("O wins")
End If
End If
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
CheckTurnTrueValidations(sender, 0)
b1 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
CheckTurnTrueValidations(sender, 1)
b2 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
CheckTurnTrueValidations(sender, 8)
b9 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
CheckTurnTrueValidations(sender, 7)
b8 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
CheckTurnTrueValidations(sender, 6)
b7 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
CheckTurnTrueValidations(sender, 4)
b5 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
CheckTurnTrueValidations(sender, 3)
b4 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
CheckTurnTrueValidations(sender, 2)
b3 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
CheckTurnTrueValidations(sender, 5)
b6 = CheckWinnerValues()
CheckWinner()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
b1 = 11
b2 = 12
b3 = 13
b4 = 14
b5 = 15
b6 = 16
b7 = 17
b8 = 18
b9 = 19
End Sub
End Class
After each click it checks for the winner using: CheckWinner() but I don't know why.
Thanks.