Quote
Sandra Rehtla <sreh...@optusnet.com.au> wrote:
> I'm writing the game battleships and I need some help.
What have you written so far?
Quote
> Firstly, I have loaded a 2D array.
> Secondly, I need to randomly place 4 'Ships' of different sizes onto the
> grid. The ships are 5, 4, 3 and 2 units long and the ships can only be place
> horizontally or vertically.
> I think that I may have to put a FOR LOOP inside a FOR LOOP or something
> similar.
> How on earth do I do this?
Well, you can include a FOR loop inside another FOR loop, it's nothing
special.
For the actual process, break it down into steps. I'd suggest the following
strategy:
Start with the biggest ship and work down to the smallest (this will involve
a FOR loop).
For each ship, pick a random spot to place it and a random orientation (do I
want to go left, right, up or down from my starting point?) - then attempt
to place it there. This may succeed or fail (e.g. if you run out of space or
if there's another ship in the way). If it fails, try again (this will
probably be a REPEAT loop). If it succeeds you can move on to the next ship.
When you've done all the ships, you're ready to go.
Now obviously this is a very broad outline - you'll need to break things
down into simpler steps (e.g. how do I 'attempt' to put a ship on the board?
What do I do if it fails, or for that matter, if it succeeds?) - if you have
trouble, post some partially-working code and you'll get tons of advice on
how to fix it up.
--
______________________________________________________________________
The Scarlet Manuka, | Nitpickers' Party motto:
Pratchett Quoter At Large, | "He who guards his lips guards his
First Prophet of Bonni, is: | soul, but he who speaks rashly will
sa...@maths.uwa.edu.au | come to ruin." -- Proverbs 13:3
______________________________|_______________________________________