Results 1 to 10 of 10

Thread: [RESOLVED] battleship help msflexgrid

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Resolved [RESOLVED] battleship help msflexgrid

    This is what the computer's grid looks like. How to make computer randomly place his ships on the grid. Place vertically or horizontally randomly. The ships are: 1 ship of length 5, 1 ship of length 4, 1 ship of length 3, 1 ship of length 3, 1 ship of length 2. The grid will still look like this after the ships are placed. When i click on the grid how do i check for a hit or a miss? if it's a hit i want to change the CellBackColor to red or if it's miss then i change CellBackColor to white.
    Edit: Ty for help
    Edit: when i click on computer's grid how do i detect if a ship is fully sunk?
    Attached Images Attached Images  
    Last edited by firezap; May 22nd, 2013 at 09:40 PM. Reason: new question

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: battleship help msflexgrid

    Quote Originally Posted by firezap View Post
    How to make computer randomly place his ships on the grid. Place vertically or horizontally randomly.
    For each ship, pick random values for row/column/direction.

    If you don't know how to generate random numbers, see the tutorial about them in our Classic VB FAQs (in the FAQ forum)

    Check that the ship can be placed at that location (ie: it doesn't go off the edge of the grid, or share a square with a ship that has already been placed), and if not pick a new set of random numbers for that ship and try again.

    Once you have made sure the position is valid, store it.
    The grid will still look like this after the ships are placed. When i click on the grid how do i check for a hit or a miss? if it's a hit i want to change the CellBackColor to red or if it's miss then i change CellBackColor to white.
    In that case you should store the values to an array rather than directly to the grid (if you don't know about arrays, see the FAQs link above).

    When a click happens, check the value in the array, and set the grid cell colour as apt.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: battleship help msflexgrid

    Dim Grid(10,10) as Integer
    RndCol = Int((10 * Rnd) + 1)
    RndRow = Int((10 * Rnd) + 1)
    RndDir = Int((2 * Rnd) + 1)

    Quote Originally Posted by si_the_geek View Post
    Check that the ship can be placed at that location (ie: it doesn't go off the edge of the grid, or share a square with a ship that has already been placed), and if not pick a new set of random numbers for that ship and try again.
    I don't know how.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: battleship help msflexgrid

    To check if it goes off the edge of the grid, work out what the end position is, and check it against the amount of rows/columns. For example, if the ship of length 5 starts at row 3 column 8, and is horizontal, there aren't enough columns (because the end position is 8+5-1, which is more than 10).

    To check if it shares a square with a ship that has already been placed, just read the elements of the array for the squares it will be put in to.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: battleship help msflexgrid

    any1 have the code?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: battleship help msflexgrid

    do i need a do while loop

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: battleship help msflexgrid

    Sure man, try this http://www.vbforums.com/showthread.p...=1#post4405323

    I answered this Battleship question the other day requesting how to do this. But if you wanna make your game look good visually, dont use a msflexgrid. Now that code I linked, you may need to modify it a notch to abide by the rules of how many hits each ship gets because I put 4 ships with 3 hits and 3 ships with 4 hits randomly placed on the grid.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: battleship help msflexgrid

    While storing data to a type could well be useful, that code is far larger than needed, and it uses the best-to-avoid GoTo without a valid reason and in ways that causes problems (eg: it sets Grid_Mark then jumps out, leaving cells unusable).

    Here is a much simpler version based on what I explained:
    Code:
    Dim Grid(10,10) as Integer
    Dim Valid as Boolean
    Dim Row as Long, Col as Long
    
    ShipLength = 5
    Do
      Valid = True
      RndCol = Int((10 * Rnd) + 1)
      RndRow = Int((10 * Rnd) + 1)
      RndDir = Int((2 * Rnd) + 1)
    
      'Make sure the position is inside the grid
      If RndDir = 0 Then   'I assumed 0 means horizontal
        If RndCol + ShipLength - 1 > 10 Then Valid= False
      Else
        If RndRow + ShipLength - 1 > 10 Then Valid= False
      End If
    
      If Valid Then
        'Make sure there is no overlap with another ship
        If RndDir = 0 Then   'I assumed 0 means horizontal
          For Col = RndCol To RndCol + ShipLength -1
            If Grid(Column, RndRow) > 0 Then Valid= False: Exit For
          Next Col
        Else
          For Row = RndRow To RndRow + ShipLength -1
            If Grid(RndCol, Row) > 0 Then Valid= False: Exit For
          Next Column    
        End If
      End If
    Loop Until Valid
    
        'Set the grid cells
    If RndDir = 0 Then   'I assumed 0 means horizontal
      For Col = RndCol To RndCol + ShipLength -1
        Grid(Col, RndRow) = 1
      Next Col
    Else
      For Row = RndRow To RndRow + ShipLength -1
        If Grid(RndCol, Row) = 1
      Next Row
    End If
    This needs to be placed inside a loop for the ships (with the ShipLength=5 line changed to something apt), but that is only a few simple lines.

    This doesn't store data to a type, but if you want that then you only need a few more lines of code (in the 'Set the grid cells section)... about 5 lines to do the equivalent of Jacobs code.
    Last edited by si_the_geek; May 21st, 2013 at 03:44 AM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    16

    Re: [RESOLVED] battleship help msflexgrid

    when i click on computer's grid how do i detect if a ship is fully sunk?

  10. #10
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [RESOLVED] battleship help msflexgrid

    Well if you took a look at my code, you would have noticed i set up some variables for each players ship in the structure. Just have a for loop go through each ship to check if all 2, 3, or 4 sections of the ship have been hit depending on the ship type. A select case statement in the for loop will check the ship type.

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