Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a Java program that simulates a simplified version of the game of Battleship. The game is played in a Sea, a 1-dimensional array, and


Implement a Java program that simulates a simplified version of the game of Battleship. The game is played in a "Sea", a 1-dimensional array, and the battleship is randomly located in the sea. The user guesses locations (index values) in an attempt to find and thereby hit the battleship. When all the battleship index values are guessed (hit), the battleship is sunk, and the game is over.


Functional Requirements (how the code will work from the user perspective)

  • User starts the game by clicking "run".
  • An introduction to the game is printed.
  • A sea array (int[20]) is declared and a battleship (4 consecutive element values assigned a value of 1) is randomly located in the sea.
    • When the sea array is printed, the location of the battleship is apparent (all 1's).
    • If the game were being played competitively, the sea array would not be printed.
  • A hit array is generated with default values only (zeros).
    • The hit array should be all zeros and is printed at the beginning of the game.
  • User is prompted to guess an array index to try to hit the battleship, and the guess is evaluated.
    • User is prompted to enter an index value (0 to 19) as their guess.
    • If a guess is a hit, the word "Hit!" is printed.
    • If a guess is a miss, the word "Miss!" is printed.
    • If not all 4 ship index values have been guessed (hit) yet, processing continues, and the user is prompted to enter another index value.
  • When all 4 index values of the ship have been guessed, the game is over.
    • "Sunk!" is printed on the console.
    • The 2 arrays are printed, sea and hit.
    • A congratulatory note is printed.
    • The total number of guesses is printed.


Technical Requirements (how you must code it)

The system is based on 2 arrays - sea and hit - and 4 vars - ship1, guess, guesses, and sum.

  • System consists of the following code sections.
    1. Instantiate objects (Scanner and Random) and declare variables.
    2. Populate the sea array - zeros where the ship is not, and 1's where the ship is.
    3. Process each guess in a while loop - indicate hit or miss.
    4. End the game.
  • Instantiate objects and declare variables.
    • Instantiate Scanner object, Random object.
    • sea array - int[20]: default values of zero to begin.
    • hit array - int[20]: default values of zero to begin.
    • ship1 - a random int value representing the first index of the ship indexes.
    • guess - an int value that is assigned the index value the user inputs as a guess.
    • guesses - an int value that is the running total of the number of guesses the user has input.
    • sum - an int value that is the sum of the hit array.
  • Populate the sea array.
    • Since sea is an int array, default values are zero, thus no initializing is required.
    • Set the location of the ship in the sea array.
      • Generate a random integer using random.nextInt method with an argument of the expression, 20 minus 4.Assign the random int value to var ship1. The ship1 var is the first index value of the battleship. The sea[ship1] element should be set to zero (i.e., sea[ship1] = 1;)
      • Populate the remaining index values of the ship to 1 using a for loop for 3 iterations.This sets the other 3 index values for a total of 4 consecutive index values with element values equal to 1, representing the ship.
  • Process the guesses in a while loop.
    • Prompt for a guess (0 to 19) and read in the guess via Scanner object method, nextInt.
    • Assign entered value to variable guess.
    • Increment guesses, the running total of the number of guesses (i.e., guesses++);
    • Test if the sea[guess] value is 1.
      • If value is 1, then it is a hit.
        • Print "Hit!".
        • Update the hit array by assigning a 1 to the index element that is a hit (i.e., hit[guess] = 1;
      • If value is not 1 (i.e., else), then it is a miss.
        • Print "Miss!".
    • Sum the element values of the hit array using a for loop or enhanced for loop, and assign the value to var sum, e.g., sum = sum + hit[i];
    • Loop continues while the value of sum is less than 4.
  • End the game.
    • When summing the hit array yields sum with a value of 4, the loop test should fail and processing inside the loop should terminate.
    • Print "Sunk!".
    • Print the 2 arrays, sea and hit, so all values are shown. The two array values should be identical at the end of the game.
    • Print a congratulatory note.
    • Print the number of guesses required to sink the battleship.
  • Implement a method to print the introduction.
  • Implement a method to print arrays:

public static void prtArray(int[] array, String arrayName) {...}

  • parameter array is the name of the array to print, either sea or hit.
  • parameter arrayName is the label you will print at the beginning of the line of the array values, e.g., "Sea" or "Hit".


  • Name your source code main class (.java file) as YourName _Project4.java.


Hints and suggestions.

  • Code the system in chunks and print values to the console to test progressively as code chunks are completed.
  • Add the loop statements (use a while loop) after the logic is tested and working correctly for a single iteration.
  • Remember to indent the statements inside the while loop.
  • The loop test will be as follows: while (sum < 4), or something similar.
  • Print the sea and hit arrays to the console when they are first generated. This is a good practice for your testing, but it also will make it easier for the TA to test your code.
    • When you want to actually play the game, you can comment out the first array print statement for the sea array so the user can test their skills.
  • Remember that the guess value is the index value for the sea and hit array.
  • Be sure to test hits and misses thoroughly by jumping around in the array arbitrarily with your guesses.
  • You will need to reinitialize the variable sum to zero at the beginning of the summing process, since you will be summing the entire hit array each time.


Enhancing the game (to be done during your summer break).This is not part of Project 4!

You could enhance the game in several ways.

  1. Add an index header when printing the arrays so the user can see the 0 and 1 values for each indexed element.
  2. On the hit array, mark each missed guess with a value such as 9, so the user can easily see not only the hits, but also the misses on the hit array. This enhancement would require you to not include the 9's in your summing logic.
  3. Allow the user to adjust the size of the array.Prompt the user to input the number of elements in the sea array.
  4. Prompt the user to input the size of the ship (number of index values).
  5. Add more ships and/or different sized ships.
    1. 2 to 3 ships for a 1D array.
    2. Create different sized ships, e.g., 2, 3, 4, or 5 index value ships.
  6. Create a sea array with 2 dimensions.
    1. Each guess would be 2 int values, a row value and a column value.
    2. Ships could be placed horizontally and vertically.


Example output(from the Eclipse console)


Welcome to the great game of MiniBattleship!

Guess the location of the battleship until you have sunk it.


Sea: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0

Hit: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


Enter an index value to try to hit the battleship:12

HIT!

Enter an index value to try to hit the battleship:13

HIT!

Enter an index value to try to hit the battleship:14

MISS!

Enter an index value to try to hit the battleship:10

HIT!

Enter an index value to try to hit the battleship:11

HIT!


SUNK!


Sea: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0

Hit: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0


Congratulations on sinking the battleship!

It took you 5 guesses.


Step by Step Solution

3.43 Rating (150 Votes )

There are 3 Steps involved in it

Step: 1

To implement a simplified Java program for the Battleship game with the given functional and technic... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Starting Out With Python Global Edition

Authors: Tony Gaddis

4th Edition

1292225750, 9781292225753

More Books

Students also viewed these Programming questions