
-----------------------------------
Sur_real
Tue May 13, 2008 4:52 pm

Battleship Game - Placing random ships
-----------------------------------
I need help on how to place 2 random ships on a 5 by 5 grid for my battleship game using arrays and is inside a procedure.

I already read the other theads regarding battleship games so there's no use linking me to them...

Thanks


(Nothing too hard please, i'm really noobie...)

-----------------------------------
Tony
Tue May 13, 2008 5:07 pm

RE:Battleship Game - Placing random ships
-----------------------------------
A really simple (to understand) approach would be to pick a random spot on the grid

row := Rand.Int(0,4)
column := Rand.Int(0,4)

+ the orientation of the ship (up/down, left/right)

orientation =: Rand.Int(0,1)

And then check if all the spots are available (that is, there are no overlapping ships and the array is not out of bound -- you need to be careful about the latter)

for i: row .. row + length_of_ship
   if array

a more sophisticated way is to realize that not all starting locations are valid for certain ship sizes and orientations, so narrowing down the range of choices will optimize the performance and will (hopefully) avoid out-of-bounds errors.

-----------------------------------
Sur_real
Tue May 13, 2008 5:23 pm

RE:Battleship Game - Placing random ships
-----------------------------------
Thanks Tony 
but what do you mean by the brackets...
 if array[i][column] then 


-----------------------------------
Tony
Tue May 13, 2008 5:34 pm

RE:Battleship Game - Placing random ships
-----------------------------------
I meant the cell at row/column position in the grid/2d-array

-----------------------------------
Sur_real
Tue May 13, 2008 5:46 pm

RE:Battleship Game - Placing random ships
-----------------------------------
Oooo
ok
