Results 1 to 3 of 3
  1. #1
    buckbuck2 is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Battleship program

    Write a program that allows a user to play a pre-determined number of text-based games of Battleship.

    Assume that Battleship will be played on one of the following five board sizes: 5 by 5, 6 by 6, 7 by 7, 8 by 8 or 9 by 9. In the provided Driver above, two games of Battleship will be played, each on a 5 by 5 board. However, make sure that your program works for the other dimensions as well.

    The following ships must be placed on the board in random locations such that they appear in either contiguous horizontal or vertical squares and do not overlap: an aircraft carrier of length 5, a battleship of length 4, a submarine of length 3 and a patrol boat of length 2.

    When a user plays a game of battleship, she will be prompted for the 1-based row and column of her next guess. The game will keep track of how many guesses it takes the user to guess where each component of each ship is located.

    The Driver program was included:

    public class Driver
    {
    public static void main (String [] args)
    {
    Battleship game = new Battleship(5); // play on a 5 by 5 board

    System.out.println("Battleship!");
    System.out.println("-----------\n");

    for (int gameNumber = 1; gameNumber <= 2; gameNumber++)
    {
    game.resetBoard();

    System.out.println("Revealed board (for verification purposes):\n");
    game.printBoard(true); // print contents of all squares

    while (!game.over())
    {
    game.printBoard(false); // only print known squares
    game.makeGuess();
    }

    game.printStatistics();
    }

    System.out.println("Goodbye. Thanks for playing.");
    }
    }

    As well as a square class
    public class Square
    {
    private boolean known; // has the user already guessed this square?
    private int contents; // the contents of the square (empty, patrol boat, etc.)

    /**
    * Reader method.
    *
    * @return whether the user has guessed the square
    */

    public boolean getKnown()
    {
    return known;
    }

    /**
    * Writer method
    *
    * @param value indicates whether the user has guessed the square
    */

    public void setKnown(boolean value)
    {
    known = value;
    }

    /**
    * Reader method.
    *
    * @return an integer indicating the contents of the square (e.g. empty, submarine, etc.)
    */

    public int getContents()
    {
    return contents;
    }

    /**
    * Writer method.
    *
    * @param value indicates the contents of the square (e.g. empty, battleship, etc.)
    */

    public void setContents(int value)
    {
    contents = value;
    }
    }

    I need to make the Battleship class to make the program run. Help!

    Also, I have included a transcript of what it should look like:

    +-+-+-+-+-+
    1 | | | | | |
    +-+-+-+-+-+
    2 | | | | | |
    +-+-+-+-+-+
    3 | | | | | |
    +-+-+-+-+-+
    4 | | | | | |
    +-+-+-+-+-+
    5 | | | | | |
    +-+-+-+-+-+
    1 2 3 4 5

    Enter a row to guess > 1
    Enter a column to guess > 2
    Hit! You just sunk part of the Submarine

    +-+-+-+-+-+
    1 | |3| | | |
    +-+-+-+-+-+
    2 | | | | | |
    +-+-+-+-+-+
    3 | | | | | |
    +-+-+-+-+-+
    4 | | | | | |
    +-+-+-+-+-+
    5 | | | | | |
    +-+-+-+-+-+
    1 2 3 4 5

    Enter a row to guess > 2
    Enter a column to guess > 2
    Hit! You just sunk part of the Submarine


    Thanks!

  2. #2
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Battleship program

    Well, we can't just give you the code, but we can help you with any errors or problems ect. also I would suggest if you're going for flexibility in the program, you could just as easily make the Battleship class like:

    Java Code:
    Battleship game = new Battleship(5, 5); // play on a 5 by 5 board
    That way you can change either dimension. Now to start off, the class should have an array to store this data, and have all the functions required to make the board and whatever else you need it to do. Or at least I'm assuming what the class is for. Of course, there will probably be people who give better advice than me xD

    EDIT: I forgot, you also need [ CODE] [ /CODE](no spaces) tags to put around your code, to make it look all neat.
    Last edited by Darkzombies; 11-28-2012 at 01:56 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Battleship program

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Battleship Program
    By zizou001 in forum New To Java
    Replies: 6
    Last Post: 11-15-2012, 08:21 AM
  2. Help with Revised Battleship program
    By Lucid15 in forum New To Java
    Replies: 1
    Last Post: 04-19-2012, 07:18 AM
  3. Having trouble, Battleship program
    By Corrrrr in forum New To Java
    Replies: 15
    Last Post: 03-09-2012, 03:08 AM
  4. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM
  5. need help creating a battleship like program
    By ss1 in forum New To Java
    Replies: 12
    Last Post: 08-19-2011, 12:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •