|
-
May 19th, 2013, 09:41 PM
#1
Thread Starter
Junior Member
-
May 20th, 2013, 05:22 AM
#2
Re: battleship help msflexgrid
 Originally Posted by firezap
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.
-
May 20th, 2013, 11:35 AM
#3
Thread Starter
Junior Member
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)
 Originally Posted by si_the_geek
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.
-
May 20th, 2013, 12:07 PM
#4
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.
-
May 20th, 2013, 02:41 PM
#5
Thread Starter
Junior Member
Re: battleship help msflexgrid
-
May 20th, 2013, 07:27 PM
#6
Thread Starter
Junior Member
Re: battleship help msflexgrid
do i need a do while loop
-
May 20th, 2013, 08:02 PM
#7
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.
-
May 21st, 2013, 03:39 AM
#8
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.
-
May 22nd, 2013, 09:41 PM
#9
Thread Starter
Junior Member
Re: [RESOLVED] battleship help msflexgrid
when i click on computer's grid how do i detect if a ship is fully sunk?
-
May 22nd, 2013, 09:47 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|