Board index » delphi » 2D Arrays

2D Arrays

Sorry guys but i was to late at getting the last message. Could you please
help me.

I am writing a game of battle ships and i would like to know how i could
randomly place a set of 5, 4, 3, and 2 unit long 'boats' on the face of the
2D array.

Sorry if i have caused any inconvenence,

Re:2D Arrays

Quote
Sandra Rehtla <sreh...@optusnet.com.au> wrote:
> Sorry guys but i was to late at getting the last message. Could you please
> help me.

You can try to use DejaNews to look for past messages, although it can be
unreliable.

Quote
> I am writing a game of battle ships and i would like to know how i could
> randomly place a set of 5, 4, 3, and 2 unit long 'boats' on the face of the
> 2D array.

How would you normally do it? That's always the first question to ask.

Speaking for myself, I place the largest units first and work my way down,
because it's easier that way; so you have a "for" loop here.

For each unit, pick a starting point and a direction (up, down, left or
right), and check whether that's a valid combination (doesn't go off the
board and no other ships are in the way) - keep doing this until you get a
valid combination; this is a "while" or "repeat" loop (probably the latter
is better here). Then place the unit on the indicated spot and mark the
appropriate squares as used (this will require another "for" loop).

You'll need some sort of array to hold the data on which spaces have been
used and which are clear.

--
______________________________________________________________________
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
______________________________|_______________________________________

Re:2D Arrays

Think of how you would "randomly" do it using, say, some graph-ruled paper and
paper "ships".  Here are some hints or things to think about:

1)  Given, say, a 1 x 5 "battleship", in how many orientations can you place it
on the game board?

2)  Think about the symmetry of the problem.  Suppose I have a particular
orientation for the ship, and I tell you where the upper-most, left-most square
goes.  Do I have to specify anything else?  [Or does that one square determine
the rest of the placement?]

3)  If I use the "placement algorithm" suggested by questions 1 and 2, are
there any restrictions on how I place, say, a 1 x 5 battleship on, say, a 10 x
10 board?  [The answer is "yes", as can be shown by the "ridiculous" example of
a battleship on a 4 x 4 board -- it won't fit!  Think about "not fitting", and
what that means -- that should give you the restrictions you need.]

Bob Schor
Pascal Enthusiast

Quote
Sandra Rehtla wrote:
> Sorry guys but i was to late at getting the last message. Could you please
> help me.

> I am writing a game of battle ships and i would like to know how i could
> randomly place a set of 5, 4, 3, and 2 unit long 'boats' on the face of the
> 2D array.

>  Sorry if i have caused any inconvenence,