locked
TIc Tac Toe RRS feed

  • Question

  • Some how Ive got my self lost in my own logic lol Im attempting to make tic tac toe using the GDI as a way to pratice with my self using the GDI. so Im a bit lost and Im getting a blue line syntax error on my loop.

     

    Public Class Form1
        Dim BoxSize As Integer = 64
        Dim BoxMax As Integer = 8
        Dim BoxCols As Integer = 3
        Dim BoxRows As Integer = 3
        Dim Boxs(BoxMax) As PictureBox
        Dim Turn As Integer
        Dim X As Integer
        Dim O As Integer
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             For I As Integer 0 to BoxMax
                Dim Pb As PictureBox = New PictureBox
                Pb.Size = New Size(BoxSize, BoxSize)
                Pb.Left = (I Mod BoxCols) * BoxSize
                Pb.Top = ((I \ BoxCols) Mod BoxRows) * BoxSize
                Pb.SizeMode = PictureBoxSizeMode.StretchImage
                Pb.BringToFront()
                Pb.BackColor = Color.Transparent
                Pb.Location = New System.Drawing.Point((I Mod BoxCols) * BoxSize + 64, ((I \ BoxCols) Mod BoxRows) * BoxSize + 64)
    
                If Turn = 0 Then
                    Pb.BackgroundImage = My.Resources.X
                    MsgBox("X Turn")
                ElseIf Turn = 1 Then
                    Pb.BackgroundImage = My.Resources.O
                    Turn -= 1
                    MsgBox("O turn")
                End If
                AddHandler Pb.Click, AddressOf Box_Click
                Me.Controls.Add(Pb)
            Next
        End Sub
    
        Public Sub Box_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            Dim I As Integer = Array.IndexOf(Boxs, B)
        End Sub
    End Class
    
    

     

    Friday, January 27, 2012 7:33 PM

Answers

  • You need adjust that line for the changeI posted above.

    Tne other error I was referring to was the failure to update Turn in this code:
               If Turn = 0 Then
                    Pb.BackgroundImage = My.Resources.X
                   MsgBox("X Turn")
               ElseIf Turn = 1 Then
                   Pb.BackgroundImage = My.Resources.O
                   Turn -= 1
                   MsgBox("O turn")
               End If

    If you were doing it with GDI then you would draw the images rather than updating a picturebox. It gives you much more room for effects. A GDI example is:

    Public Class Form1
        
    'EXAMPLE
        'TicTacToe Game
        Dim Cols As Integer = 3
        
    Dim rows As Integer = 3
        
    Dim Wide As Integer = 60
        
    Dim High As Integer = 60
        
    Dim Square(rows, Cols) As Integer

        Dim Turn As Integer = 1 'X plays first

        Private Sub Form1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Click
            
    Dim P As Point = Me.PointToClient(MousePosition)
            
    Dim I As Integer = P.X \ Wide
            
    Dim J As Integer = P.Y \ High
            
    If I < Cols And J < rows Then
                If Square(I, J) = 0 Then
                    Square(I, J) = Turn
                    Turn *= -1
                    
    Me.Invalidate()
                
    End If
            End If
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Me.KeyPreview = True
        End Sub

        Private Sub Form1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            
    Dim P As Pen = New Pen(Color.Black, Wide \ 5)
            P.EndCap = Drawing2D.LineCap.Round
            P.StartCap = Drawing2D.LineCap.Round
            
    For I As Integer = 0 To Cols - 1
                
    For j As Integer = 0 To rows - 1
                    
    If Square(I, j) = 1 Then
                        'Draw X
                        e.Graphics.DrawLine(P, I * Wide + (Wide \ 10), j * High + (High \ 10), (I + 1) * Wide - (Wide \ 10), (j + 1) * High - (High \ 10))
                        e.Graphics.DrawLine(P, (I + 1) * Wide - (Wide \ 10), j * High + (High \ 10), I * Wide + (Wide \ 10), (j + 1) * High - (High \ 10))
                    
    ElseIf Square(I, j) = -1 Then
                        'Draw O
                        e.Graphics.DrawEllipse(P, New Rectangle(I * Wide + (Wide \ 10), j * High + (High \ 10), Wide * 0.8, High * 0.8))
                    
    End If
                Next
            Next
            P.Dispose()
        
    End Sub

    End Class

    • Edited by Acamar Saturday, January 28, 2012 12:25 AM sp
    • Marked as answer by Mike Feng Monday, January 30, 2012 9:44 AM
    Saturday, January 28, 2012 12:17 AM
  • You have made some additional changes which are incorrect. 

    When you create the pictureboxes, you must ensure that each one is created as a different reference.  To do this, the variable you use for the reference (PB) must be declared within the loop.  To see what's happening, set the border style of each picture box (eg, to FixedSingle).  You will see that there is only one being created. 

    You probably made that change because of an error you got when you moved the keystroke code into its own routine - that was the wrong fix.  The correct fix is as I mentioned above.  You have to be sure that the picturebox image is updated using the reference to the picturebox that you calculate at the start of the click handler. 

    Also, each picture box must be connected to the handler, and each one must be added to the controls collection.  Your original code was correct except that the code from If to End If had to be moved out.

    With those fixes, the code works.

    Testing for a win is tricky because you are not keeping track of the moves. One solution is:
    - Create an array of integer to match the pictureboxes
    - Put a reference number (0 to 8) in the tag property of each picturebox
    - When the image is written, update the array with a value to indicate X or O, using the tag value to give you the array index
    - To test for a win
      - Create patterns that identify a win, eg 1,1,1,0,0,0,0,0,0   0,0,0,1,1,1,0,0,0  etc where 0 means 'don't care'
      - Compare the array to each pattern

    For instance:
    comparing  X,O,X,X,O,O,X,-,-  with the pattern 1,0,0,1,0,0,1,0,0 indicates a win for X.

    • Marked as answer by Mike Feng Monday, January 30, 2012 9:44 AM
    Saturday, January 28, 2012 7:00 AM

All replies

  • You haven't indicated the line at which the error occurs or the message that is associated with the error. However this code:

                If Turn = 0 Then
                    Turn = 1
                    Pb.BackgroundImage = My.Resources.X
                    MsgBox(
    "X Turn")
                
    ElseIf Turn = 1 Then
                    Pb.BackgroundImage = My.Resources.O
                    Turn = 0
                    MsgBox(
    "O turn")
                
    End If

    should be in the click event and not in the form load event because you want it to execute when the user clicks on one of the picture boxes to indicate where the X or O is to go.    You have also calculated the position of the box twice, and you have not added the box to the array of boxes.

    Friday, January 27, 2012 7:59 PM
  • Woopps lol O rewrote it

    Full Source

    Public Class Form1
        Dim BoxSize As Integer = 64
        Dim BoxMax As Integer = 8
        Dim BoxCols As Integer = 3
        Dim BoxRows As Integer = 3
        Dim Boxs(BoxMax) As PictureBox
        Dim Pb As PictureBox = New PictureBox
        Dim Turn As Integer
        Dim X As Integer
        Dim O As Integer
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             For I As Integer 0 to BoxMax
                Pb.Size = New Size(BoxSize, BoxSize)
                Pb.Left = (I Mod BoxCols) * BoxSize
                Pb.Top = ((I \ BoxCols) Mod BoxRows) * BoxSize
                Pb.SizeMode = PictureBoxSizeMode.StretchImage
                Pb.BringToFront()
                Pb.BackColor = Color.Transparent
                Pb.Location = New System.Drawing.Point((I Mod BoxCols) * BoxSize + 64, ((I \ BoxCols) Mod BoxRows) * BoxSize + 64)
            Next
        End Sub
    
        Public Sub Box_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            Dim I As Integer = Array.IndexOf(Boxs, B)
    
            If Turn = 0 Then
                Pb.BackgroundImage = My.Resources.X
                MsgBox("X Turn")
            ElseIf Turn = 1 Then
                Pb.BackgroundImage = My.Resources.O
                Turn -= 1
                MsgBox("O turn")
            End If
            AddHandler Pb.Click, AddressOf Box_Click
            Me.Controls.Add(Pb)
        End Sub
    End Class
    
    

    Here is the line that gives a error. Its saying I have a syntax error

    For I As Integer 0 to BoxMax
    



    Friday, January 27, 2012 8:18 PM
  • Sorry - I assumed that was just a typo.  Use

            For I As Integer = 0 To BoxMax

     
    Your loop now refers to the wrong variable, and you need to add the pictureboxes to the array as you create them. You are not indicating that the "X" turn has occurred.

    • Edited by Acamar Friday, January 27, 2012 9:05 PM sp
    Friday, January 27, 2012 9:04 PM
  • You only need to use AddHandler Pb.Click once, in the Form1_Load handler, as you had before.

    --
    Andrew

    Friday, January 27, 2012 9:24 PM
  • This caught my attention when I saw it earlier, so I thought I'd put my own together. This doesn't do any sort of "scoring", it's just for display but in case you might find some use from it, here it is:

     

     

    As you can see above, there are nine picture boxes placed on the form and the background for the form shows a grid. The images that I used in this are on my site here, so feel free to use them if you want them.

    Unseen in that screenshot above are also nine context menus which are shown in the components tray:

     

    Most of everything is set in properties, not code. It doesn't matter what you call them but if you name them something similar to its corresponsing picturebox, obviously it'll make things more evident when you do the coding for it. The code follows:


    Code Snippet

        Private Sub ContextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles A1_X.Click, A1_O.Click, B1_X.Click, B1_O.Click, C1_X.Click, C1_O.Click, _
                    A2_X.Click, A2_O.Click, B2_X.Click, B2_O.Click, C2_X.Click, C2_O.Click, _
                    A3_X.Click, A3_O.Click, B3_X.Click, B3_O.Click, C3_X.Click, C3_O.Click
    
            ' This event fires when any item of any of the nine context menus
            ' are clicked.
    
            Dim tsmi As System.Windows.Forms.ToolStripMenuItem = _
               DirectCast(sender, System.Windows.Forms.ToolStripMenuItem)
    
            Select Case tsmi.Name
                Case "A1_X"
                    pb_A1.Image = My.Resources.Symbol_X
                Case "A1_O"
                    pb_A1.Image = My.Resources.Symbol_O
                Case "B1_X"
                    pb_B1.Image = My.Resources.Symbol_X
                Case "B1_O"
                    pb_B1.Image = My.Resources.Symbol_O
                Case "C1_X"
                    pb_C1.Image = My.Resources.Symbol_X
                Case "C1_O"
                    pb_C1.Image = My.Resources.Symbol_O
                Case "A2_X"
                    pb_A2.Image = My.Resources.Symbol_X
                Case "A2_O"
                    pb_A2.Image = My.Resources.Symbol_O
                Case "B2_X"
                    pb_B2.Image = My.Resources.Symbol_X
                Case "B2_O"
                    pb_B2.Image = My.Resources.Symbol_O
                Case "C2_X"
                    pb_C2.Image = My.Resources.Symbol_X
                Case "C2_O"
                    pb_C2.Image = My.Resources.Symbol_O
                Case "A3_X"
                    pb_A3.Image = My.Resources.Symbol_X
                Case "A3_O"
                    pb_A3.Image = My.Resources.Symbol_O
                Case "B3_X"
                    pb_B3.Image = My.Resources.Symbol_X
                Case "B3_O"
                    pb_B3.Image = My.Resources.Symbol_O
                Case "C3_X"
                    pb_C3.Image = My.Resources.Symbol_X
                Case "C3_O"
                    pb_C3.Image = My.Resources.Symbol_O
            End Select
    
        End Sub
    


     

     

     

     

    For what it's worth.

    :)

    Friday, January 27, 2012 9:46 PM
  • @ Frank sorry man but Im attempting to use the GDI. Your method is pretty easy to accomplish. Im trying to do it using code instead of using picture boxes.

     

    @Acamar, I thought this was building the "grid" and the click event was defining it or not it was x or o. OOO I see as weil i need to tell the machine who turn is first correct?



    Edit:

    Ok made some adjustments and added comments. I hope this helps solve the problem. Still not sure what I did wrong Acamar. But I hope you can explain what I did wrong to me lol

     

    Public Class Form1
        Dim BoxSize As Integer = 64
        Dim BoxMax As Integer = 8
        Dim BoxCols As Integer = 3
        Dim BoxRows As Integer = 3
        Dim Boxs(BoxMax) As PictureBox
        Dim Pb As PictureBox = New PictureBox
        Dim Turn As Integer
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Defualt Turn
            Turn = 0
            MsgBox("X Goes First")
    
            'Build Grid
            For I As Integer 0 to BoxMax
                Pb.Size = New Size(BoxSize, BoxSize)
                Pb.Left = (I Mod BoxCols) * BoxSize
                Pb.Top = ((I \ BoxCols) Mod BoxRows) * BoxSize
                Pb.SizeMode = PictureBoxSizeMode.StretchImage
                Pb.BringToFront()
                Pb.BackColor = Color.Transparent
                Pb.Location = New System.Drawing.Point((I Mod BoxCols) * BoxSize + 64, ((I \ BoxCols) Mod BoxRows) * BoxSize + 64)
            Next
            AddHandler Pb.Click, AddressOf Box_Click
            Me.Controls.Add(Pb)
        End Sub
    
        Public Sub Box_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            Dim I As Integer = Array.IndexOf(Boxs, B)
    
            'Click Events
            If Turn = 0 Then
                Pb.BackgroundImage = My.Resources.X
                Turn += 1
                MsgBox("O Turn")
            ElseIf Turn = 1 Then
                Pb.BackgroundImage = My.Resources.O
                Turn -= 1
                MsgBox("X turn")
            End If
        End Sub
    End Class
    

     

    Friday, January 27, 2012 10:02 PM
  • @Acamar, I thought this was building the "grid" and the click event was defining it or not it was x or o. OOO I see as weil i need to tell the machine who turn is first correct?

    The click event is testing to see whose turn it is.  When it is the X turn, you indicate that the O turn is next.  But when it is the O turn, you do not indicate that it is the X turn next.

    I don't quite understand you comment about picture boxes.  Your code uses the picture boxes and images - not GDI.

    You should consider how you are going to detect whether or not there was a winner.  This may affect what you are doing at this stage.

    Friday, January 27, 2012 10:14 PM
  • I can switch it over to a image. Im trying to build this of course with out using the GUI on the forum. which was i thought was the main point of GDI. sorry did not know GDI you must use image instead of PB. Anyway still ever a syntax error on line

     

    For I As Integer 0 to BoxMax
    
    

    Not sure what or how to fix that issue.


    The win process I was going to ad in later. Right now I was trying to get it to load with no errors.
    Friday, January 27, 2012 11:00 PM
  • You need adjust that line for the changeI posted above.

    Tne other error I was referring to was the failure to update Turn in this code:
               If Turn = 0 Then
                    Pb.BackgroundImage = My.Resources.X
                   MsgBox("X Turn")
               ElseIf Turn = 1 Then
                   Pb.BackgroundImage = My.Resources.O
                   Turn -= 1
                   MsgBox("O turn")
               End If

    If you were doing it with GDI then you would draw the images rather than updating a picturebox. It gives you much more room for effects. A GDI example is:

    Public Class Form1
        
    'EXAMPLE
        'TicTacToe Game
        Dim Cols As Integer = 3
        
    Dim rows As Integer = 3
        
    Dim Wide As Integer = 60
        
    Dim High As Integer = 60
        
    Dim Square(rows, Cols) As Integer

        Dim Turn As Integer = 1 'X plays first

        Private Sub Form1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Click
            
    Dim P As Point = Me.PointToClient(MousePosition)
            
    Dim I As Integer = P.X \ Wide
            
    Dim J As Integer = P.Y \ High
            
    If I < Cols And J < rows Then
                If Square(I, J) = 0 Then
                    Square(I, J) = Turn
                    Turn *= -1
                    
    Me.Invalidate()
                
    End If
            End If
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Me.KeyPreview = True
        End Sub

        Private Sub Form1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            
    Dim P As Pen = New Pen(Color.Black, Wide \ 5)
            P.EndCap = Drawing2D.LineCap.Round
            P.StartCap = Drawing2D.LineCap.Round
            
    For I As Integer = 0 To Cols - 1
                
    For j As Integer = 0 To rows - 1
                    
    If Square(I, j) = 1 Then
                        'Draw X
                        e.Graphics.DrawLine(P, I * Wide + (Wide \ 10), j * High + (High \ 10), (I + 1) * Wide - (Wide \ 10), (j + 1) * High - (High \ 10))
                        e.Graphics.DrawLine(P, (I + 1) * Wide - (Wide \ 10), j * High + (High \ 10), I * Wide + (Wide \ 10), (j + 1) * High - (High \ 10))
                    
    ElseIf Square(I, j) = -1 Then
                        'Draw O
                        e.Graphics.DrawEllipse(P, New Rectangle(I * Wide + (Wide \ 10), j * High + (High \ 10), Wide * 0.8, High * 0.8))
                    
    End If
                Next
            Next
            P.Dispose()
        
    End Sub

    End Class

    • Edited by Acamar Saturday, January 28, 2012 12:25 AM sp
    • Marked as answer by Mike Feng Monday, January 30, 2012 9:44 AM
    Saturday, January 28, 2012 12:17 AM
  • You need adjust that line for the changeI posted above.

    Tne other error I was referring to was the failure to update Turn in this code:
               If Turn = 0 Then
                    Pb.BackgroundImage = My.Resources.X
                   MsgBox("X Turn")
               ElseIf Turn = 1 Then
                   Pb.BackgroundImage = My.Resources.O
                   Turn -= 1
                   MsgBox("O turn")
               End If

    If you were doing it with GDI then you would draw the images rather than updating a picturebox. It gives you much more room for effects. A GDI example is:

    Public Class Form1
        
    'EXAMPLE
        'TicTacToe Game
        Dim Cols As Integer = 3
        
    Dim rows As Integer = 3
        
    Dim Wide As Integer = 60
        
    Dim High As Integer = 60
        
    Dim Square(rows, Cols) As Integer

        Dim Turn As Integer = 1 'X plays first

        Private Sub Form1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Click
            
    Dim P As Point = Me.PointToClient(MousePosition)
            
    Dim I As Integer = P.X \ Wide
            
    Dim J As Integer = P.Y \ High
            
    If I < Cols And J < rows Then
                If Square(I, J) = 0 Then
                    Square(I, J) = Turn
                    Turn *= -1
                    
    Me.Invalidate()
                
    End If
            End If
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    Me.KeyPreview = True
        End Sub

        Private Sub Form1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            
    For I As Integer = 0 To Cols - 1
                
    Dim P As Pen = New Pen(Color.Black, Wide \ 5)
                P.EndCap = Drawing2D.LineCap.Round
                P.StartCap = Drawing2D.LineCap.Round
                
    For j As Integer = 0 To rows - 1
                    
    If Square(I, j) = 1 Then
                        'Draw X
                        e.Graphics.DrawLine(P, I * Wide + (Wide \ 10), j * High + (High \ 10), (I + 1) * Wide - (Wide \ 10), (j + 1) * High - (High \ 10))
                        e.Graphics.DrawLine(P, (I + 1) * Wide - (Wide \ 10), j * High + (High \ 10), I * Wide + (Wide \ 10), (j + 1) * High - (High \ 10))
                    
    ElseIf Square(I, j) = -1 Then
                        'Draw O
                        e.Graphics.DrawEllipse(P, New Rectangle(I * Wide + (Wide \ 10), j * High + (High \ 10), Wide * 0.8, High * 0.8))
                    
    End If
                Next
                P.Dispose()
            
    Next
        End Sub
    End
     Class

    Saturday, January 28, 2012 12:17 AM
  • Argg fixed, now I have a issue with it not populating all the boxes.

     

    Public Class Form1
        Dim BoxSize As Integer = 64
        Dim BoxMax As Integer = 8
        Dim BoxCols As Integer = 3
        Dim BoxRows As Integer = 3
        Dim Boxs(BoxMax) As PictureBox
        Dim Pb As PictureBox = New PictureBox
        Dim Turn As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Defualt Turn
            Turn = 0
            MsgBox("X Goes First")
            'Build Grid
            For I As Integer = 0 To BoxMax
                Pb.Size = New Size(BoxSize, BoxSize)
                Pb.Left = (I Mod BoxCols) * BoxSize
                Pb.Top = ((I \ BoxCols) Mod BoxRows) * BoxSize
                Pb.SizeMode = PictureBoxSizeMode.StretchImage
                Pb.BringToFront()
                Pb.BackColor = Color.Transparent
                Pb.Location = New System.Drawing.Point((I Mod BoxCols) * BoxSize + 64, ((I \ BoxCols) Mod BoxRows) * BoxSize + 64)
            Next
            AddHandler Pb.Click, AddressOf Box_Click
            Me.Controls.Add(Pb)
        End Sub
    
        Public Sub Box_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            Dim I As Integer = Array.IndexOf(Boxs, B)
            'Click Events
            If Turn = 0 Then
                Pb.BackgroundImage = My.Resources.X
                Turn += 1
                MsgBox("O Turn")
            ElseIf Turn = 1 Then
                Pb.BackgroundImage = My.Resources.O
                Turn -= 1
                MsgBox("X turn")
            End If
        End Sub
    End Class
    

     

    Saturday, January 28, 2012 12:47 AM
  • This code can't be right:

            Dim B As PictureBox = CType(sender, PictureBox)
            Dim I As Integer = Array.IndexOf(Boxs, B)
            'Click Events
            If Turn = 0 Then
                Pb.BackgroundImage = My.Resources.X
                Turn += 1
                MsgBox("O Turn")
            ElseIf Turn = 1 Then
                Pb.BackgroundImage = My.Resources.O
                Turn -= 1
                MsgBox("X turn")
            End If
        End Sub
    You create B to refer to the picturebox that was clcked, but then update a different picture box with the result!

    Saturday, January 28, 2012 1:08 AM
  • Sorry got a little side tracked. Yeah likely not correct. I was trying to use the other program you helped me make beofre using it s a template. This is as far as Ive got now. Still hitting brick walls and trying to figure out how to do the win thing now. Id like to do it based off the array but again not sure how. Plus the boxes are not populating correctly.

     

    Public Class Form1
        Dim BoxSize As Integer = 64
        Dim BoxMax As Integer = 8
        Dim BoxCols As Integer = 3
        Dim BoxRows As Integer = 3
        Dim Boxes(BoxMax) As PictureBox
        Dim Pb As PictureBox = New PictureBox
        Dim Turn As Integer
        Dim Value As Integer
        Dim Win As Boolean
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Defualt Turn
            Turn = 0
            MsgBox("X Goes First")
            'Build Grid
            For I As Integer = 0 To BoxMax
                Pb.Size = New Size(BoxSize, BoxSize)
                Pb.Left = (I Mod BoxCols) * BoxSize
                Pb.Top = ((I \ BoxCols) Mod BoxRows) * BoxSize
                Pb.SizeMode = PictureBoxSizeMode.StretchImage
                Pb.BringToFront()
                Pb.BackColor = Color.Transparent
                Pb.Location = New System.Drawing.Point((I Mod BoxCols) * BoxSize + 64, ((I \ BoxCols) Mod BoxRows) * BoxSize + 64)
            Next
            AddHandler Pb.Click, AddressOf Box_Click
            Me.Controls.Add(Pb)
        End Sub
    
        Public Sub Box_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            'Click Events
            If Turn = 0 Then
                B(Boxes).click = Boxes
                Pb.BackgroundImage = My.Resources.X
                Value = 0
                Turn += 1
                PlayerWinCheck()
                If Win = True Then
                    MsgBox("X Wins")
                Else
                    MsgBox("O Turn")
                End If
            ElseIf Turn = 1 Then
                Pb.BackgroundImage = My.Resources.O
                Value = 1
                Turn -= 1
                PlayerWinCheck()
                If Win = True Then
                    MsgBox("O Wins")
                Else
                    MsgBox("X Turn")
                End If
            End If
        End Sub
    
        Public Sub PlayerWinCheck()
            'Value X = 0 
            'Value O = 1
    
        End Sub
    End Class
    

     

    Saturday, January 28, 2012 5:06 AM
  • You have made some additional changes which are incorrect. 

    When you create the pictureboxes, you must ensure that each one is created as a different reference.  To do this, the variable you use for the reference (PB) must be declared within the loop.  To see what's happening, set the border style of each picture box (eg, to FixedSingle).  You will see that there is only one being created. 

    You probably made that change because of an error you got when you moved the keystroke code into its own routine - that was the wrong fix.  The correct fix is as I mentioned above.  You have to be sure that the picturebox image is updated using the reference to the picturebox that you calculate at the start of the click handler. 

    Also, each picture box must be connected to the handler, and each one must be added to the controls collection.  Your original code was correct except that the code from If to End If had to be moved out.

    With those fixes, the code works.

    Testing for a win is tricky because you are not keeping track of the moves. One solution is:
    - Create an array of integer to match the pictureboxes
    - Put a reference number (0 to 8) in the tag property of each picturebox
    - When the image is written, update the array with a value to indicate X or O, using the tag value to give you the array index
    - To test for a win
      - Create patterns that identify a win, eg 1,1,1,0,0,0,0,0,0   0,0,0,1,1,1,0,0,0  etc where 0 means 'don't care'
      - Compare the array to each pattern

    For instance:
    comparing  X,O,X,X,O,O,X,-,-  with the pattern 1,0,0,1,0,0,1,0,0 indicates a win for X.

    • Marked as answer by Mike Feng Monday, January 30, 2012 9:44 AM
    Saturday, January 28, 2012 7:00 AM
  • Frank,

    It is nice!!!

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
    Saturday, January 28, 2012 8:03 AM