In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x
ID: 3852415 • Letter: I
Question
In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x n grid and m ships. Each ship takes up exactly three cells. In the game, it’s you against the computer, but unlike the real battleship game, you don’t place any ships on your own. Instead, your job is to sink the computer’s ship in the fewest number of guess.
Goal:Sink all the computer’s ships(0<m <6in this case)in the fewest number ofguesses. You’re given a rating or level, based on how well you perform.At thebeginning of the game you can ask the user to enter how many (m) ships they wantto place on the board.The number of ships can be placedis between 1 and 5
Setup:When the game program is launched, user is again asked to enter whatwould be the size of the grid, that is the value ofn. The value ofnmust bebetween 3and10. Then, the computer placesmships on an x ngrid (for an example, see thefigure below).When that’scomplete the game asks for your first guess.
How to play:We haven’t learned to build GUI (Graphical User Interface) yet, so thisversion works at the command-line. The computer will prompt you to enter a guess(a cell), that you willtype at the command-line as “1 3”(where “1 3” means 3rd cell of the 1strow), “45”, etc.. In response to your guess, you will see a result at thecommand line, either “Hit”, “Miss”, “Kill” (or whatever the lucky battleship of the dayis!). When you have sunk all m battleships, the game ends by printing out your rating.
Here is a sample run of the game:
Welcome to the Battle Ships game!
Please enter the size of the grid: 8
Please enter number of ships: 3
Your goal is to sink 3 ships.
Try to sink them all in the fewest number of guesses
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 2 1 hit
Enter a guess [row(1 to 8)] [col (1 to 8)]: 2 2 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 1 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 7 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 3 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 5 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 5 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 61 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 5 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 6 kill
All ships are sank!
It only took you18 guesses.
You got out before your options sank.
When player can finish the game within 20 guesses,print the following message (where xx is the number of guesses):
All ships are sank! It only took you XX guesses.
You got out before your options sank.
If it takes more than 20 guesses, print the following message All ships are sank! Took you long enough.XX guesses.Fish are dancing with your options.
Your program should term
inate after 25 guesses and
reveal the positions of the
ships.
NOTE: While designing the classes for this lab, you must follow OOP design concepts (Abstraction,Modularity,Inheritance and API design)
i need a UML diagram for the program
.and i want this program to run with java application
Explanation / Answer
Answer:
import java.util.*;
public class BattleShipGame {
public static void main(String[] args) {
int[][] board = new int[10][10];
int[][] ships = new int[10][10];
int[] shoot = new int[10];
int attempts=0,
shotHit=0;
initBoard(board);
initShips(ships);
System.out.println();
do{
showBoard(board);
shoot(shoot);
attempts++;
if(hit(shoot,ships)){
hint(shoot,ships,attempts);
shotHit++;
}
else
hint(shoot,ships,attempts);
changeboard(shoot,ships,board);
}while(shotHit!=3);
System.out.println(" Battleship Java game finished! You hit 3 ships in "+attempts+" attempts");
showBoard(board);
}
public static void initBoard(int[][] board){
for(int row=0 ; row < 10 ; row++ )
for(int column=0 ; column < 10 ; column++ )
board[row][column]=-1;
}
public static void showBoard(int[][] board){
System.out.println(" 1 2 3 4 5");
System.out.println();
for(int row=0 ; row < 10 ; row++ ){
System.out.print((row+1)+"");
for(int column=0 ; column < 10 ; column++ ){
if(board[row][column]==-1){
System.out.print(" "+"~");
}else if(board[row][column]==0){
System.out.print(" "+"*");
}else if(board[row][column]==1){
System.out.print(" "+"X");
}
}
System.out.println();
}
}
public static void initShips(int[][] ships){
Random random = new Random();
for(int ship=0 ; ship < 10; ship++){
ships[ship][0]=random.nextInt(10);
ships[ship][1]=random.nextInt(10);
for(int last=0 ; last < ship ; last++){
if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
do{
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
}while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
}
}
}
public static void shoot(int[] shoot){
Scanner input = new Scanner(System.in);
System.out.print("Row: ");
shoot[0] = input.nextInt();
shoot[0]--;
System.out.print("Column: ");
shoot[1] = input.nextInt();
shoot[1]--;
}
public static boolean hit(int[] shoot, int[][] ships){
for(int ship=0 ; ship<ships.length ; ship++){
if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
System.out.printf("You hit a ship located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
return true;
}
}
return false;
}
public static void hint(int[] shoot, int[][] ships, int attempt){
int row=0,
column=0;
for(int line=0 ; line < ships.length ; line++){
if(ships[line][0]==shoot[0])
row++;
if(ships[line][1]==shoot[1])
column++;
}
System.out.printf(" Hint %d: Row %d -> %d ships " +
"Column %d -> %d ships ",attempt,shoot[0]+1,row,shoot[1]+1,column);
}
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
if(hit(shoot,ships))
board[shoot[0]][shoot[1]]=1;
else
board[shoot[0]][shoot[1]]=0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.