|
-
May 26th, 2004, 03:15 PM
#1
Thread Starter
Addicted Member
Tic-Tac-Toe (lol)
For school, I have to create a tic tac toe program but I have found checking for a win seems to be longer than is probably needed. I used the tag property to keep track of who clicked on the square and to make sure that u can't click on one that has already been clicked.
VB Code:
If Index = 0 Then
If lblBox(0).Tag = "1" Then
If lblBox(3).Tag = "1" And lblBox(6).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
ElseIf lblBox(0).Tag = "2" Then
If lblBox(3).Tag = "2" And lblBox(6).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
ElseIf lblBox(0).Tag = "1" Then
If lblBox(1).Tag = "1" And lblBox(2).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
ElseIf lblBox(0).Tag = "2" Then
If lblBox(1).Tag = "2" And lblBox(2).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
ElseIf lblBox(0).Tag = "1" Then
If lblBox(4).Tag = "1" And lblBox(8).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
ElseIf lblBox(0).Tag = "2" Then
If lblBox(4).Tag = "2" And lblBox(8).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
End If
End If
This is the code I am using now and all that only checks for across the top, down the left, and diangonal left top to right bottom. If theres an easier way to do this could you please help. Thanks.
-
May 26th, 2004, 03:52 PM
#2
Hyperactive Member
you could develop an algorithm that for each space:
1 checks if the space has a X or O
2 checks spaces around that space to see if any of them have the same value as the space just checked (X or O)
3 if they do check the next space in that direction to see if it also has the same Value
4 Reapeat 2 for all spaces around space 1
5 repreat 1 for all spaces
tell me if you need me to better explain this or give you an example
-
May 26th, 2004, 04:28 PM
#3
Thread Starter
Addicted Member
How would one go about checking the spaces around the space clicked? The index numbers start at 0 top left and end at 8 bottom right.
0 1 2
3 4 5
6 7 8
If that matters. Would I just kinda check the index number and then work it out from there or would there be an easier way to do it so that it could just be a general checking procedure that they could all use? Because it would probably be easier to check both of the spaces instead of just one at a time. Thanks for the help.
-
May 26th, 2004, 04:32 PM
#4
Ex-Super Mod'rater
You could make a function that will check all the surrounding tiles but seen as though the grid is only 3x3 its not really worth it, the code that ensures you don't check a position thats off the grid would actually be longer than just using what you have. Suppose you could tidy the If statements up a little but simply the same thing .
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
May 26th, 2004, 05:36 PM
#5
Thread Starter
Addicted Member
My teacher tryed to do somthing like...
VB Code:
for x = 0 to 8
if val(lblbox(x).tag) + val(lblbox(x+3) + val(lblbox(x+6) = 3 then msgbox "Player 1 Wins", vbokonly, "Winner"
next x
The tag being 1 if the box contains an X and 10 if it contains an O. Would that work? it would have to be changed alittle so that there are different numbers for each row....
VB Code:
if x <=2 then
'Code
if x >=3 and x <=5 then
'Code
.
.
.
Would this way work just so that I don't have to write out the first post's vb code 8 times? Thanks.
-
May 26th, 2004, 06:27 PM
#6
Ex-Super Mod'rater
VB Code:
'Checks verticle wins...
For x = 0 To 2
If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 3 Then MsgBox "Player 1 Wins", vbOKOnly, "Winner"
If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 30 Then MsgBox "Player 2 Wins", vbOKOnly, "Winner"
Next
'Check for horizontal win...
For x = 0 To 6 Step 3
If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 3 Then MsgBox "Player 1 Wins", vbOKOnly, "Winner"
If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 30 Then MsgBox "Player 2 Wins", vbOKOnly, "Winner"
Next
'Check Diagonal...
Do you want me to do the diagonal too? I guess it might be better to do the diagonals as you shoed in your first post cos there is only two possible ways .
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|