Results 1 to 2 of 2

Thread: [Resolved] Check for win in Naughts and Crosses

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    [Resolved] Check for win in Naughts and Crosses

    Hi,
    I'm writing a Naughts and Crosses game (kind of like Tic-Tac-Toe, but you have an unlimited playing field and your goal is to get five in a row).
    I've written a function to check if there are five in a row or not. First it check vertical, then horizontal, then diagonaly - this is what is causing me troubles... I can check one of the diagonals, the one leaning to the left at the top (kind of like this: \ ) But the one leaning to the right at the top (like this: / ) is harder.
    The way my function works is that it takes the cell the user placed the last piece on and then checks four cells out from that cell in all directions. This is the piece of code that doesn't work (it doesn't detect a win). Please have a look
    VB Code:
    1. 'check (/) ----------------------------------------
    2.     intForRow = intRow + 5 'intRow is the row the last piece was put on
    3.     For intFor = (intCol - 4) To (intCol + 4) 'intCol is the col the last piece was put on
    4.         intForRow = intForRow - 1
    5.         If intFor >= 0 And intFor <= mfg.Cols - 1 And intForRow >= 0 And intForRow <= mfg.Rows - 1 Then
    6.             mfg.Col = intFor
    7.             mfg.Row = intForRow
    8.             If mfg.Text = strPlayer Then 'strPlayer is either X or O
    9.                 intCheck = intCheck + 1
    10.             Else
    11.                 If intCheck >= 5 Then
    12.                     check = True 'check is the name of this function
    13.                     Exit Function
    14.                 End If
    15.                 intCheck = 0
    16.             End If
    17.         End If
    18.     Next
    (I'm playing this on a MSFlexGrid)
    Last edited by McCain; Oct 10th, 2003 at 07:12 AM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Solved it... I just had to move
    VB Code:
    1. If intCheck >= 5 Then
    2.     check = True 'check is the name of this function
    3.     Exit Function
    4. End If
    out of the Else statement it was in and put it right after it instead... Like this:
    VB Code:
    1. intForRow = intRow + 5 'intRow is the row the last piece was put on
    2. For intFor = (intCol - 4) To (intCol + 4) 'intCol is the col the last piece was put on
    3.     intForRow = intForRow - 1
    4.     If intFor >= 0 And intFor <= mfg.Cols - 1 And intForRow >= 0 And intForRow <= mfg.Rows - 1 Then
    5.         mfg.Col = intFor
    6.         mfg.Row = intForRow
    7.         If mfg.Text = strPlayer Then
    8.             intCheck = intCheck + 1
    9.         Else
    10.             intCheck = 0
    11.         End If
    12.         If intCheck >= 5 Then
    13.             check = True 'check is the name of this function
    14.             Exit Function
    15.         End If
    16.     End If
    17. Next
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

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