Hey, I'm pretty new at VB.NET and had gained interest in creating a tic-tac-toe game from scratch.. although i had no idea where to begin so i downloaded some source code... though i need some help decifering it as a lot of it doesn't make sense... mainly what each private sub does and some of the confusing bits in it.. like bytes and chars and whatnot...
I know it's a lot of code to just go and summerize, but any help is appreciated, especially with the math and checking stuff. The form is composed of 9 picturebox controls.. XO 1-5 and thats for the playing board.
Again, any help is appreciated :)
Dim chrXOChar As Char = "X"
Private Sub XO1PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XO9PictureBox.Click, XO6PictureBox.Click, XO8PictureBox.Click, XO5PictureBox.Click, XO7PictureBox.Click, XO4PictureBox.Click, XO3PictureBox.Click, XO2PictureBox.Click, XO1PictureBox.Click
Dim lblClicked As Label
lblClicked = sender
If GetLblText(lblClicked.Tag()) <> Nothing Then Exit Sub
Call SetLblText(lblClicked.Tag(), chrXOChar)
If CheckWin(lblClicked.Tag) = True Then
MessageBox.Show(chrXOChar & " Wins!")
Exit Sub
End If
If chrXOChar = "X" Then
chrXOChar = "O"
Else
chrXOChar = "X"
End If
End Sub
Private Function CheckWin(ByVal bindex As Byte) As Boolean
CheckWin = True
If CheckGrid(1, 3, 1) = True Then Exit Function
If CheckGrid(4, 6, 1) = True Then Exit Function
If CheckGrid(7, 9, 1) = True Then Exit Function
If CheckGrid(1, 7, 3) = True Then Exit Function
If CheckGrid(2, 8, 3) = True Then Exit Function
If CheckGrid(3, 9, 3) = True Then Exit Function
If CheckGrid(1, 9, 4) = True Then Exit Function
If CheckGrid(3, 7, 2) = True Then Exit Function
CheckWin = False
Dim i As Byte
For i = 1 To 9
If GetLblText(i) = Nothing Then
Exit Function
End If
Next
MessageBox.Show("Draw!")
End Function
Private Function CheckGrid(ByVal bfrom As Byte, ByVal bto As Byte, ByVal bstep As Byte) As Boolean
Dim bctr As Byte
Dim i As Byte = 0
Dim c(2) As Char
For bctr = bfrom To bto Step bstep
c(i) = GetLblText(bctr)
i = i + 1
Next
If c(0) = c(1) And c(1) = c(2) Then
If c(0) <> Nothing Then
CheckGrid = True
End If
End If
End Function
Private Sub SetLblText(ByVal bIndex As Byte, ByVal sText As Char)
Select Case bIndex
Case 1 : XO1PictureBox.Text = sText
Case 2 : XO2PictureBox.Text = sText
Case 3 : XO3PictureBox.Text = sText
Case 4 : XO4PictureBox.Text = sText
Case 5 : XO5PictureBox.Text = sText
Case 6 : XO6PictureBox.Text = sText
Case 7 : XO7PictureBox.Text = sText
Case 8 : XO8PictureBox.Text = sText
Case 9 : XO9PictureBox.Text = sText
Case Else
MessageBox.Show("Please contact game designers and quote error #69")
End Select
End Sub
Private Function GetLblText(ByVal bIndex As Byte) As Char
Select Case bIndex
Case 1 : GetLblText = XO1PictureBox.Text
Case 2 : GetLblText = XO2PictureBox.Text
Case 3 : GetLblText = XO3PictureBox.Text
Case 4 : GetLblText = XO4PictureBox.Text
Case 5 : GetLblText = XO5PictureBox.Text
Case 6 : GetLblText = XO6PictureBox.Text
Case 7 : GetLblText = XO7PictureBox.Text
Case 8 : GetLblText = XO8PictureBox.Text
Case 9 : GetLblText = XO9PictureBox.Text
Case Else
MessageBox.Show("Please contact game designers and quote error #96")
End Select
End Function
Private Sub NewGameMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameMenuItem.Click
Dim i As Byte
For i = 1 To 9 Step 1
Call SetLblText(i, Nothing)
Next
End Sub
'Exits the program
Private Sub ExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenuItem.Click
Me.Close()
End Sub