Write an interactive, text-based program that allows the user to play a one-play
ID: 3744105 • Letter: W
Question
Write an interactive, text-based program that allows the user to play a one-player simplified Battleship game. This game will use a 1-dimensional array. The user will play against the computer. You can read more about the general game on Wikipedia (Links to an external site.)Links to an external site..
From the picture shown below I would like you to display this through the game of Battleship, the computer will randomly place ships somewhere in a 1-dimensional area. The user will guess positions. If the user guesses all positions of a ship, they sink that ship. If they sink all ships, they win.
Game Requirements
The ships:
The computer must create at least 3 ships.
The ships CAN NOT all be the same sizes, they must be different from the other ships sizes.
The ships must be randomly placed within the array.
The array must be a grid and show every time whether a ship is hit or missed on the board as shown in the picture below.
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 e.g “Outside of range”.
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 loses.
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.
Note: because we have not yet covered classes and objects, you are not required to create separate game/interaction classes.
Use at least one 1-dimensional array to represent the location of the ships.
Note: you may choose to use more than one array!
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.
Carefully consider the best logical breakdown for your methods.
For full credit, 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.
Notes and Suggestions
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 can be very useful to print out information during testing/programming that won't be printed in the final version of the game.
For example, the random locations of the ships (e.g., System.out.println(Arrays.toString(shipLocations));
It can be helpful to start with a small array size for testing purposes (e.g., 20) and then only increase the size once the game is working.
Final
In your output for the user, include a message when the user has sunk each additional ship. For example, instead of just saying "Hit!", you would say either "Hit!" or "Hit! You've sunk the size 3 ship!"
elcome to Battleship! The sizes of the ships are [3, 4, 5] Enter a number to fire at the ship. You only get 10 misses! ere are your shots so far (H is hit, M is miss): e 1 2 3 45 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 474849 re avay! Aln for a location betveen e and seExplanation / Answer
package Game;
import java.util.*;
// Class Battleship definition
public class Battleship
{
// Instance variable to store data
static char player[];
static int size;
static int numberOfGuesses;
static int currentGuess;
static int hitStatus;
static int ship[];
// Static method to initialize data member
public static void initialize(int si, int ng)
{
// Initializes size of the array
size = si;
// Initializes number of guesses
numberOfGuesses = ng;
currentGuess = 0;
hitStatus = 0;
player = new char[size+1];
ship = new int[3];
// Loops 3 times to initialize ship position to zero
for(int x = 0; x < 3; x++)
ship[x] = 0;
// Loops up to given array size to initialize player position to '-'
for(int x = 0; x <= size; x++)
player[x] = '-';
}// End of method
// Static method to display the board
public static void displayBoard()
{
System.out.println("Here are your shots so far (H is hit, M is miss): ");
System.out.print("0 ");
// Loops up to board size
for(int x = 1; x <= size; x++)
{
// Checks if loop counter value is divisible by 20 then display x value with new line
if(x % 20 == 0)
System.out.println(x + " ");
// Otherwise not divisible by 20 display only x value
else
System.out.print(x + " ");
}// End of for loop
System.out.println("-");
// Loops up to board size
for(int x = 1; x <= size; x++)
{
// Checks if loop counter value is divisible by 20 then display x value with new line
if(x % 20 == 0)
System.out.println(player[x] + " ");
// Otherwise not divisible by 20 display only x value
else
System.out.print(player[x] + " ");
}// End of for loop
System.out.println(" Fire away! Aim for a location between 0 and " + size);
}// End of method
// Method to generate random position for the ship
public static void generateRandomPos(int no)
{
// Random class object created
Random rand = new Random();
// Loops till number of ship
for(int x = 0; x < no; x++)
// Generate random number between 0 and size and store it in x index position of ship
ship[x] = rand.nextInt((size - 0) + 1) + 0;
}// End of method
// Method to return true if valid position given by the user otherwise returns false
public static boolean validPosition(int pos)
{
// Checks if position is greater than or equals to zero and less than or equals to size then return true
if(pos >= 0 && pos <= size)
return true;
// Otherwise return false
else
return false;
}// End of method
// Method to set the hit or miss status
public static void hitingStatus(int pos)
{
// Checks if position entered by the user is equals to the value stored at 0 index position
if(pos == ship[0])
{
// Set 'H' for Hit at pos index position of player array
player[pos] = 'H';
// Assigns -1 at 0 index position of ship array
ship[0] = -1;
// Increase the hit count by one
hitStatus++;
}// End of if condition
// Otherwise checks if position entered by the user is equals to the value stored at 1 index position
else if(pos == ship[1])
{
// Set 'H' for Hit at pos index position of player array
player[pos] = 'H';
// Assigns -1 at 1 index position of ship array
ship[1] = -1;
// Increase the hit count by one
hitStatus++;
}// End of else if condition
// Otherwise checks if position entered by the user is equals to the value stored at 1 index position
else if(pos == ship[2])
{
// Set 'H' for Hit at pos index position of player array
player[pos] = 'H';
// Assigns -1 at 2 index position of ship array
ship[2] = -1;
// Increase the hit count by one
hitStatus++;
}// End of else if condition
// Otherwise set 'M' for miss at pos index position of player array
else
player[pos] = 'M';
}// End of method
// Method to return game finish status
public static boolean gameFinishStatus()
{
boolean status = false;
// Checks if hit status is less than number of ships then set the status to false
if(hitStatus < ship.length)
status = false;
// Otherwise set the status to true
else
status = true;
// Returns the status
return status;
}// End of method
// Method to display initial message
public static void initialMessage()
{
System.out.println("Welcome to Battleship!");
System.out.println("The size of the ships are: [3, 4, 5]");
System.out.println("Enter a number to fire at the ship. You only get " + numberOfGuesses + " misses!");
}// End of method
// main method definition
public static void main(String ss[])
{
// To store user choice
char choice;
// To store the position entered by the user
int pos;
// Scanner class object created to accept data from the user
Scanner sc = new Scanner(System.in);
// Calls the method to initialize board size and number of hit
initialize(50, 5);
// Calls the method to display initial message
initialMessage();
// Calls the method to generate number of random ship position
generateRandomPos(3);
// Calls the method to display board
displayBoard();
// Loops till user choice is not 'N' or 'n'
do
{
// Accepts the position from the user
pos = sc.nextInt();
// Calls the method to validate the position
if(validPosition(pos))
{
// Increase the current change counter
currentGuess++;
// Call the method to check hitting status
hitingStatus(pos);
}// End of if condition
// Otherwise display error message
else
System.out.println("Out of range. Try again.");
// Calls the method to display board
displayBoard();
// Checks game finish status
if(gameFinishStatus())
{
// Checks if hit status is equals to number of ships
if(hitStatus == ship.length)
{
// Displays the winning status
System.out.println("Congratulation You Won The Battle");
// Accepts user choice
System.out.println("Play again? Enter y or n");
choice = sc.next().charAt(0);
// Calls the method to initialize board size and number of hit
initialize(50, 5);
// Calls the method to generate number of random ship position
generateRandomPos(3);
// Calls the method to display board
displayBoard();
// Checks if choice is 'N' or 'n' then stop the game
if(choice == 'N' || choice == 'n')
{
System.out.println("Good Buy");
break;
}// End of inner if condition
}// End of if condition for hit
}// End of if condition for game finish
// Checks if current change is equals to maximum number of chances
else if(currentGuess == numberOfGuesses)
{
// Displays the winning status
System.out.println("Sorry you loss the game.");
// Accepts user choice
System.out.println("Play again? Enter y or n");
choice = sc.next().charAt(0);
// Calls the method to initialize board size and number of hit
initialize(50, 5);
// Calls the method to generate number of random ship position
generateRandomPos(3);
// Calls the method to display board
displayBoard();
// Checks if choice is 'N' or 'n' then stop the game
if(choice == 'N' || choice == 'n')
{
System.out.println("Good Buy");
break;
}// End of inner if condition
}// End of else if condition
}while(true);// End of do - while loop
}// End of method
}// End of class
Sample Output:
Welcome to Battleship!
The size of the ships are: [3, 4, 5]
Enter a number to fire at the ship. You only get 5 misses!
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
21
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - - - - - - - - - - -
H - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
1
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
H - - - - - - - - - - - - - - - - - - -
H - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
55
Out of range. Try again.
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
H - - - - - - - - - - - - - - - - - - -
H - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
31
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
H - - - - - - - - - - - - - - - - - - -
H - - - - - - - - - H - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
Congratulation You Won The Battle
Play again? Enter y or n
y
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
11
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - M - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
22
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - M - - - - - - - - -
- M - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
55
Out of range. Try again.
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - M - - - - - - - - -
- M - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
40
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - M - - - - - - - - -
- M - - - - - - - - - - - - - - - - - M
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
23
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - M - - - - - - - - -
- M H - - - - - - - - - - - - - - - - M
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
2
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- M - - - - - - - - M - - - - - - - - -
- M H - - - - - - - - - - - - - - - - M
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
Sorry you loss the game.
Play again? Enter y or n
n
47 10 20
Here are your shots so far (H is hit, M is miss):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -
Fire away! Aim for a location between 0 and 50
Good Buy
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.