locked
Tic-Tac-Toe Code Help! RRS feed

  • Question

  • Afternoon,
    I'm new to the whole programing world, (only started a couple months ago) and I need help with a tic-tac-toe game I've been trying to make. I keep getting a "System.NullReferenceException" if that means anything to anyone, on the lines that say "Dim x As Integer = Val(index.Chars(0))" Either way, as I said I'm new, and the code is right out of the text book and it's getting that error and won't run.... help me out if you can... here's the code. Thanks!

    P.S. if this is in the wrong section of the form, just tell me and I'll be happy to move it.

        Private Sub btnMoveMade_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles btn00.Click, _
        btn01.Click, btn02.Click, btn10.Click, btn11.Click, _
        btn12.Click, btn20.Click, btn21.Click, btn22.Click
    
            Dim btnSquareClicked As Button = sender
            Static chrTTT(2, 2) As Char      'store players moves
            Static player As Char = "X"     'x goes first
    
            'check for existing x or o
            If btnSquareClicked.Text <> Nothing Then
                MessageBox.Show("Invalid Move")
            Else
    
                'show move
                btnSquareClicked.Text = player
    
    
                'store moves in chrTTT
                Dim index As String
                index = btnSquareClicked.Tag
                Dim x As Integer = Val(index.Chars(0))
                Dim y As Integer = Val(index.Chars(2))
                Call StoreMove(x, y, player, chrTTT)
    
    
                'check winner 
                If IsWinner(chrTTT) Then
                    MessageBox.Show("Game over!")
                Else
                    If player = "X" Then
                        player = "O"
                    Else
                        player = "X"
                    End If
                End If
            End If
        End Sub
    
        Sub StoreMove(ByVal x As Integer, ByVal y As Integer, _
        ByVal player As Char, ByRef TTT(,) As Char)
            TTT(x, y) = player
        End Sub
    
        Function IsWinner(ByRef TTT(,) As Char) As Boolean
            'check all rows
            For row As Integer = 0 To 2
                If TTT(row, 0) = TTT(row, 1) And _
                TTT(row, 1) = TTT(row, 2) And _
                (TTT(row, 0) = "X" Or TTT(row, 0) = "O") Then
                    Return True     'winner
                End If
            Next row
    
            'check all collums
            For col As Integer = 0 To 2
                If TTT(0, col) = TTT(1, col) And _
                TTT(1, col) = TTT(2, col) And _
                (TTT(0, col) = "X" Or TTT(0, col) = "O") Then
                    Return True
                End If
            Next col
    
            'check one diagonal 
            If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) _
            And (TTT(0, 2) = "X" Or TTT(0, 2) = "O") Then
                Return True
            End If
    
            'chech for empty squarres
            Dim movesLeft As Boolean = False
            For row As Integer = 0 To 2
                For col As Integer = 0 To 2
                    If TTT(row, col) = Nothing Then
                        movesLeft = True
                    End If
                Next col
            Next row
            If Not movesLeft Then
                Return True     'all fields filled
            End If
    
            Return False        'no winner found
        End Function

    Wednesday, May 27, 2009 6:53 PM

Answers

  • Are you putting something into the Tag property of the buttons somewhere?

    This line of code is pulling the value of the Tag property of the button and using it:

    index = btnSquareClicked.Tag

    If there is nothing in the tag, you will get a null reference exception on thi sline

    Dim x As Integer = Val(index.Chars(0))

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, May 27, 2009 7:05 PM

All replies

  • Are you putting something into the Tag property of the buttons somewhere?

    This line of code is pulling the value of the Tag property of the button and using it:

    index = btnSquareClicked.Tag

    If there is nothing in the tag, you will get a null reference exception on thi sline

    Dim x As Integer = Val(index.Chars(0))

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, May 27, 2009 7:05 PM
  • Oh, wow I never knew anything like that had to be done.
    Don't I feel silly.
    So what should I put in the Tag property.
    What the buttons are labeled, like....

    0,0  0,1  0,2
    1,0  1,1  1,2
    2,0  2,1  2,2

    As I said I'm new at this, and this code was in a text book and I had never used the .Tag feature before.
    Thanks for the help though! I might still be able to work with that

    EDIT: Yeah, that was it, I tagged them as I said, and now I see X's and O's Thanks!
    Thats one problem solved!
    Wednesday, May 27, 2009 7:15 PM
  • If you want to look at my Multi Dimensional Tic Tac Toe project you can download it and have a look it may give you some ideas . MD_Tic_Tac_Toe.zip


    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Wednesday, May 27, 2009 10:12 PM
  • Cool. Glad you were able to solve it.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, May 27, 2009 10:43 PM
  • i am making the same TTTBaord, using the same code from the book. but i am also having troubles with the NullReferenceException. How dod you fix this? and how did you get the X's and O's to show up on the buttons?
    thanks
    Wednesday, June 3, 2009 9:55 PM
  • i am making the same TTTBaord, using the same code from the book. but i am also having troubles with the NullReferenceException. How dod you fix this? and how did you get the X's and O's to show up on the buttons?
    thanks

    I just made sure that each buttons are tagged,

    0,0  0,1  0,2
    1,0  1,1  1,2
    2,0  2,1  2,2

    That was the order you tag each button as. That solved my problem.
    Muskoka? As in Gravenhurst Ontario area?
    Thursday, June 4, 2009 10:34 PM