PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[VB 6] Help with parallel arrays and loop statments-VBForums
Results 1 to 9 of 9

Thread: Help with parallel arrays and loop statments

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    8

    Exclamation Help with parallel arrays and loop statments

    I'm working on a battleship game in vb. I have designed the game so there are 2 grids 9 by 9 or 81 buttons each. I need help makeing the arrays and the loop statments that check through the arrays to see which has been clicked. I was wondering if anyone knew how to do this and if someone could help by supplying some code.

    Thanks and please help
    Last edited by Jokersmoke106; Apr 30th, 2013 at 11:55 AM.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Help with parallel arrays and loop statments

    This is a sample code for how to loop 2D array

    Code:
            Dim BattleShip(8, 8) As Integer ' it is 9x9 grid as the array is zero based index
            For x = 0 To 8
                For y = 0 To 8
                    If BattleShip(x, y) = 1 Then
                        ' it is clicked
                    End If
                Next
            Next



  3. #3
    Lively Member
    Join Date
    May 2009
    Location
    UK
    Posts
    68

    Re: Help with parallel arrays and loop statments

    if you have an array of buttons 0-80, then

    when a person clicks a button, its index is returned

    butt0 butt1 butt2 butt3 butt4 butt5

    eg index=5 - butt5 pressed

    eg given x and y, take action on button - button(x*9 + y).caption ="X"

  4. #4
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,252

    Re: Help with parallel arrays and loop statments

    Personally I would never ever ever use VB based objects for a Battleship game. Or any game for that matter. It ends up adding more complexity the more objects you use, and you are limited to only 255 objects on the form at once. Maybe it's time you dive into the world of actual graphics for your game. At least then you'll have more control, and the game would at least look appealing to your eyes as well as others eyes.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    8

    Re: Help with parallel arrays and loop statments

    Code:
            Dim BattleShip(8, 8) As Integer ' it is 9x9 grid as the array is zero based index
            For x = 0 To 8
                For y = 0 To 8
                    If BattleShip(x, y) = 1 Then
                        ' it is clicked
                    End If
                Next
            Next
    Thanks this helps, and the part of the code that says x = 0 to 8 and y = 0 to 8. Can u explan more about what this code does? I understand they are x and y axis but will vb understand it, then return true if the button was clicked.

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Help with parallel arrays and loop statments

    Quote Originally Posted by Jokersmoke106 View Post
    Can u explan more about what this code does?
    That code is just demonstrate how to walk through 2D array to check every item, so if your array is 4x10, the code will be
    Code:
            For x = 0 To 3
                For y = 0 To 9
                    If BattleShip(x, y) = 1 Then
                        ' it is clicked
                    End If
                Next
    Quote Originally Posted by Jokersmoke106 View Post
    but will vb understand it, then return true if the button was clicked.
    Of course not, you should check the array items according to how you store the data in it, in above sample code, i assume you are set the item at x,y to 1 if the corresponding button was clicked.

    Read the post #3 by asymetrix, it may help you.



  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,325

    Re: Help with parallel arrays and loop statments

    By the way, if this is really Vb6, arrays don't have to be 0 based, so the code may be to loop from 1 to 9, or something else, even. If you have Option Base 1 on, then the loop would be 1 to 9.
    My usual boring signature: Nothing

  8. #8
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,252

    Re: Help with parallel arrays and loop statments

    Just to let you know that code alone won't help you much. You gotta go one step further. So let me guide you in the right direction here. Lets create some structures:

    vb Code:
    1. Private Type Vertex2D
    2.      X As Long
    3.      Y As Long
    4. End Type
    5.  
    6. Private Type Ship_Type
    7.      Position As Vertex2D
    8.      Section(3) As Vertex2D
    9.      Hit(3) As Boolean
    10.      Number_Of_Targets As Long '3 or 4
    11.      Direction As Long
    12. End Type
    13.  
    14. Private P1_Ship(7) As Ship_Type
    15. Private P2_Ship(7) As Ship_Type
    16.  
    17. Private Grid_Mark(81, 81) As Boolean

    Now you need a sub to randomly setup your ships without them crossing the borders nor the other ships, and have them randomly be horizontally or vertically:

    vb Code:
    1. Private Sub Setup_Ships()
    2.      
    3.      Dim I As Long, J As Long 'For loop variables
    4.      
    5.      Randomize
    6.  
    7.      For I = 0 To 7
    8.         With P1_Ship(I)
    9.              If I >= 4 Then .Number_Of_Targets = 4 Else .Number_Of_Targets = 3
    10.              .Direction = Rnd * 1 'Horizontal or Vertical
    11.              .Position.X = Rnd * 81
    12.              .Position.Y = Rnd * 81
    13. P1_Reposition:
    14.              If .Direction = 0 Then 'Horizontal
    15.                 If .Number_Of_Targets = 4 Then
    16.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    17.                     .Section(1).X = .Position.X + 1: .Section(1).Y = .Position.Y
    18.                     .Section(2).X = .Position.X + 2: .Section(2).Y = .Position.Y
    19.                     .Section(3).X = .Position.X + 3: .Section(3).Y = .Position.Y
    20.                     .
    21.                 Else
    22.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    23.                     .Section(1).X = .Position.X + 1: .Section(1).Y = .Position.Y
    24.                     .Section(2).X = .Position.X + 2: .Section(2).Y = .Position.Y
    25.                 End If
    26.             Else 'Vertical
    27.                 If .Number_Of_Targets = 4 Then
    28.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    29.                     .Section(1).X = .Position.X: .Section(1).Y = .Position.Y + 1
    30.                     .Section(2).X = .Position.X: .Section(2).Y = .Position.Y + 2
    31.                     .Section(3).X = .Position.X: .Section(3).Y = .Position.Y + 3
    32.                 Else
    33.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    34.                     .Section(1).X = .Position.X: .Section(1).Y = .Position.Y + 1
    35.                     .Section(2).X = .Position.X: .Section(2).Y = .Position.Y + 2
    36.                 End If
    37.             End If
    38.            
    39.             For J = 0 To 3
    40.                 If .Section(J) > 81 Then GoTo P1_Reposition
    41.             Next J
    42.            
    43.             'If it all passed
    44.             If .Direction = 0 Then 'Horizontal
    45.                 If .Number_Of_Targets = 4 Then
    46.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P1_Reposition
    47.                     If Grid_Mark(.Position.X + 1, Position.Y) = False Then Grid_Mark(.Position.X + 1, Position.Y) = True Else GoTo P1_Reposition
    48.                     If Grid_Mark(.Position.X + 2, Position.Y) = False Then Grid_Mark(.Position.X + 2, Position.Y) = True Else GoTo P1_Reposition
    49.                     If Grid_Mark(.Position.X + 3, Position.Y) = False Then Grid_Mark(.Position.X + 3, Position.Y) = True Else GoTo P1_Reposition
    50.                 Else
    51.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P1_Reposition
    52.                     If Grid_Mark(.Position.X + 1, Position.Y) = False Then Grid_Mark(.Position.X + 1, Position.Y) = True Else GoTo P1_Reposition
    53.                     If Grid_Mark(.Position.X + 2, Position.Y) = False Then Grid_Mark(.Position.X + 2, Position.Y) = True Else GoTo P1_Reposition
    54.                 End If
    55.             Else 'Vertical
    56.                 If .Number_Of_Targets = 4 Then
    57.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P1_Reposition
    58.                     If Grid_Mark(.Position.X, Position.Y + 1) = False Then Grid_Mark(.Position.X, Position.Y + 1) = True Else GoTo P1_Reposition
    59.                     If Grid_Mark(.Position.X, Position.Y + 2) = False Then Grid_Mark(.Position.X, Position.Y + 2) = True Else GoTo P1_Reposition
    60.                     If Grid_Mark(.Position.X, Position.Y + 3) = False Then Grid_Mark(.Position.X, Position.Y + 3) = True Else GoTo P1_Reposition
    61.                 Else
    62.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P1_Reposition
    63.                     If Grid_Mark(.Position.X, Position.Y + 1) = False Then Grid_Mark(.Position.X, Position.Y + 1) = True Else GoTo P1_Reposition
    64.                     If Grid_Mark(.Position.X, Position.Y + 2) = False Then Grid_Mark(.Position.X, Position.Y + 2) = True Else GoTo P1_Reposition
    65.                 End If
    66.             End If
    67.         End With
    68.  
    69.         With P1_Ship(I)
    70.              If I >= 4 Then .Number_Of_Targets = 4 Else .Number_Of_Targets = 3
    71.              .Direction = Rnd * 1 'Horizontal or Vertical
    72.              .Position.X = Rnd * 81
    73.              .Position.Y = Rnd * 81
    74. P2_Reposition:
    75.              If .Direction = 0 Then 'Horizontal
    76.                 If .Number_Of_Targets = 4 Then
    77.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    78.                     .Section(1).X = .Position.X + 1: .Section(1).Y = .Position.Y
    79.                     .Section(2).X = .Position.X + 2: .Section(2).Y = .Position.Y
    80.                     .Section(3).X = .Position.X + 3: .Section(3).Y = .Position.Y
    81.                     .
    82.                 Else
    83.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    84.                     .Section(1).X = .Position.X + 1: .Section(1).Y = .Position.Y
    85.                     .Section(2).X = .Position.X + 2: .Section(2).Y = .Position.Y
    86.                 End If
    87.             Else 'Vertical
    88.                 If .Number_Of_Targets = 4 Then
    89.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    90.                     .Section(1).X = .Position.X: .Section(1).Y = .Position.Y + 1
    91.                     .Section(2).X = .Position.X: .Section(2).Y = .Position.Y + 2
    92.                     .Section(3).X = .Position.X: .Section(3).Y = .Position.Y + 3
    93.                 Else
    94.                     .Section(0).X = .Position.X: .Section(0).Y = .Position.Y
    95.                     .Section(1).X = .Position.X: .Section(1).Y = .Position.Y + 1
    96.                     .Section(2).X = .Position.X: .Section(2).Y = .Position.Y + 2
    97.                 End If
    98.             End If
    99.            
    100.             For J = 0 To 3
    101.                 If .Section(J) > 81 Then GoTo P2_Reposition
    102.             Next J
    103.            
    104.             'If it all passed
    105.             If .Direction = 0 Then 'Horizontal
    106.                 If .Number_Of_Targets = 4 Then
    107.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P2_Reposition
    108.                     If Grid_Mark(.Position.X + 1, Position.Y) = False Then Grid_Mark(.Position.X + 1, Position.Y) = True Else GoTo P2_Reposition
    109.                     If Grid_Mark(.Position.X + 2, Position.Y) = False Then Grid_Mark(.Position.X + 2, Position.Y) = True Else GoTo P2_Reposition
    110.                     If Grid_Mark(.Position.X + 3, Position.Y) = False Then Grid_Mark(.Position.X + 3, Position.Y) = True Else GoTo P2_Reposition
    111.                 Else
    112.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P2_Reposition
    113.                     If Grid_Mark(.Position.X + 1, Position.Y) = False Then Grid_Mark(.Position.X + 1, Position.Y) = True Else GoTo P2_Reposition
    114.                     If Grid_Mark(.Position.X + 2, Position.Y) = False Then Grid_Mark(.Position.X + 2, Position.Y) = True Else GoTo P2_Reposition
    115.                 End If
    116.             Else 'Vertical
    117.                 If .Number_Of_Targets = 4 Then
    118.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P2_Reposition
    119.                     If Grid_Mark(.Position.X, Position.Y + 1) = False Then Grid_Mark(.Position.X, Position.Y + 1) = True Else GoTo P2_Reposition
    120.                     If Grid_Mark(.Position.X, Position.Y + 2) = False Then Grid_Mark(.Position.X, Position.Y + 2) = True Else GoTo P2_Reposition
    121.                     If Grid_Mark(.Position.X, Position.Y + 3) = False Then Grid_Mark(.Position.X, Position.Y + 3) = True Else GoTo P2_Reposition
    122.                 Else
    123.                     If Grid_Mark(.Position.X, Position.Y) = False Then Grid_Mark(.Position.X, Position.Y) = True Else GoTo P2_Reposition
    124.                     If Grid_Mark(.Position.X, Position.Y + 1) = False Then Grid_Mark(.Position.X, Position.Y + 1) = True Else GoTo P2_Reposition
    125.                     If Grid_Mark(.Position.X, Position.Y + 2) = False Then Grid_Mark(.Position.X, Position.Y + 2) = True Else GoTo P2_Reposition
    126.                 End If
    127.             End If
    128.         End With
    129.     Next I
    130. End Sub

    That sub should randomly setup player 1 and player 2s ships around the grid without crossing each other and without passing the boundry both either randomly horizontally or vertically with 4 ships being 3 hit and 3 ships being 4 hit. Now the buttons you have (which I do not recommend using) can check the Grid_Mark(X,Y) to see if it is the opposing players ship and mark Hit() if it is, then it can check to see if all sections of the ship has been hit to confirm its been destroyed. Hopefully this should help you get in the right direction. And if you need help doing it graphically rather than using lame vb buttons, Id be more than willing to help you build this.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,325

    Re: Help with parallel arrays and loop statments

    You should probably move the Randomize call out of that method. It does have to be called somewhere, and it may be safe in that method, but the method could easily be called more twice in short order, and if that were the case, having the Randomize in the method would cause your random numbers to be anything but random. It would be better to put the Randomize call somewhere else, such as a Load event, or even earlier.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width