PDA

Click to See Complete Forum and Search --> : What is wrong with this code? (A bigass loop)


amesjustin
Mar 5th, 2001, 01:18 AM
hey!
this is the prototype of the WinCheck function of my TicTacToe game. The labels (0-8) are the 9 squares you play on.

Function WeHaveAWinner() As Boolean
WeHaveAWinner = False
Dim strWinningCombo(7) As String
Do
strWinningCombo(0) = lblMove(0).Caption And lblMove(1).Caption And lblMove(2).Caption
strWinningCombo(1) = lblMove(3).Caption And lblMove(4).Caption And lblMove(5).Caption
strWinningCombo(2) = lblMove(6).Caption And lblMove(7).Caption And lblMove(8).Caption
strWinningCombo(3) = lblMove(0).Caption And lblMove(3).Caption And lblMove(6).Caption
strWinningCombo(4) = lblMove(1).Caption And lblMove(4).Caption And lblMove(7).Caption
strWinningCombo(5) = lblMove(2).Caption And lblMove(5).Caption And lblMove(8).Caption
strWinningCombo(6) = lblMove(0).Caption And lblMove(4).Caption And lblMove(8).Caption
strWinningCombo(7) = lblMove(2).Caption And lblMove(4).Caption And lblMove(6).Caption
If strWinningCombo() Is "XXX" Then
WeHaveAWinner = True
If strComputer = "X" Then
Call ComputerWin
Exit Function
End If
If strHuman = "X" Then
Call HumanWin
Exit Function
End If
Else:
If strWinningCombo() = "OOO" Then
WeHaveAWinner = True
If strComputer = "O" Then
Call ComputerWin
Exit Function
End If
If strHuman = "O" Then
Call HumanWin
Exit Function
End If
End If
End If
Loop Until (WeHaveAWinner = True) Or (Boardfull = True)
End Function

When it gets to the [If strWinningCombo() Is "XXX" Then] line it gets a "Type Mismatch" error on the "XXX".
I have tried at least 809 different configurations of this loop and this is the one error I cannot seem to figure out.
please help.

kedaman
Mar 5th, 2001, 02:11 AM
I thought i answered this at least once.

And operator is a boolean logic operator, could also be used for bitwise comparation for numerical values, but never with strings. There's only one operator for strings and it's & used to conjunct them.

Is opertor is used to compare objects or comparations in select cases, not strings, use = instead. It's definitely not used to compare each default property of a control array with a string. You should have a loop here instead.

: after else is unnessesary, it acts as an extra line feed in code.

X=True is the same thing as X, remember this axiom.

The loop wont break until you have a winner, and that never happens since you exit the function in that case.

I recommend you declare the winner as boolean for human true and computer false:
winner=strHuman=strWinningCombo(i)
where i is the loop counter for the control array.
wehaveawinner=strwinningcombo(i)="XXX" or strwinningcombo(i)="000"

now i don't recommend strings for these jobs, but youre a newbe and it's forgivable. you should though get more used to the available datatypes though, arrays, loops and function calls should also be on your list.

HarryW
Mar 5th, 2001, 02:12 AM
Umm... I don't really see what you're trying to do with that captions stuff... and I don't see how strWinningCombo() could have a string value, when it's just an array name without an index. I think you want to loop through each winning combo to check, if the line is a winner or not. The loop also doesn't seem to end until the game ends, but I think the function is just meant to check if anyone has won yet.

Since you seem to be kind of lost in this, I'm going to suggest some architecture for this game, okay? First off, I'm going to repeat that I suggest you seperate the interface from the implementation - don't start checking values of labels against each other, just use them to show what's going on. You should keep track of what's actually going on in a data structure within your code. Make displaying the current status a seperate part of the game.

Store your tic-tac-toe grid in a 3x3 array, and create a seperate 3x8 array to be the lookup array.

Okay here's some pseudocode (more or less):


global stuff:
declare an array called Grid(1 To 3, 1 To 3) to hold one character in each element
declare a type called Coord2D:
Type Coord2D
Dim X As Integer
Dim Y As Integer
End Type

declare an array called Lookup(1 To 3, 1 To 8) to be of type Coord2D:
Dim Lookup(1 To 3, 1 To 8) As Coord2D


game initialisation:
set up each element in Lookup to hold the coordinates of a corresponding element in Grid. The coordinates should be like this:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3, 3)
(1, 1) (2, 1) (3, 1)
(1, 2) (2, 2) (3, 2)
(1, 3) (2, 3) (3, 3)
(1, 1) (2, 2) (3, 3)
(3, 1) (2, 2) (1, 3)

main game:
Do while the game is not over
DoEvents
if it's the player's turn then
nothing to do yet
else
'must be computer's turn
choose computer's move
update the display to represent the grid with latest move
if someone has won then game is over
end turn by giving player the next turn
end if
Loop


That's a pretty basic layout for your game. It doesn't include player's input because you can use the event-driven nature of VB to deal with that for you (that's why DoEvents is in there, to check for new events)

The standard layout of a game is initialisation, then a main loop, then shutdown. I didn't include any shutdown stuff because I don't know what you want to do when the game has been won or drawn.

For your win-checking code I would suggest something like this:

Dim squares(1 To 3) As String*1

For i = 1 To 8
For j = 1 To 3
squares(j) = Grid(Lookup(j, i).X, Lookup(j, i).Y)
Next j
If squares(1) = squares(2) And _
squares(2) = squares(3) And _
Trim(squares(1)) <> "" Then
'The game is won
End If
Next i

It's far cleaner than all that stuff with label captions.