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
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