Results 1 to 29 of 29

Thread: Check for win?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Check for win?

    As i posted last night, i was making a game of tic-tac-toe.
    Im pretty much done, i just dont know how to add a win check. Once i get that ill make it winsock for online fun ; )..Heres the code:

    VB Code:
    1. Option Explicit
    2. Dim x, y As Integer
    3. Dim teamname, compname As String
    4.  
    5. Private Sub Form_Load()
    6.     Call Restart1
    7. End Sub
    8.  
    9. Private Sub Restart_Click()
    10.     Call Restart1
    11. End Sub
    12.  
    13. Private Sub xolbl_Click(Index As Integer)
    14. If Len(xolbl(Index).Caption) > 0 Then Exit Sub
    15.   Select Case teamname
    16.         Case "x"
    17.              xolbl(Index).Caption = "x"
    18.         Case "o"
    19.              xolbl(Index).Caption = "o"
    20.     End Select
    21.     Call rndfunc
    22.     Call addblock
    23. End Sub
    24.  
    25. Private Sub rndfunc()
    26.         y = Int((8 * Rnd) + 1)
    27.       '  MsgBox y
    28. End Sub
    29.  
    30. Private Sub changeteams()
    31.     Select Case teamname
    32.         Case "x"
    33.             compname = "o"
    34.         Case "o"
    35.             compname = "x"
    36.     End Select
    37.     If Len(xolbl(y)) > 0 Then
    38.             Call rndfunc
    39.             Call addblock
    40.             End If
    41. End Sub
    42.  
    43. Private Sub addblock()
    44. On Error GoTo errhndlr
    45.         If Len(xolbl(y)) > 0 Then
    46.             Call rndfunc
    47.             Call changeteams
    48.         Else
    49.             Call changeteams
    50.             xolbl(y) = compname
    51.         End If
    52.         xolbl(y) = compname
    53.         Call Checkforwin
    54. errhndlr:
    55.     If Len(Err.Description) > 0 Then
    56.         MsgBox Err.Number & Err.Description & " Source " & Err.Source
    57.     End If
    58. End Sub
    59.  
    60. Private Sub Checkforwin()
    61.    
    62. End Sub
    63.  
    64. Private Sub Restart1()
    65. Dim i As Integer
    66. Randomize
    67.     x = Int((2 * Rnd) + 1)
    68.     If x = 2 Then
    69.         teamname = "x"
    70.     Else
    71.         teamname = "o"
    72.     End If
    73.     For i = 0 To xolbl.UBound
    74.     xolbl(i).Caption = vbNullString
    75.  Next i
    76.  You.Caption = "You = " & teamname
    77. End Sub

    How do i check for three in a row?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Check for win?

    I'm guessing that your label array is arranged like this:
    012
    345
    678

    There is only 5 ways someone can win. If the labels 0, 1, and 2 have the same caption. If label 3, 4, and 5 have the same caption. If label 6, 7, and 8 have the same caption. If labels 0, 4, and 8 have the same caption. If labels 2, 4, and 6 have the same caption. I guess you just have to check that. Of course the same caption doesn't mean an empty string in all positions

  3. #3
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Check for win?

    There are actually eight

    012
    345
    678
    036
    147
    258
    048
    246

  4. #4
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Check for win?

    have a little code, it's untested...
    Private Function checkforwin() As Boolean
    If xolbl(0).Caption = xolbl(1).Caption And xolbl(1).Caption = xolbl(2).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(3).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(5).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(6).Caption = xolbl(7).Caption And xolbl(7).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(0).Caption = xolbl(3).Caption And xolbl(3).Caption = xolbl(6).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(1).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(7).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(2).Caption = xolbl(5).Caption And xolbl(5).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(2).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(6).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    If xolbl(0).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
    End Function

    Called like so (pseudocode)
    Xplays
    if Checkforwin then
    msgbox "X wins!"
    restartgame
    end if
    Oplays
    if Checkforwin then
    msgbox "O wins!"
    restartgame
    end if
    Last edited by Turiya; Mar 25th, 2005 at 07:40 PM. Reason: ...on And Len(xolbl(1).Caption) <> 0 Th...checks for 0 length strings and fixes the problem

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    This is how I'd do it
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    Turiya, i like your code. ill try it ; )

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Turiya's code is not checking for empty values, either (Mine was not checking it because it was not checking play-by-play but at the end). After the first play, that code would find one line or column (and possibly a diagonal too) which has all the position equalls to "". For example, I play my "X" in the center box... the line If xolbl(0).Caption = xolbl(1).Caption And xolbl(1).Caption = xolbl(2).Caption Then checkforwin = True will fit the condition and your code would yield I won. You should bear that in mind.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    I got it to work, is there a neater way of doing it though?

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Have a look at this full-working code. Now you can play.
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    Yes it works, but it seems you over complicated the game a little too much...

  11. #11
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Quote Originally Posted by |2eM!x
    Yes it works, but it seems you over complicated the game a little too much...
    You asked for a neater way. I think that's as neat as it could be done. I could add some comments lines... but you don't seem willing to learn from it, so it would be a waste of time.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    In other words... a neater code would be a code that would let you (in the future) upgrade it so that you can let the user decide how many rows and columns he wants without modifying the whole code. My code would need to use a variable instead of all of those "2" (or "3"), and a little modification on the diagonal checking (because there would be more than 2 diagonals if there are more than 9 squares). That's neat!!

    The other way (proposed coding) you should limit the number of lines and/or columns the user wants and write a load of IF lines as above according to the number of lines and/or columns the user wanted.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    im sorry dude, but your code is either to advanced, or not exactly what im looking for. I in no way meant to offend you as it seems i did

  14. #14
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Don't worry. You did not offend me. I was just trying to make my point. If you want a neater way... you should build up your knowledge to understand and code harder things. For example, here's the first hint:

    VB Code:
    1. Select Case teamname
    2.         Case "x"
    3.              xolbl(Index).Caption = "x"
    4.         Case "o"
    5.              xolbl(Index).Caption = "o"
    6.     End Select
    This code makes no sense at all. A neater way would be
    VB Code:
    1. xolbl(Index).Caption = teamname
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  15. #15

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    jeez, never even thought of that

  16. #16
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Check for win?

    If the guy doesn't want to change his code like that, he doesn't have to. I used to have messy code back in grade 7 and 8, I have neater code now, but I think that if the code works, you have a good program, if you have code that does what you want, you're amazing.

  17. #17

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    yup it works good, except i hate all the if statements

  18. #18
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Hey! I'm not forcing anyone here! I don't want him to change all his program. He asked for a neater way and I posted an example. If he does not want to implement something like that... he won't do it.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  19. #19
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Check for win?

    Sorry, I meant no offense

  20. #20
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Quote Originally Posted by |2eM!x
    yup it works good, except i hate all the if statements
    No Ifs, but Fors... and a much simpler way.
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Quote Originally Posted by Turiya
    Sorry, I meant no offense
    Not taken.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  22. #22

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    Yes, but i dont understand it

  23. #23
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Quote Originally Posted by |2eM!x
    Yes, but i dont understand it
    The CheckForWinners calls the functions CheckForLines, CheckForColumns and CheckForDiagonals.

    VB Code:
    1. Private Function CheckForLines() As Boolean
    2.     Dim sValue As String
    3.    
    4.     For iLine = 0 To 2
    5.         'We'll start with the first line... that's Line=0, then Line=1 and finally Line=2
    6.         iColumn = 0  'Set the column variable to the first one.
    7.         sValue = arr(Index)  'Load the variable sValue with the content of the
    8.                                     'first element on this line. This is the one that's
    9.                                     'on Column=0
    10.        
    11.         For iColumn = 1 To 2
    12.             'We'll check the other two values (Column=1 and Column=2)                
    13.             If sValue <> arr(Index) Or sValue = "" Then
    14.                 'The value in this square does not match with the value in
    15.                 'the first column (sValue)...
    16.                 'or the value in the first square was empty.
    17.                 Exit For
    18.             End If
    19.         Next iColumn
    20.        
    21.         If iColumn > 2 Then
    22.             'If we get here... the three columns have the same value and did not
    23.             'did not "EXIT FOR".    
    24.             CheckForLines = True
    25.         End If
    26.     Next iLine
    27. End Function

    The other functions (CheckForColumns and CheckForDiagonals) are very similar.

    BTW, it seems you did not download the latest code (Tic-Tac-Toe.zip (5.1 KB, 0 views)). It's much simpler than the one I posted first. As you can see in this post, the functions returns a boolean instead of a collection.
    Last edited by Mc Brain; Mar 25th, 2005 at 08:08 PM.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  24. #24

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    cool, can you tell me why sometimes, this doesnt clear one label?
    VB Code:
    1. Private Sub Restart1()
    2. Dim i As Integer
    3. Randomize
    4.     x = Int((2 * Rnd) + 1)
    5.     If x = 2 Then
    6.         teamname = "x"
    7.     Else
    8.         teamname = "o"
    9.     End If
    10.     For i = 0 To xolbl.UBound
    11.     xolbl(i).Caption = vbNullString
    12.  End If
    13.  Next i
    14. End Sub

    xolbl(i).Caption = vbNullString, that should clear alll in the array, but it leaves the last selected box sometimes..do you know why?

  25. #25
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Quote Originally Posted by |2eM!x
    cool, can you tell me why sometimes, this doesnt clear one label?
    VB Code:
    1. Private Sub Restart1()
    2. Dim i As Integer
    3. Randomize
    4.     x = Int((2 * Rnd) + 1)
    5.     If x = 2 Then
    6.         teamname = "x"
    7.     Else
    8.         teamname = "o"
    9.     End If
    10.     For i = 0 To xolbl.UBound
    11.     xolbl(i).Caption = vbNullString
    12.  End If
    13.  Next i
    14. End Sub

    xolbl(i).Caption = vbNullString, that should clear alll in the array, but it leaves the last selected box sometimes..do you know why?
    First tell me what's this END IF is for....

    VB Code:
    1. Private Sub Restart1()
    2. Dim i As Integer
    3. Randomize
    4.     x = Int((2 * Rnd) + 1)
    5.     If x = 2 Then
    6.         teamname = "x"
    7.     Else
    8.         teamname = "o"
    9.     End If
    10.     For i = 0 To xolbl.UBound
    11.     xolbl(i).Caption = vbNullString
    12.  End If  '<---- What is this for??
    13.  Next i
    14. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  26. #26

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    its not there, i realized it was dumb code when i posted it, so i deleted it and forgot the end if, its not there in code

  27. #27
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    And which one is the one that's not being cleared?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  28. #28

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Check for win?

    Its different every time, its usually the last one clicked

  29. #29
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Check for win?

    Then... I'm guessing the problem is that it's caption is being re-written after you've deleted ots contents. Place a breakpoint at the end of the routine and check if it everything is Ok. If it does... you have the answer.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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