talekin Posted December 5, 2005 Posted December 5, 2005 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 Quote
bri189a Posted December 5, 2005 Posted December 5, 2005 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 :) 'They are declaring a global variable here an initially setting it to 'X' - confusing and not good practice. Dim chrXOChar As Char = "X" 'The handles portion is saying that any of the 9 pictures boxes that are clicked will execute this procedure 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 'Here they are assigning the object that was clicked into a label - which 'in itself is odd because this being called by Picture Box events...not 'great coding practice. Dim lblClicked As Label lblClicked = sender 'This is saying that if the object that was clicked doesn't have a tag 'then to exit the sub. Also it should be lblClicked.Tag not Tag() - it's 'a property not a method. If GetLblText(lblClicked.Tag()) <> Nothing Then Exit Sub 'This is calling a method called SetLblText - there is no reason to use ' the Call statement - this is old VB6 coding style, it should be removed. Call SetLblText(lblClicked.Tag(), chrXOChar) 'This is calling a function called CheckWin where the label tag is being 'passed in as an argument. The ' = True' part is not necessary if the 'function is returning Boolean. If CheckWin(lblClicked.Tag) = True Then 'The shows a message box. MessageBox.Show(chrXOChar & " Wins!") 'This exits the sub...ideally this hole block would be a part of the 'CheckWin method. Exit Sub End If 'This litle end if part is switching the current 'turn' - again with the 'the way the global variable is used and this logic it might be confusing 'to a novice. If chrXOChar = "X" Then chrXOChar = "O" Else chrXOChar = "X" End If End Sub 'This function checks for the winner but doesn't end the game. Private Function CheckWin(ByVal bindex As Byte) As Boolean 'This is setting the value of the return (incorrectly) - this is VB6 'style, and not good style at that. Ideally you would have something 'like: Dim hasWon as Boolean and a whole different flow to check for 'the winner, this isn't a very well written function. CheckWin = True 'All these if's are just look across a Grid (through the CheckGrid 'function) to see if there is a TIC-TAC-TOE win (all across, diag, etc.) 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 'If a win wasn't found this is setting the return (incorrectly) to 'false. CheckWin = False 'This For loop and message box are checking for a draw - horrible 'logic. Dim i As Byte For i = 1 To 9 If GetLblText(i) = Nothing Then Exit Function End If Next MessageBox.Show("Draw!") End Function 'I myself am having a hard time figuring out what the heck they are 'trying to accomplish here...Basically it appears they're checking for ' a winning pattern. 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 'This is setting the X or the O value of the labels 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 'This is getting the X or O value of the labels. 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 'This sets up a new game. 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 'Again Call isn't necessary - it's VB6 legacy style - this is setting 'the default text of the TIC-TAC-TOE boxes. Call SetLblText(i, Nothing) Next End Sub 'Exits the program 'This exits the function Private Sub ExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenuItem.Click Me.Close() End Sub Listen, this is a poorly written program. It has a lot of legacy VB6 coding styles and poor logic. I wouldn't consider this as a good program to jump into and learn VB (or .NET) correctly. It's no wonder you're confused. Quote
bri189a Posted December 5, 2005 Posted December 5, 2005 (edited) Listen, also, I wasn't trying to bash the person who wrote it, it works, it does the job, and that's what counts - it just had some coding preferences that I don't like and I just would like to see you or anyone else learn 'better' practice (mine certaintly aren't perfect), I spent the last half hour writing a TIC-TAC-TOE program for you...hopefully it will be easier to follow.TicTacToe.zip Edited December 5, 2005 by bri189a Quote
talekin Posted December 5, 2005 Author Posted December 5, 2005 thanks a lot, the code is a lot easier to understand :) time to code :cool: Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.