Thread: Battleship program, two questions?

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    Battleship program, two questions?

    First of all, to set up the array that defines the board I thought I was suppose to use:

    myArray[SIZE][SIZE];

    but someone said that using calloc would be better. I am not familiar with it, and dont see how it could be applied here.

    2nd, we are suppose to just get to the part where the board is set up with random computer ships (no further) but I have no idea how to set up the function to randomly assign ships. Here are the instructions:

    CSCE 1030
    You shall write a function
    to assign a ship to a location on the board, given the
    data
    structure for the board passed by reference and
    another parameter providing some
    identifying factor, such as the length or type of the ship.
    This function is called by
    main()
    and is to only assign
    (or attempt to assign) one ship that is passed to it.
    You
    shall randomly generate the orientation (i.e., either vertical or horizontal) of the ship.
    You shall also randomly generate the row and column position for the start of the ship. If
    the ship does n
    ot fit on the board at this starting location, you can either return
    information indicating that the ship could not be assigned (and so it will be up to
    main()
    to keep calling this
    function until it is successfully assigned) or keep randomly
    generating row
    and column positions for the ship
    ’s starting position until one is found
    that can be used to successfully assign the ship. A ship may be assigned anywhere on
    the board (as long as it fits), but it may not be assigned on top of another ship that
    exists at
    that location.
    This must be a single function designed to handle all ships.
    You will also write a function to display the game board, giving the data structure for the
    board passed by reference.
    It
    is called by
    main()
    and is invoked anytime th
    e board has
    b
    een updated (for this homework, it is invoked after the aircraft carrier and battleship
    have been placed on the board).
    This function
    will display the row and column headers
    for the board as well as the board itself
    (see sample output). Open space on the board
    (i.e., where no ships have been assigned) shall be indicated by a blank space on the
    board. The aircraft carrier shall be represented by an
    ‘A’ for each of its 5 unit length,
    while the battleship shall be repres
    ented by a ‘B’ for each of its 4 unit length.
    Any help would be great, thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    calloc() is for dynamic memory allocation and initialising the allocated memory to zero. For setting up a 2D array using calloc(), there is additional work (e.g. keeping track of multiple calloc() calls) and few obvious obvious benefits, so I wouldn't bother for a small array (and the typical 10x10 grid for battleship qualifies as small).

    To set up ships in random positions, all you need is a random number generator. The relevant functions in standard C are srand() [seed random number generator] which must be called only ONCE throughout a program, and rand() to subsequently retrieve random values. Once you have random values, use them somehow to set positions and orientation of your ships, but just remember to check that ships don't intersect each other.

    I'll leave the problems of seeding (for example, providing srand() a different seed every time your program runs) and of ensuring random numbers are in a range (for example, to set up a 10x10 grid, you might want random values between 0 and 9) as exercises for you to work out.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-04-2013, 09:15 PM
  2. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 08:40 PM
  3. question on battleship program.
    By newbc in forum C Programming
    Replies: 0
    Last Post: 12-07-2010, 08:32 AM
  4. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  5. Battleship program
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 09:02 AM