View Poll Results: Should I just completely forget about this TicTacToe game and being a programmer?

Voters
8. You may not vote on this poll
  • Yes

    2 25.00%
  • No

    3 37.50%
  • What is TicTacToe?

    0 0%
  • All Your Base Are Belong To Us

    3 37.50%
Results 1 to 3 of 3

Thread: What is wrong with this code? (A bigass loop)

  1. #1

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116

    Post

    hey!
    this is the prototype of the WinCheck function of my TicTacToe game. The labels (0-8) are the 9 squares you play on.
    Code:
    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.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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):

    Code:
    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:
    Code:
    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.
    Harry.

    "From one thing, know ten thousand things."

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