Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Battleship java

mles
mles 


You will build an application that allows a person to play Battleship against the computer. I will provide you a Driver and a class to represent spots on the board. You must use what I provide and build your own class to represent the game. Your application will create the board, place ships randomly, ask the user for spots they want to shoot at, report/update the result, and repeat until the game is over (all ships are hit in each spot they occupy). You must also be able to report the number of guesses it took the user to finish the game. There will also be limited error checking as described below. ""This is the diver and spot class they must not change !"" -------------------------------------- public class Driver { public static void main (String [] args) { Battleship game = new Battleship(5, 7); //play on a board with 5 rows and 7 columns System.out.println("Battleship!"); System.out.println("-----------\n"); game.setBoard(); //get board ready to play System.out.println("Revealed board (for verification purposes):\n"); game.printBoard(true); //print contents of all spots while (!game.over()) { game.printBoard(false); //only print visited spots game.makeGuess(); //prompt user for guess and process it } game.printStatistics(); //print number of guesses it took to win game System.out.println("Goodbye. Thanks for playing."); } } -------------------------------------------------- /** * will represents one spots in the Battleship board. */ public class Spot { private boolean visited; //has the user guessed this spot? private int ship; //ship type in spot (5 - Carrier, 4 - Battleship, ..., 0 - Vacant) public boolean getVisited() { return visited; } public void setVisited(boolean val) { visited = val; } public int getShip() { return ship; } public void setShip(int val) { ship = val; } } --------------------------------------------- Here are some specifications and assumptions for you: The board size can change in the Constructor call. Just because the current Driver makes a 5x7 board does not give you permission to assume it will always be that size. You can assume the board will always be big enough to put the ships in randomly (5x4 or 4x5). The numbering of rows and columns in the printed output must start at 1, not 0. The user will use those values to reference spots in the board. (see output) There are 4 ships that need to be placed on the board. The Carrier occupies 5 spots, the Battleship 4, Submarine 3, and Patrol Boat 2. The ships will be placed randomly (random location, random vertical/horizontal orientation). This ships will not be placed diagonally and must not overlap. he following errors must be caught: sample The user cannot input a row or column value that does not exist. The user cannot chose a spot that they already selected. The user cannot input anything other than an int for row and column values. The Error massage. -------------------------------------- Battleship! ----------- Revealed board (for verification purposes): +-+-+-+-+-+-+-+ 1 |0|0|0|2|0|0|0| +-+-+-+-+-+-+-+ 2 |0|0|0|2|0|0|0| +-+-+-+-+-+-+-+ 3 |3|3|3|0|0|0|0| +-+-+-+-+-+-+-+ 4 |5|5|5|5|5|0|0| +-+-+-+-+-+-+-+ 5 |0|0|4|4|4|4|0| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 +-+-+-+-+-+-+-+ 1 | | | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 1 Miss! There was nothing at that location +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 1 You already guessed that location! +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 1 You already guessed that location! +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 8 Error: Invalid column. +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 6 Enter a column to guess > 1 Error: Invalid row. +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 6 Enter a column to guess > 8 Error: Invalid row. +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 1 You already guessed that location! +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > a Error. You did not enter an integer value. Please try again. +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > dog Error. You did not enter an integer value. Please try again. +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 1 You already guessed that location! +-+-+-+-+-+-+-+ 1 |0| | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 2 Miss! There was nothing at that location +-+-+-+-+-+-+-+ 1 |0|0| | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > ------------------------------ The output style and user interaction must match the sample output below. Battleship! ----------- Revealed board (for verification purposes): +-+-+-+-+-+-+-+ 1 |0|0|0|3|3|3|5| +-+-+-+-+-+-+-+ 2 |0|0|0|0|0|2|5| +-+-+-+-+-+-+-+ 3 |0|4|4|4|4|2|5| +-+-+-+-+-+-+-+ 4 |0|0|0|0|0|0|5| +-+-+-+-+-+-+-+ 5 |0|0|0|0|0|0|5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 +-+-+-+-+-+-+-+ 1 | | | | | | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 4 Hit! You just hit the Submarine +-+-+-+-+-+-+-+ 1 | | | |3| | | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 5 Hit! You just hit the Submarine +-+-+-+-+-+-+-+ 1 | | | |3|3| | | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 6 Hit! You just hit the Submarine +-+-+-+-+-+-+-+ 1 | | | |3|3|3| | +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 1 Enter a column to guess > 7 Hit! You just hit the Carrier +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | | | +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 2 Enter a column to guess > 7 Hit! You just hit the Carrier +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | | | | | | | | +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 7 Hit! You just hit the Carrier +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | | | | | | |5| +-+-+-+-+-+-+-+ 4 | | | | | | | | +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 4 Enter a column to guess > 7 Hit! You just hit the Carrier +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | | | | | | |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | | | +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 5 Enter a column to guess > 7 Hit! You just hit the Carrier +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | | | | | | |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 2 Hit! You just hit the Battleship +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4| | | | |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 3 Hit! You just hit the Battleship +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4|4| | | |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 4 Hit! You just hit the Battleship +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4|4|4| | |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 5 Hit! You just hit the Battleship +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4|4|4|4| |5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 3 Enter a column to guess > 6 Hit! You just hit the Patrol Boat +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4|4|4|4|2|5| +-+-+-+-+-+-+-+ 4 | | | | | | |5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 4 Enter a column to guess > 6 Miss! There was nothing at that location +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | | |5| +-+-+-+-+-+-+-+ 3 | |4|4|4|4|2|5| +-+-+-+-+-+-+-+ 4 | | | | | |0|5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Enter a row to guess > 2 Enter a column to guess > 6 Hit! You just hit the Patrol Boat +-+-+-+-+-+-+-+ 1 | | | |3|3|3|5| +-+-+-+-+-+-+-+ 2 | | | | | |2|5| +-+-+-+-+-+-+-+ 3 | |4|4|4|4|2|5| +-+-+-+-+-+-+-+ 4 | | | | | |0|5| +-+-+-+-+-+-+-+ 5 | | | | | | |5| +-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 Congratulations! You sunk the enemy fleet using 15 guesses. Game over. Goodbye. Thanks for playing. ..................................... Attachments Tags Clarifications

Answers

Battleship Solution

mariobeka
mariobeka 



Below you will find the implementation of the battleship gave, which beholds all the requirements mentioned in your question. Also you'll find a screen shot of the program running. Attachments
Battleship.java (12 K) Download Preview
the ship size can be set, False otherwise. */ private boolean canBeSet(int size, int row, int col, int direction) { boolean isSpace = true; switch (direction) { case 0: // North if (row - (size - 1) < 0) isSpace = false; else for (int i = row; i >=

Driver.java (1 K) Download Preview
and 7 columns System.out.println("Battleship!"); System.out.println("-----------\n"); game.setBoard(); //get board ready to play System.out.println("Revealed board (for verification purposes):\n"); game.printBoard(true); //print contents of all spots while

Spot.java (1 K) Download Preview
return visited; } public void setVisited(boolean val) { visited = val; } public int getShip() { return ship; }

Screen Shot 2015-11-25 at 15.44.05.png (98 K) Download Preview

Screenshots
Screenshot
Purchase Answer