Results 1 to 11 of 11

Thread: Tic-Tac-Toe Game - Loops Needed

  1. #1

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    Attached is my Tic-Tac-Toe game. It is not finished - I still need to write the Hard level AI and the End-Game determination loop and fix a few minor bugs.

    What I need is Help writing a loop to determine when the computer or the player wins. I could also use some Ideas on how to condense the code. I already know how to fix the bugs and write the Hard AI. Thanks guys!
    -Justin
    Attached Files Attached Files

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Christ thats a lot of code ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    To check if someone won :
    (first I'd create a control array for the labels)

    Code:
        PlayerWon = False
        For i = 1 To 4
           Select Case i
               Case 1:
    	    PlayerWon = (Lbl(i).Caption = Lbl(i + 1).Caption) And (Lbl(i + 1).Caption = Lbl(i + 2).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 3).Caption) And (Lbl(i + 3).Caption = Lbl(i + 7).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 4).Caption) And (Lbl(i + 4).Caption = Lbl(i + 8).Caption)
               Case 2:
    	    PlayerWon = (Lbl(i).Caption = Lbl(i - 1).Caption) And (Lbl(i - 1).Caption = Lbl(i + 1).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 3).Caption) And (Lbl(i + 3).Caption = Lbl(i + 6).Caption)
               Case 3:
    	    PlayerWon = (Lbl(i).Caption = Lbl(i - 2).Caption) And (Lbl(i - 2).Caption = Lbl(i - 1).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 2).Caption) And (Lbl(i + 2).Caption = Lbl(i + 4).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 3).Caption) And (Lbl(i + 3).Caption = Lbl(i + 6).Caption)
               Case 4:
    	    PlayerWon = (Lbl(i).Caption = Lbl(i - 3).Caption) And (Lbl(i - 3).Caption = Lbl(i + 3).Caption)
    	    PlayerWon = PlayerWon Or (Lbl(i).Caption = Lbl(i + 1).Caption) And (Lbl(i + 1).Caption = Lbl(i + 2).Caption)                
           End Select
        Next i

    Something like that should work ...

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    The computer player moves to the same spot each time I move to a certain place...

    WOW I thought you were using loops! Why so much code?????????

    I have a multiplayer tic tac toe game on my site. That will show you about how much code you should have.

    Wow you really made it hard on yourself!

  5. #5

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    That's why I am asking for help - a single player game is alot tougher to code than a mulitplayer, and I could not think of a way to incorporate loops into the AI - I will check out your game, though.

    Thanks Plenderj - I will try it!

    I still need to reduce that code, though....

  6. #6

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

    Smile

    Obviously, I ripped no one off, Steve.

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I'd rip his code
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    It's on my site for the taking

    Just remember its internet play only.

  9. #9

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    actually, steve took an entirely different approach than I am with mine - the only part I really need help with is the Computer movement & Win-Check coding. Steves is a single player, therefore not having any AI code. Still inspired some of my ideas, though....

  10. #10

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    I meant steves was NOT multiplayer. Braindead today.

    The internet playing was a cool idea, too.

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well the code I posted above, or a modification thereof, would tell you who won.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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