2D Arrays 
Author Message
 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,

Yours Sincerely Adan Rehtla



Sun, 16 Mar 2003 10:00:19 GMT  
 2D Arrays

Quote:

> 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

______________________________|_______________________________________



Sun, 16 Mar 2003 11:36:00 GMT  
 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:

> 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,

> Yours Sincerely Adan Rehtla



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. 2D array

2. Randomly Placings On A 2D Array

3. 2d arrays

4. 2d array file stuff ..thanks

5. 2d array file errors

6. thanks for the 2d arrays

7. very large 2d array

8. writing a text file to a 2d array -HELP

9. Large 2d arrays

10. Filling 2D Arrays

11. adding 2d array info to string grid on another form

12. Knowingly over indexing a 2d array, anyone done it?

 

 
Powered by phpBB® Forum Software