Need help with logic for Tictactoe game!

gmc8757

Member
Joined
Dec 17, 2005
Messages
15
Programming Experience
Beginner
I appologize if this is not the right spot for this thread, but i think it is. I'm new to vb.net. And I'm using 2003.

Ok, i have a vb.net homework to make a tictactoe game. There's two parts, the first is to make a normal 3x3 game. The second is to make it so the user can select form 3x3 to 10x10 grid. I made the 3x3 one, took a while but did it. I'm having a problem checking if there is a winner in part 2 because the amount of blocks is dynamic. For the first part i just hardcoded the boxes in and if there was an X or an O in a line, there was a winner. For part 2, I can't hardcode obviously (I know it's not good to hardcode but i didn't know what else to do)

So if someone would be kind enough to look at how i checked for a winner in my 3x3 and help me put logic in it so nothings hardcoded, that'd be great.

I also had to change the colors of the winning blocks, i hardcoded that too. I only left the color change in the first if statement just so it didn't make the code posted here longer then it is already. Just a little background, myblocks(0,0) is an array of blocks that i dynamically display to screen depending on what size grid the user wants. SO if you had a 3x3 grid, going from left to right top to bottom, it's like this:
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2

I tried to use what i konw about for loops/if statements and so on, but had no luck. Also the variable gameWidth is the number of blocks across or down. for the 3x3 grid, gameWidth is set to 2.(starting at zero to two, leaving 3) if the user selects a 5x5 grid, gameWidth gets set to 4. At the end of all this, there is an IF statement which checks the variable numBoxes against numBoxClicked, if they are equal, every box was clicked and there was no winner. I call this sub every time a box is selected. I also disable the box once it's clicked so it can not be clicked again. If you would like to see the rest of the code, please let me know.

PrivateSub testForWin()
Dim ourWinner AsString

If myblocks(0, 0).Text = "X" And myblocks(0, 1).Text = "X" And myblocks(0, 2).Text = "X" Then
ourWinner = "X"
myblocks(0, 0).BackColor = Color.FromName("yellow")
myblocks(0, 1).BackColor = Color.FromName("yellow")
myblocks(0, 2).BackColor = Color.FromName("yellow")
weHaveWinner(ourWinner)
ElseIf myblocks(1, 0).Text = "X" And myblocks(1, 1).Text = "X" And myblocks(1, 2).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(2, 0).Text = "X" And myblocks(2, 1).Text = "X" And myblocks(2, 2).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 0).Text = "X" And myblocks(1, 1).Text = "X" And myblocks(2, 2).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 0).Text = "X" And myblocks(1, 0).Text = "X" And myblocks(2, 0).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 1).Text = "X" And myblocks(1, 1).Text = "X" And myblocks(2, 1).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 2).Text = "X" And myblocks(1, 2).Text = "X" And myblocks(2, 2).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 2).Text = "X" And myblocks(1, 1).Text = "X" And myblocks(2, 0).Text = "X" Then
ourWinner = "X"
weHaveWinner(ourWinner)


ElseIf myblocks(0, 0).Text = "O" And myblocks(0, 1).Text = "O" And myblocks(0, 2).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(1, 0).Text = "O" And myblocks(1, 1).Text = "O" And myblocks(1, 2).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(2, 0).Text = "O" And myblocks(2, 1).Text = "O" And myblocks(2, 2).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 0).Text = "O" And myblocks(1, 1).Text = "O" And myblocks(2, 2).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 0).Text = "O" And myblocks(1, 0).Text = "O" And myblocks(2, 0).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 1).Text = "O" And myblocks(1, 1).Text = "O" And myblocks(2, 1).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 2).Text = "O" And myblocks(1, 2).Text = "O" And myblocks(2, 2).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf myblocks(0, 2).Text = "O" And myblocks(1, 1).Text = "O" And myblocks(2, 0).Text = "O" Then
ourWinner = "O"
weHaveWinner(ourWinner)
ElseIf numBoxes = numboxclicked Then
If MsgBox("There was no winner. Do you want to play again?", MsgBoxStyle.YesNo, "Do you want to play again??") = MsgBoxResult.Yes Then
initializeGame()
Else
End
EndIf
EndIf
EndSub
 
I'm sorry to bring this back up, but i really have no clue how to test for a winner when the user picks the amount of squares. I have no problem testing for a regular 3 by 3 game. But part 2 of the project is to be able to have the player chose how many squares between 3 and 10. Please let me know if anyone is willing to give me a hand.
Thanks,
Joe
 
They were useful, but all they did was hardcode the possible winning solutions. I have to put the check in a for loop or something because i never know what the user will pick as for the game size. Coule be 3x3, 4x4, 5x5, 6x6, 7x7, 8x8, 9x9, or 10x10. So i can't hardcode all the solutions in. Those examples were all 3x3, so there aren't many winning combiniations so you can get away with hardcoding. I'm sure there's a way, i'm just not a programmer, although i wish i was. Getting there i guess.
Thanks
 
The code you write is just a representation of what you physically want to achieve. Think about what you would physically do to check for a winner. You would look at the first row to see if all the squares were the same, then the second row and so on. Then you would do the same for the columns, then the diagonals. Assuming that you are using a 2D array to store the squares, all you have to do is use a For loop to test each row and column in turn. The actual number of rows and columns is immaterial because you can code a loop to just go from the start of an array to the end.

You are making the classic mistake of taking a general impression of what you want to achieve and trying to code it. Work logically and systematically. Start by writing down what you want to achieve. Then you can write out how you would do it physically. Then you can turn that into psuedocode. Then finally you can write the actual code. This is what's known as software development. You don't just think "I want a to write a program that does X" and just jump into writing code. We all do it to an extent, but when you aren't sure how to proceed you need to take a step back and do things properly.
 
I appreciate your help here. I did try to think it through before coding, i'm just real inexperienced with this.
This is what i was working on, right now it's only testing for the first line. It picks out a winner if the whole line is marked with the same letter. Now i just need to figure out how to do it for the rest of the lines. I'm just not sure how to make it totally dynamic with no hardcoding at all. I'm working on it right now tho.
VB.NET:
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] y = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] gameWidth
    [/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] gameWidth
        mark = myblocks(0, 0).Text

        [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] myblocks(0, x).Text = mark [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] myblocks(0, 0).Text <> "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
            [/COLOR][/SIZE][SIZE=2]winner = [/SIZE][SIZE=2][COLOR=#0000ff]True
            [/COLOR][/SIZE][SIZE=2]theWinner = myblocks(0, x).Text
        [/SIZE][SIZE=2][COLOR=#0000ff]Else
            [/COLOR][/SIZE][SIZE=2]winner = [/SIZE][SIZE=2][COLOR=#0000ff]False
            [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GoTo[/COLOR][/SIZE][SIZE=2] myline
        [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
   [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
Next[/COLOR][/SIZE][/COLOR][/SIZE]

 
Last edited by a moderator:
Yick! Get rid of that GoTo for a start. Here's how I would do it fro the rows and the easy diagonal:
VB.NET:
    Private Sub TestForWinner()
        Dim winner As String

        'Check each row.
        For row As Integer = 0 To boardWidth - 1
            For column As Integer = 1 To boardWidth - 1
                'Compare each square to the one before it.
                If gameBoard(row, column).Text = gameBoard(row, column - 1).Text Then
                    If column = boardWidth - 1 Then
                        'This row is a winner.
                        winner = gameBoard(row, column).Text
                    End If
                Else
                    'This row is not a winner.
                    Exit For
                End If
            Next column

            If Not winner Is Nothing Then
                'A winner has already been found.
                Exit For
            End If
        Next row

        If winner Is Nothing Then
            'Check the first diagonal.
            For i As Integer = 1 To boardWidth - 1
                If gameBoard(i, i).Text = gameBoard(i - 1, i - 1).Text Then
                    If i = boardWidth - 1 Then
                        'This diagonal is a winner.
                        winner = gameBoard(i, i).Text
                    End If
                Else
                    'This diagonal is not a winner.
                    Exit For
                End If
            Next i
        End If

        If winner Is Nothing Then
            MessageBox.Show("No winner.")
        Else
            MessageBox.Show("The winner is " & winner)
        End If
    End Sub
If you want to use this I'll leave it to you to adapt the method for the columns and the other diagonal.
 
You don't know how much i appreciate this. I just need a jump start on this...i'm going to work on it and let you know how it goes.
Thanks,
Joe
 
Back
Top