Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Rep Power

    Default battleship in java

    have no clue what they want out of this

    Modified "Battleship"
    The classic version of the game Battleship consists of two players who attempt to sink all of the opposing players' ships. In the full original version, each player has a total of 6 ships ranging in size from 2 to 6 blocks in the game board. For the purpose of this project, you will be using 3 ships, 1 each of sizes 2, 3 and 4.
    Game Boards
    To simulate the game, there will be one "game board" for each player. Each board is a 5x5 two-dimension array of integers with the following values possible in each cell:
    0 There is no ship in this cell
    2 Part of a ship of size 2 is in this cell.
    3 Part of a ship of size 3 is in this cell.
    4 Part of a ship of size 4 is in this cell.
    -1 Part of a ship has been sunk in this cell.
    -2 The player took a shot at this cell and missed.
    A ship can only be laid out across or up and down and not diagonal. This rule is the same as the original version.
    Here is a possible initial set up of a game board:

    Here is the game
    board after "hitting" cells [0,1], [1,2], [3,0] and [0,4]

    4- 1 4 4 -2
    0 0-1 0 0
    0 0 2 0 0
    0 3-2 0 0
    0 3 3 3 0

    Setting up the Game Boards
    Write the following method for your project:
    private static void readFile(String filename, int[][] player)
    There will be 2 text files, one for each game board, named player1.txt and player2.txt. These will contain the coordinates for placing the ships on the board. The line in each file will be of the following format:
    where the pipe ("|") symbol separates the total number of coordinates. And the number of coordinates tell you the ship's size. The ship of size 2 will have two coordinates, size 3 will have 3 coordinates and size 4 will have 4 coordinates. So, the following file would generate the example game board above:
    0,0|0,1|0,2|0,3 4,1|4,2|4,3 1,2|2,2
    You will need to tokenize each line of the file using a StringTokenizer, first using the "|" to separate the coordinates, then, for each coordinate, tokenize using "," to get each coordinate number (as a String) and convert the String to integer, and place the proper value into the game board grid (recall, the number of coordinate tokens equals the size of the ship.)
    At this point, we will not have any errors in the input file (coordinates out of bounds, or ships that go on top of each other, for example.)
    Printing the Game Board
    Write the following method for your project:
    private static String print(int[][] player)
    This method will return a String which, when displayed will show the player's game board as follows:
    "H" means the cell has a ship that has been hit.
    "X" means a miss
    "+" means the cell is still in play.
    So, printing the following game board:
    4- 1 4 4 -2
    0 0-1 0 0
    0 0 2 0 0
    0 3-2 0 0
    0 3 3 3 0
    Would result in the string:
    " + H + + X\n + + H + +\n + + + + +\n X + + + +\n + + + + +\n"
    Which would be displayed to the console or to a JOptionPane as
    + H + + X
    + + H + +
    + + + + +
    X + + + +
    + + + + +

    Playing the Game
    Players will alternate entering coordinates into a JOptionPane. Part of the string the JOptionPane displays should he the player's game board:
    After tokenizing, you must perform some error checking on the input: make sure the coordinates are positive numbers, and that they are on the game board. If not, display an error message using a showMessageDialog and let the player try again.
    Once you have correct coordinates, you need to see if the entered values hit the opposing player's ship. If the player has made a hit, enter a -1 into the opposing player's board and then display a message dialog with a message similar to “YOU HIT HIM! Go again!” and allow the player to go again.
    If the player has missed the at that coordinate, enter a -2 into the opposing player's board which signifies a miss. Print a message stating they missed and allow the other player to go.
    You need to keep track of which ships you have sunk. Provide a helper method inside of the print method which will be appended to the string representing the board.
    private static String checkSunk(int[][] player)
    The method will check if any particular ship is sunk. For example, you can tell if a ship of size 2 is sunk if there are no 2's in the matrix.
    Winning the Game
    After each hit you must check if the player has won the game. Just as in the original game, when all ships have sunk the opposing player has won.
    Provide a boolean method which checks through the array and returns true of the number of 2's, 3's and 4's is equal to zero.
    Quitting the Game
    At any time during the game play, if you enter a -1 or press Cancel when being asked for the coordinates, the program should terminate.
    This project needs to have appropriate Javadoc comments. This will be worth 10% of each project’s grade from here on in.
    Appropriate commentary inside your program, as described in lecture, is required and will count 10%.

    Summary of Method headers
    These headers may help you out in the course of the project:
    public static void main(String args[])
    The main method.
    private static void readFile(String filename, int[][] player)
    The method to populate the array for each player.
    private static boolean allSunk(int[][] player)
    The method to check if all ships are sunk.
    private static String print(int[][] player)
    The method to print the board.
    private static String checkSunk(int[][] player)
    The method to check for a particular ship sunk on the board (this is called inside the print method).

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: battleship in java

    They seem to want a simplified battleships game.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: battleship in java

    Your other (identical) thread has bee moved to the New To Java forum; I'm closing this one.

    kind regards,

    I have the stamina of a seal; I lie on the beach instead of running on it.

Similar Threads

  1. battleship in java
    By matthewandalex5 in forum New To Java
    Replies: 2
    Last Post: 03-01-2012, 07:09 AM
  2. Java Battleship
    By lolerskates2695 in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 12:03 AM
  3. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 02:09 AM
  4. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 01:58 AM

Posting Permissions

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