Results 1 to 6 of 6

Thread: Tic-Tac-Toe (lol)

  1. #1

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164

    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:
    1. If Index = 0 Then
    2.     If lblBox(0).Tag = "1" Then
    3.         If lblBox(3).Tag = "1" And lblBox(6).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
    4.     ElseIf lblBox(0).Tag = "2" Then
    5.         If lblBox(3).Tag = "2" And lblBox(6).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
    6.     ElseIf lblBox(0).Tag = "1" Then
    7.         If lblBox(1).Tag = "1" And lblBox(2).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
    8.     ElseIf lblBox(0).Tag = "2" Then
    9.         If lblBox(1).Tag = "2" And lblBox(2).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
    10.     ElseIf lblBox(0).Tag = "1" Then
    11.         If lblBox(4).Tag = "1" And lblBox(8).Tag = "1" Then MsgBox "Player 1 wins", vbOKOnly, "Winner!"
    12.     ElseIf lblBox(0).Tag = "2" Then
    13.         If lblBox(4).Tag = "2" And lblBox(8).Tag = "2" Then MsgBox "Player 2 Wins", vbOKOnly, "Winner!"
    14.     End If
    15. 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.
    -Psychotic-

  2. #2
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    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

  3. #3

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164
    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.
    -Psychotic-

  4. #4
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    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.

  5. #5

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164
    My teacher tryed to do somthing like...

    VB Code:
    1. for x = 0 to 8
    2.     if val(lblbox(x).tag) + val(lblbox(x+3) + val(lblbox(x+6) = 3 then msgbox "Player 1 Wins", vbokonly, "Winner"
    3. 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:
    1. if x <=2 then
    2.   'Code
    3. if x >=3 and x <=5 then
    4.   'Code
    5. .
    6. .
    7. .

    Would this way work just so that I don't have to write out the first post's vb code 8 times? Thanks.
    -Psychotic-

  6. #6
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    VB Code:
    1. 'Checks verticle wins...
    2. For x = 0 To 2
    3.     If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 3 Then MsgBox "Player 1 Wins", vbOKOnly, "Winner"
    4.     If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 30 Then MsgBox "Player 2 Wins", vbOKOnly, "Winner"
    5. Next
    6.  
    7. 'Check for horizontal win...
    8. For x = 0 To 6 Step 3
    9.     If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 3 Then MsgBox "Player 1 Wins", vbOKOnly, "Winner"
    10.     If Val(lblbox(x).Tag) + Val(lblbox(x + 3)) + Val(lblbox(x + 6)) = 30 Then MsgBox "Player 2 Wins", vbOKOnly, "Winner"
    11. Next
    12.  
    13. '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
  •  



Click Here to Expand Forum to Full Width