I need to make a battleship game that looks like this picture. It can be totally
ID: 3538954 • Letter: I
Question
I need to make a battleship game that looks like this picture. It can be totally different as long as it works. It needs to be 100x100. It also has to have 4 ships 4 squares long either vertical or horizontal. The battleship game is supposed to work automatically and side one VS side 2 as shown in the picture. Thanks. I have existing code so you can modify that.
________________________________________________________________________________________________
EXISTING CODE:
import java.util.*;
import java.util.Scanner;
public class BattleshipLab
{
Scanner input = new Scanner(System.in);
public static final boolean DEBUG = false;
public static void breakln()
{
System.out.println("_____________________________________");
System.out.println("");
}
public static void createBoard(String[][] board)
{
for(int r = 0; r < board.length; r++)
{
for(int c = 0; c < board[0].length; c++)
{
board[r][c] = "~";
}
}
}
public static void showBoard(String[][] board)
{
breakln();
for(int r = 0; r < board.length; r++)
{
if(DEBUG == true)
{
for(int c = 0; c < board[0].length; c++)
{
System.out.print(" "+board[r][c]);
}
System.out.println("");
}
else
{
for(int c = 0; c < board[0].length; c++)
{
if(board[r][c].equals("S"))
{
System.out.print(" "+"~");
}
else
{
System.out.print(" "+board[r][c]);
}
}
System.out.println("");
}
}
breakln();
}
public static void createShip(String[][] board,int size)
{
if(Math.random() < 0.5)
{
int col = (int)(Math.random()*5);
int row = (int)(Math.random()*7);
for(int i = 0; i<size; i++)
{
board[row][col+i] = "S";
}
}
else
{
int col = (int)(Math.random()*7);
int row = (int)(Math.random()*5);
for(int i = 0; i<size; i++)
{
board[row+i][col] = "S";
}
}
}
public static int userFire(String[][] board, int hits, int torps)
{
Scanner input = new Scanner(System.in);
int row,col;
System.out.println("You have: "+ torps +" torpedos left!");
System.out.println("Select a row to fire in: ");
row = input.nextInt();
while(row > 8 || row < 1) // Error checking for row
{
System.out.println("Enter a valid row (1 -> 8)");
row = input.nextInt();
}
System.out.println("Select a column to fire in: ");
col = input.nextInt();
while(col > 8 || col < 1) // Error checking for column
{
System.out.println("Enter a valid col (1 -> 8)");
col = input.nextInt();
}
if(board[row-1][col-1].equals("S"))
{
hits ++;
System.out.println("~~~~~~~ HIT ~~~~~~~");
board[row-1][col-1] = "!";
}
else
{
System.out.println("~~~~~~~ MISS ~~~~~~~");
board[row-1][col-1] = "M";
}
return hits;
}
public static void finall(int hits, int torps)
{
if(hits < 4)
System.out.println("Sorry, but you lost because you didn't sink the ship.");
if(torps < 1)
System.out.println("You have lost all your torpedos");
else
if(hits >= 4)
{
System.out.println("You have beaten the game battleship, Thanks for playing!");
}
System.out.println("Good game, well played!");
}
public static void main(String[] arg)
{
String[][] board = new String[8][8];
createBoard(board);
createShip(board, 4);
int torps = 15;
int hits = 0;
/// Starting real stuff
while(torps > 0 && hits < 4)
{
showBoard(board);
hits = userFire(board, hits, torps);
torps --;
}
finall(hits, torps);
}
_________________________________________________________________________________________________
Explanation / Answer
import java.util.Random; import java.util.Scanner; public class battleShip { public static void main(String[] args) { int[][] board = new int[5][5]; int[][] ships = new int[3][2]; int[] shoot = new int[2]; 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 < 5 ; row++ ) for(int column=0 ; column < 5 ; 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 < 5 ; row++ ){ System.out.print((row+1)+""); for(int column=0 ; column < 5 ; 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 < 3 ; ship++){ ships[ship][0]=random.nextInt(5); ships[ship][1]=random.nextInt(5); //let's check if that shot was already tried //if it was, just finish the do...while when a new pair was randomly selected for(int last=0 ; lastRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.