View Poll Results: Can this loop be written?

Voters
3. You may not vote on this poll
  • Yes, and you are an idiot for not knowing

    3 100.00%
  • No Way

    0 0%
Results 1 to 12 of 12

Thread: Looping through a control array - DEAR GOD HELP ME!!!!

  1. #1

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

    Question

    Hey guys,

    Yes this is related to my single player TTT game that still tortures me. OK - I have 9 labels in a control array like this:

    0 1 2
    3 4 5
    6 7 8

    I already have the code in place so that whenever you click on a label, it checks to see if it is available or not. What I need now is a loop or 3 to determine what move the computer is going to make next.

    I already have the basics planned out - there are 3 difficulty levels:

    Easy -
    1. AI trys for the center square first - lblMove(4) becuase it is in 4 of the 8 possible Win lines (012, 345, 678, 036, 147, 258, 048, & 246).

    2. Then it randomly tries for on of the 4 corners (0, 2, 6, 8) - they have 3 possible win lines.

    3. Finally it randomly chooses one of the four remaing squares (1, 3, 5, 7).

    Medium -
    AI looks for one of the 24 possible eminent human player wins along the 8 win lines (12x, 69x, 57x, etc) and attempts to block it. If there are no eminent human wins, the AI moves on to the Easy level moves.

    Hard -
    AI looks for one of the 24 possible eminent Computer player wins along the 8 win lines and attempts to win. If there are no eminent computer wins, the AI moves on to the Medium level moves.

    I have attached my current form. The game is basically complete except for the AI moves and the wincheck loop. I already know how to approach that, I just need to get this other part in first.

    please please PLEASE help me if you think you can!

    Thank you!!!

    -Justin

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Did you decide against the approach suggested before?
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    Not yours - I have some questions about it though. Check the other thread. Thanx!

    -Justin

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Yes, I just found it (oops)
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I hardly ever check the general VB forum any more by the way (only when I'm really bored), but I check games & graphics a lot, so it might be better if we continue it here.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    WeHaveAWinnder() Function.
    Note: Set the caption of the boxes to "", not " ".

    Code:
    Private Function WeHaveAWinner() As Boolean
        Dim i As Long
        For i = 0 To 3
            Select Case i
                Case 0:
                    If ((lblMove(0).Caption <> "") And (lblMove(1).Caption <> "") And (lblMove(2).Caption <> "")) Then If (lblMove(0).Caption = lblMove(1).Caption) And (lblMove(1).Caption = lblMove(2).Caption) Then WeHaveAWinner = True
                    If ((lblMove(0).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(8).Caption <> "")) Then If (lblMove(0).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(8).Caption) Then WeHaveAWinner = True
                    If ((lblMove(0).Caption <> "") And (lblMove(3).Caption <> "") And (lblMove(6).Caption <> "")) Then If (lblMove(0).Caption = lblMove(3).Caption) And (lblMove(3).Caption = lblMove(6).Caption) Then WeHaveAWinner = True
                Case 1:
                    If ((lblMove(1).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(7).Caption <> "")) Then If (lblMove(1).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(7).Caption) Then WeHaveAWinner = True
                Case 2:
                     If ((lblMove(2).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(6).Caption <> "")) Then If (lblMove(2).Caption = lblMove(4).Caption) And (lblMove(6).Caption = lblMove(2).Caption) Then WeHaveAWinner = True
                Case 3:
                    If ((lblMove(3).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(5).Caption <> "")) Then If (lblMove(3).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(5).Caption) Then WeHaveAWinner = True
            End Select
        Next i
    End Function
    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7

    Thread Starter
    Lively Member amesjustin's Avatar
    Join Date
    Feb 2001
    Location
    Orange County, California, USA
    Posts
    116
    Thanks for the reply, jamie, but that piece of code I already have written (Similar to yours).

    What I was looking for was a loop that would determine what moves the computer player would make in response to the Human players moves.

    Thanks,

    -Justin

  8. #8

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

    I cannot get these 2 peices of code to work - I get an expected end of statement error:



    code:--------------------------------------------------------------------------------Dim LookupArray(1 To 3)(1 To 8) As Coord2D--------------------------------------------------------------------------------


    code:--------------------------------------------------------------------------------Dim GridArray(1 To 3)(1 To 3) As String*1--------------------------------------------------------------------------------


    I also am having a hard time following the logic structure of this code:

    code:--------------------------------------------------------------------------------LookupArray(1, 1).X = 1
    LookupArray(1, 1).Y = 1

    LookupArray(2, 1).X = 1
    LookupArray(2, 1).Y = 2

    LookupArray(3, 1).X = 1
    LookupArray(3, 1).Y = 3
    --------------------------------------------------------------------------------


    If you or anyone else can shed a little light on this, I would be VERY appreciative!!!

    -Justin

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay well if you remember this is what I said:
    You have your board, a 3x3 array like this
    _ _ _
    |_|_|_|
    |_|_|_|
    |_|_|_|

    If you number them 1 to 9 like this:

    123
    456
    789

    then you have the 8 possible winning lines of 3 here in this list:

    123,
    456,
    789,
    147,
    258,
    369,
    159,
    357.

    So what you do is you create a second array to represent the list you have, so that each row in the array represents a winning line. This array will be 8 rows by 3 columns. Call this the lookup array.

    When you add a 0 or X to the 3x3 grid array, you would add that same 0 or X to each of the entries In the lookup array. So, say you had this grid:

    0|_|_
    0|X|_
    X| |0

    Your lookup array would be like this:

    0 _ _
    0 X _
    X _ 0
    0 0 X
    _ X _
    _ _ 0
    0 X 0
    _ X X
    You have to fill in your lookup array with the coordinates of the grid squares that are indicated by the numbers on this grid:

    123
    456
    789
    147
    258
    369
    159
    357

    where your grid array looks like this:

    123
    456
    789

    The code I gave you was just setting up the coordinates in the lookup array so that they point to the right square. I'm not sure I can explain it any better than that. It's a fairly simple idea, perhaps it's difficult to get it across in text. Remember, this code is executed one time only, at startup. It's not working out the next move, it's creating a resource for you to use in your main game code, to make life easier.

    I don't know why those array declarations aren't working Maybe it's because of where you're putting them. Maybe try ReDim instead of Dim?
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Dim A(1 to 8,1 to 3) as variant

    THAT is the format, not

    Dim A(1 to 8)(1 to 3) as variant
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ohhh yeah, sorry. I'm used to C++ syntax, I got confused
    Harry.

    "From one thing, know ten thousand things."

  12. #12
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Sub FillLookUpArray()
    ... 'code here, such as
    LUA(1,1)=GridArray(1,1)
    LUA(2,1)=GridArray(2,1)
    LUA(3,1)=GridArray(3,1)
    LUA(1,2)=GridArray(1,2)
    LUA(2,2)=GridArray(2,2)
    LUA(3,2)=GridArray(3,2)
    ... etc, following HarryW's guidelines.
    End Sub
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

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