Create a JAVA Battleship game use JFrames The game will be
Create a JAVA Battleship game
- use JFrames
- The game will be played automatically by a client and sever
- 4 ships each with a different size
- game board will be 100x100 blocks
Must have the following features:
- Config File for ships size and placement (Any ASCII format is accepted) (MUST be named ConfigFile.txt)
- A LogFile of the events of the application. (Time Stamped, and updated (append) for each game played):
-Capture errors that my occur (Such as missing or configfile not found.
-Log significant events (Such as start of game, win or lose game.
- Appropriate use of try catch blocks.
EXAMPLE:
Se BattleShip! Incomming shots (SERVER)Solution
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(\"\\t1 \\t2 \\t3 \\t4 \\t5\");
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(\"\\t\"+\"~\");
}else if(board[row][column]==0){
System.out.print(\"\\t\"+\"*\");
}else if(board[row][column]==1){
System.out.print(\"\\t\"+\"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;
}
}


