Menu

Instructions Assignment Create Simple Class Playing Game Battleship Battleship Played Squa Q43822982

Instructions

In this assignment, you will create a simple class for playingthe game battleship. Battleship is played on a square grid of 10rows and 10 columns, which you will represent using a 2-d array ina class named Board. A separate class, Battleship, will allow youto try playing your game and see whether the methods work asplanned.

To play battleship, 2 players secretly add ships of variouslengths to their individual boards. A ship may be placedhorizontally or vertically on the board. In your program, you willrepresent blank squares on the board using the character ‘-’, andsquares where ships have been added with the character ‘b’. Forexample, the board below contains 3 boats, 2 of length 5 and one oflength 3:

– – – – – – – m – – – x x x – – m x – – – – – – – – – x – – – – – m – – – b – – – – – – – – – b – – – m – – – – – b – – – – – – m – – – – – – – b b b b b – m – – – – – – – – – – – – – – – – m – – – –

Players then take turns to try and shoot the ships on eachother’s boards by choosing a row and column reference for eachattempted shot. When a player chooses a square containing a ship,that square is marked as a “hit”: in your program, this will bedone using the character ‘x’. When the player “misses” by choosinga blank square this will be marked with the character ‘m’ in yourprogram. The game is over when one player has shot every squarecontaining part of a ship on their opponent’s board. The boardbelow is from a game in progress. One ship has already beencompletely destroyed and 2 squares of one of the other ships havebeen shot.

– – – – – – – m – – – x x x – – m x – – – – – – – – – x – – – – – m – – – b – – – – – – – – – b – – – m – – – – – b – – – – – – m – – – – – – – b b b b b – m – – – – – – – – – – – – – – – – m – – – –

The specification for the Board class is as follows:

Variables

char[][] squares – An array which represents the board on whicha player places their battleship and records shots.

Constructors

Board() – This constructor initializes the squares array withevery value set to ‘-’, which is the character used to represent ablank square.

Methods

  • public String toString() – Returns amulti-line representation of the board by printing each characterin squares with spaces after each character and a new line for eachrow.
  • public boolean addShip(int row, int col, int len,boolean horizontal) – Attempts to add a ship of length lento the grid, starting at the row and column specified andproceeding either rightwards (if horizontal is true), or downwards(if vertical is true). If the ship can be placed in the placespecified, each square making up the ship should be set to ‘b’, andthe method should return true. A ship may not be placed if it wouldgo off the grid, or would intersect another ship on the grid. Ifthe ship cannot be placed, no values in squares should be changedand the method should return false.
  • public int shoot(int row, int col) – If rowand col specify a square which is out of bounds, the method shouldreturn -1. If the square at the specified row and column contains‘-’ (i.e. is blank), the square should be changed to ‘m’ to signifya miss, and the method should return 0. If the square contains ‘b’(i.e. a battleship which hasn’t been hit yet) this square should bechanged to an ‘x’ to signify a hit, and the method should return 1.If the square contains either ‘x’ or ‘m’ the method should return 2(these are squares which have already been “shot”).
  • public boolean gameOver() – If the character‘b’ does not appear at any location in squares, then there are nounsunk ships remaining on the board, so return true to indicatethat the game is over. Otherwise return false.
  • public boolean foundShip(int len) – Search theboard for any possible ships of length len. If there are exactlylen consecutive squares (either horizontal or vertical) containinga ‘b’ character somewhere in the grid, then return true, otherwise,return false.

When you have created your Board class, download theBattleship.java (Links to an external site.) class to the samefolder. When you run the main method in this class, you will beprompted to add ships to the board, then to attempt to shoot these.See the sample run below for an example.

Sample Run

Welcome to Battleship!- – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Add a new ship? (y/n) nYou need a ship of length 3 to play!Add a new ship? (y/n) yStarting in which row? 2Starting in which column? 3How long? 3Horizontal (h) or vertical (v)? hNew ship added!- – – – – – – – – – – – – – – – – – – – – – – b b b – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Add a new ship? (y/n) nOk, let’s play!Input row 5Input column 6Miss!- – – – – – – – – – – – – – – – – – – – – – – b b b – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – m – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Input row 2Input column 3Hit!- – – – – – – – – – – – – – – – – – – – – – – x b b – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – m – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Input row 2Input column 4Hit!- – – – – – – – – – – – – – – – – – – – – – – x x b – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – m – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Input row 2Input column 5Hit!- – – – – – – – – – – – – – – – – – – – – – – x x x – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – m – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – Game over!

You should run the game several times and try different inputs -this uses the methods from your Board class, so these will all needto produce the correct results and outputs if the program is towork as desired. You can also add, edit, or isolate code to helpyou with testing specific features.

When you are ready, paste your entire Board.java class fromstart to finish in the box below, click run to test the output, andclick submit when you are satisfied with your results.

Milestones

As you work on this assignment, you can use the milestones belowto inform your development process:

Milestone 1: Write the Board class header,declare the squares variable and write the single constructorrequired.

Milestone 2: Write and test the toString andgameOver methods.

Milestone 3: Write and test the addShipmethod.

Milestone 4: Write and test the shoot method.Run the full Battleship program to test all the features.

Battleship file:

/* Term 2 Assignment 7 – Battleship *//* Main Battleship class – * requires Board class to be written */import java.util.Scanner;public class Battleship{ public static void main(String[] args) { // Set up board and print welcome Board b = new Board(); Scanner scan = new Scanner(System.in); System.out.println(“Welcome to Battleship!n”); System.out.println(b + “n”); boolean addNew = true; while(addNew) { // Ask for new ship System.out.println(“Add a new ship? (y/n)”); String ans = scan.nextLine(); if(ans.toLowerCase().equals(“y”)) { // Get parameters for new ship System.out.println(“Starting in which row?”); int r = scan.nextInt(); System.out.println(“Starting in which column?”); int c = scan.nextInt(); System.out.println(“How long?”); int l = scan.nextInt(); scan.nextLine(); System.out.println(“Horizontal (h) or vertical (v)?”); String d = scan.nextLine(); boolean h = (d.toLowerCase().equals(“h”)); // Call addShip method and return message based on true/false value if(b.addShip(r, c, l, h)) { System.out.println(“nNew ship added!nn” + b + “n”); } else System.out.println(“nCan’t put a ship there!n”); } else { addNew = false; System.out.println(“nOk, let’s play!n”); } } // As long as ships remain, play game while(!b.gameOver()) { // Get row and column to shoot System.out.println(“Input row”); int r = scan.nextInt(); System.out.println(“Input column”); int c = scan.nextInt(); // Perform shot and store result int result = b.shoot(r,c); // Choose message based on result if(result == 1) System.out.println(“nHit!nn” + b + “n”); else if(result == 0) System.out.println(“nMiss!nn” + b + “n”); else if(result == 2) System.out.println(“nYou already tried thatn”); else System.out.println(“nInvalid coordinatesn”); } System.out.println(“Game over!”); }}

Expert Answer


Answer to Instructions In this assignment, you will create a simple class for playing the game battleship. Battleship is played on…

OR