|
-
Oct 9th, 2003, 12:32 PM
#1
Thread Starter
Fanatic Member
[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:
'check (/) ----------------------------------------
intForRow = intRow + 5 'intRow is the row the last piece was put on
For intFor = (intCol - 4) To (intCol + 4) 'intCol is the col the last piece was put on
intForRow = intForRow - 1
If intFor >= 0 And intFor <= mfg.Cols - 1 And intForRow >= 0 And intForRow <= mfg.Rows - 1 Then
mfg.Col = intFor
mfg.Row = intForRow
If mfg.Text = strPlayer Then 'strPlayer is either X or O
intCheck = intCheck + 1
Else
If intCheck >= 5 Then
check = True 'check is the name of this function
Exit Function
End If
intCheck = 0
End If
End If
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
-
Oct 10th, 2003, 07:11 AM
#2
Thread Starter
Fanatic Member
Solved it... I just had to move
VB Code:
If intCheck >= 5 Then
check = True 'check is the name of this function
Exit Function
End If
out of the Else statement it was in and put it right after it instead... Like this:
VB Code:
intForRow = intRow + 5 'intRow is the row the last piece was put on
For intFor = (intCol - 4) To (intCol + 4) 'intCol is the col the last piece was put on
intForRow = intForRow - 1
If intFor >= 0 And intFor <= mfg.Cols - 1 And intForRow >= 0 And intForRow <= mfg.Rows - 1 Then
mfg.Col = intFor
mfg.Row = intForRow
If mfg.Text = strPlayer Then
intCheck = intCheck + 1
Else
intCheck = 0
End If
If intCheck >= 5 Then
check = True 'check is the name of this function
Exit Function
End If
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|