Requirements: The computer must create at least 3 ships. The ships cannot all be
ID: 3742647 • Letter: R
Question
Requirements:
The computer must create at least 3 ships.
The ships cannot all be the same size.
The ships must be randomly placed within the array.
User communication:
Provide information to the user about the number of hits and misses. Provide a message about whether each guess is a hit or miss.
If the user guesses a number outside of the acceptable range, provide an error message.
Ending the game:
If the user hits all positions on all ships, the user wins.
If the user gets more than the maximum number of allowed misses, the user looses.
After the user wins or loses, ask them if they want to play again.
Programming Requirements
Place all of your code inside of one class: Battleship.java.
not required to create separate game/interaction classes.
Use at least one 1-dimensional array to represent the location of the ships.
Break your code up into logical methods.
Do not put all of your code inside of main. Write static methods and invoke those methods from main.
follow good principles of object-oriented programming, method design, and naming conventions.
Think carefully about what variables you need.
Use constants where appropriate.
Avoid duplicating code. Reuse code whenever possible.
To avoid problems with switching between reading numeric input and text input from the user, use this code to read in number input:
int number = Integer.parseInt(scan.nextLine());
For variables that you want to use across methods (for example, the array that represents the ship locations, the number of guesses, etc.), you can declare the outside of any method and make it static.
For example: private static int numberOfGuesses;
It's often a good idea to write a program like this in "layers."
For example, the random locations of the ships (e.g., System.out.println(Arrays.toString(shipLocations));
Explanation / Answer
Below is your code
import java.util.*;
public class BattleshipGame {
// variabe declarations
private static int numberOfGuesses = 5;
static int size = 20;
public static void main(String[] args) {
// initiaize the variables
int shipLocations[] = new int[size];
int x = 1;
int hit = 0;
// scanner class
Scanner sc = new Scanner(System.in);
setShip(shipLocations); // set the ship, by calling the function
// play the game for no of times
while (x == 1) {
hit = 0;
for (int i = 0; i < numberOfGuesses; i++) {
hit += getGuess(shipLocations, sc); // get the guess from user
// if all hit, then print the hit message
if (hit == 3) {
System.out.println("Hit!You've sunk the size 3 ship!");
break;
}
}
// else print miss message
if (hit < 3)
System.out.println("You've missed it!");
displayShip(shipLocations);
// get user option , to continue the game or not
System.out.println("Do you want to pay again? 'yes-1' 'No -0': ");
x = sc.nextInt();
}
}
// function to set the ship randomly
public static void setShip(int shipLocations[]) {
for (int i = 0; i < size; i++)
shipLocations[i] = 0; // first set 0 for all position, 0 means no
// ship
for (int i = 0; i < 3; i++) {
int loc = new Random().nextInt(size); // generate the ship location
// randomly
if (shipLocations[loc] == 0)
shipLocations[loc] = 1; // 1 means ship is placed
else
i--;
}
}
// display the ship position
public static void displayShip(int shipLocations[]) {
System.out.println(Arrays.toString(shipLocations));
}
// get the user guess
private static int getGuess(int[] shipLocations, Scanner sc) {
System.out.println("Enter the ship position: ");
int pos = sc.nextInt(); // ask user to enter the position by guess
// if position is hit, the display hit
if (shipLocations[pos] == 1) {
System.out.println("Hit!");
return 1;
}
// else display miss
System.out.println("Miss!");
return 0;
}
}
Output
Enter the ship position:
1
Hit!
Enter the ship position:
2
Miss!
Enter the ship position:
4
Hit!
Enter the ship position:
6
Hit!
Hit!You've sunk the size 3 ship!
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Do you want to pay again? 'yes-1' 'No -0':
1
Enter the ship position:
2
Miss!
Enter the ship position:
3
Miss!
Enter the ship position:
4
Hit!
Enter the ship position:
6
Hit!
Enter the ship position:
2
Miss!
You've missed it!
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Do you want to pay again? 'yes-1' 'No -0':
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.