Hi.. i have created a tic tac toe game in vb net 2010..
im almost done with my project but there are still some errors.
Can somebody pls help me with my code? A big thanks to whoever will help me..

my errors are:
buttons remaining are still clickable when there is already a winner declared.
and when all buttons are filled with x's and o's, and neither of the two got tic tac toe, it says the winner and after that it also says draw..

sorry for my bad english grammar.
i hope u still understand..

anyways, here is my code..

http://pastebin.com/qDubEzw2

Pls somebody help me.. Thank youuu!

Recommended Answers

All 5 Replies

Is this for a school assignment? If so I would give you abour 2 out of 10 if your code worked. I would be happy to offer suggestions depending on the circumstances and the time you have available. I coded up my own version of the game for fun after someone else posted a similar question but Ii will not post this version. My version including comments was 152 lines.

this is actually a school project, and im just a newbie in using vb2010. We are instructed to do a tic tac toe game with the use of if else statements and buttons. Fortunately, my codes are working.. its just that i am having a problem on this parts: (1) buttons remaining are still clickable when there is already a winner declared. (2) when all buttons are filled with x's and o's, and neither of the two got tic tac toe, it says the winner and after that it also says draw..

Would you like me to show the tic tac toe game I've done so far?
And I have time till tomorrow night to work on with this proj. I would be grateful if you could help me sir! :) Suggestions are very much appreciated :) Thank you!

My version uses the same handler for all nine buttons and starts by copying references to the buttons into a 3x3 array. This way the logic to check for a win is

Private Function CheckWin() As Boolean

    'Return True if three X or O in a row in any direction

    Dim p As String

    'check for a win in any row

    For r As Integer = 0 To 2
        p = ""
        For c As Integer = 0 To 2
            p &= grid(r, c).Text
        Next
        If p = "XXX" Or p = "OOO" Then Return True
    Next

    'check for a win in any column

    For c As Integer = 0 To 2
        p = ""
        For r As Integer = 0 To 2
            p &= grid(r, c).Text
        Next
        If p = "XXX" Or p = "OOO" Then Return True
    Next

    'check for a win in any diagonal

    p = ""
    For i As Integer = 0 To 2
        p &= grid(i, i).Text
    Next
    If p = "XXX" Or p = "OOO" Then Return True

    p = ""
    For i As Integer = 0 To 2
        p &= grid(i, 2 - i).Text
    Next
    If p = "XXX" Or p = "OOO" Then Return True

    Return False

End Function

Because you have a separate handler for each button you could add something like

If winner Then Exit Sub

Again, because you have so much unnecessary duplicate code (because of the separate handlers) you have to modify nine handlers to change the behaviour.

Another thing is that you have code like

If turn.Text = "X" Then
    Button2.Text = "X"
    turn.Text = "O"
Else
    Button2.Text = "O"
    turn.Text = "X"
End If

If you had a global variable named Player and set it to X or O then you could do

Button2.Text = Player
Player = IIF(Player = "x", "y", "X")

But because of the overwhelming amount of unnecessary duplicate code I would have to give you very low marks. At the very least you should move the "check for win or draw" code into Subs.

wow.. This is really a big help for me to fix my codes.. Thank you so much sir! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.