Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.applet.*;
- import java.net.*;
- import java.io.*;
- public class battleShip{
- private static String[][] fakeCompGrid = {
- {"║ ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "║ Name of Ship ║", " Status ║"},
- {"║A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Aircraft Carrier ║", " Alive ║"},
- {"║C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Battleship ║", " Alive ║"},
- {"║E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Destroyer ║", " Alive ║"},
- {"║G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Submarine ║", " Alive ║"},
- {"║I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Patrol Boat ║", " Alive ║"} };
- private static String[][] realCompGrid = {
- {"###", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "# Name of Ship |", " Status #"},
- {"#A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
- {"#B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Aircraft Carrier |", " Alive #"},
- {"#C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
- {"#D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Battleship |", " Alive #"},
- {"#E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
- {"#F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Destroyer |", " Alive #"},
- {"#G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
- {"#H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Submarine |", " Alive #"},
- {"#I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
- {"#J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Patrol Boat |", " Alive #"} };
- private static String[][] humanGrid = {
- {"║ ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "║ Name of Ship ║", " Status ║"},
- {"║A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Aircraft Carrier ║", " Alive ║"},
- {"║C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Battleship ║", " Alive ║"},
- {"║E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Destroyer ║", " Alive ║"},
- {"║G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Submarine ║", " Alive ║"},
- {"║I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
- {"║J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Patrol Boat ║", " Alive ║"} };
- private static final int GAME_OVER_LIMIT = 17;
- private static final int HUMAN = 1;
- private static int currentPlayer = 1;
- private static final KeyboardReader INPUT;
- private static int humanTotalHitsTaken;
- private static int compTotalHitsTaken;
- private static String name;
- private static int row, column;
- private static ArrayList<String> coordsY;
- private static ArrayList<Integer> coordsX;
- private static ArrayList<String> coordsStringX;
- private static AudioClip explosion, splash;
- private static int airShipComputer = 5;
- private static int battleshipComputer = 4;
- private static int destroyerComputer = 3;
- private static int submarineComputer = 3;
- private static int patrolBoatComputer = 2;
- private static int airshipHuman = 5;
- private static int battleshipHuman = 4;
- private static int destroyerHuman = 3;
- private static int submarineHuman = 3;
- private static int patrolBoatHuman = 2;
- private static String logComputer = "";
- private static String logHuman = "";
- static{
- INPUT = new KeyboardReader();
- coordsY = new ArrayList<String>();
- coordsY.add("A");
- coordsY.add("B");
- coordsY.add("C");
- coordsY.add("D");
- coordsY.add("E");
- coordsY.add("F");
- coordsY.add("G");
- coordsY.add("H");
- coordsY.add("I");
- coordsY.add("J");
- coordsStringX = new ArrayList<String>();
- coordsStringX.add("1");
- coordsStringX.add("2");
- coordsStringX.add("3");
- coordsStringX.add("4");
- coordsStringX.add("5");
- coordsStringX.add("6");
- coordsStringX.add("7");
- coordsStringX.add("8");
- coordsStringX.add("9");
- coordsStringX.add("10");
- for (int i = 1; i < 10; i++){
- coordsX = new ArrayList<Integer>();
- coordsX.add(i);
- }
- explosion = null;
- splash = null;
- try
- {
- explosion = Applet.newAudioClip(new URL("file:" + new File(".").getCanonicalPath()
- + "/" + "explosion.wav"));
- splash = Applet.newAudioClip(new URL("file:" + new File(".").getCanonicalPath() +
- "/" + "splash.wav"));
- } catch (Exception e)
- {
- System.out.println(e.toString());
- }
- }
- public static void main (String [] args) {
- welcomeBanner();
- initializeGame();
- while (humanTotalHitsTaken < GAME_OVER_LIMIT && compTotalHitsTaken < GAME_OVER_LIMIT)
- {
- displayGame();
- if (currentPlayer == HUMAN)
- humanPlayerTakesTurn();
- else
- computerPlayerTakesTurn();
- }
- congratulateWinner();
- }
- public static void welcomeBanner(){
- System.out.println("██████╗ █████╗ ████████╗████████╗██╗ ███████╗███████╗██╗ ██╗██╗██████╗ ");
- System.out.println("██╔══██╗██╔══██╗╚══██╔══╝╚══██╔══╝██║ ██╔════╝██╔════╝██║ ██║██║██╔══██╗");
- System.out.println("██████╔╝███████║ ██║ ██║ ██║ █████╗ ███████╗███████║██║██████╔╝");
- System.out.println("██╔══██╗██╔══██║ ██║ ██║ ██║ ██╔══╝ ╚════██║██╔══██║██║██╔═══╝ ");
- System.out.println("██████╔╝██║ ██║ ██║ ██║ ███████╗███████╗███████║██║ ██║██║██║ ");
- System.out.println("╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ");
- System.out.println(" |__");
- System.out.println(" |\\/");
- System.out.println(" ---");
- System.out.println(" / | [");
- System.out.println(" ! | |||");
- System.out.println(" _/| _/|-++'");
- System.out.println(" + +--| |--|--|_ |-");
- System.out.println(" { /|__| |/\\__| |--- |||__/");
- System.out.println(" +---------------___[]-_===_.'____ /\\");
- System.out.println(" ____`-' ||___-{]_| _[}- | |_[___\\==-- \\/ _");
- System.out.println(" __..._____-===/___]_|__|_____________________________[___\\==--____,------' .7");
- System.out.println("|═*═*═*═*═*═╗ BB-61/");
- System.out.println(" \\═════════╝_______________________________________________________________|");
- }
- public static void initializeGame(){
- placeShips(humanGrid);
- placeShips(realCompGrid);
- int choiceValue = 1;
- int flip = (int) (Math.random()*2)+1;
- for (name = INPUT.readString("\nEnter your name: "); name.length() > 49 || name.length() == 0;){
- name = INPUT.readString("Invalid. Re-enter your name:");
- }
- System.out.println("We will be flipping a coin to see who goes first!");
- while(true){
- String choice = INPUT.readString("Select 'Heads' or 'Tails' (non-case sensitive): ");
- choice.trim();
- if (choice.equalsIgnoreCase("heads")){
- choiceValue = 1;
- System.out.println("You chose heads!");
- break;
- }
- else if (choice.equalsIgnoreCase("tails")){
- choiceValue = 2;
- System.out.println("You chose tails!");
- break;
- }
- else
- System.out.println("That's not a valid input -_-");
- }
- if (choiceValue == flip){
- System.out.println("You won the coinflip!");
- currentPlayer = 1;
- }
- else{
- System.out.println("Yeah you didn't win the coinflip, not that you care.");
- currentPlayer = 2;
- }
- }
- public static void displayGame(){
- System.out.println("\f");
- System.out.println("╔═══════════════════════════════════════════════════╗");
- System.out.println("║ Computer.EXE ║");
- System.out.println("╚═══════════════════════════════════════════════════╝");
- System.out.println("╔═══════════════════════╦══════════════════╦════════╗");
- for (int i = 0; i <= 10; i++){
- for (int z = 0; z <= 12; z++)
- System.out.print(fakeCompGrid[i][z]);
- System.out.println();
- }
- System.out.println("╚═══════════════════════╩══════════════════╩════════╝");
- System.out.println("╔═══════════════════════════════════════════════════╗");
- int spaces = 49 - name.length();
- System.out.print("║");
- for (int i = 0; i < (spaces/2); i++)
- System.out.print(" ");
- System.out.print(name);
- for (int k = 0; k < (spaces/2); k++)
- System.out.print(" ");
- if (spaces%2 == 1)
- System.out.print(" ");
- System.out.print(" ║");
- System.out.print("\n");
- System.out.println("╚═══════════════════════════════════════════════════╝");
- System.out.println("╔═══════════════════════╦══════════════════╦════════╗");
- for (int i = 0; i <= 10; i++){
- for (int z = 0; z <= 12; z++)
- System.out.print(humanGrid[i][z]);
- System.out.println();
- }
- System.out.println("╚═══════════════════════╩══════════════════╩════════╝");
- }
- public static void humanPlayerTakesTurn(){
- int repeatCount = 0;
- System.out.println("╔═════════╗");
- System.out.println("║Computer:║ "+logComputer);
- System.out.println("╚═════════╝");
- System.out.print("╔");
- for (int i = 0; i <= name.length();i++)
- System.out.print("═");
- System.out.print("╗");
- System.out.println("\n║"+name+":║ "+logHuman);
- System.out.print("╚");
- for (int i = 0; i <= name.length();i++)
- System.out.print("═");
- System.out.print("╝\n");
- while(true){
- boolean validInput = false;
- String coordY;
- int coordX = 0;
- int valueY = 0;
- if (repeatCount >= 1){
- System.out.println("Invalid Response");
- }
- repeatCount++;
- String input = INPUT.readString(name+"!Enter a coordinate (like B,4):");
- if (input.length() < 5 && input.length() > 2){
- coordY = input.substring(0, 1).toUpperCase();
- if (input.length() == 4){
- if (coordsStringX.indexOf(input.substring(2,3)) >=0 && coordsStringX.indexOf(input.substring(2,4)) <= 10){
- coordX = coordsStringX.indexOf(input.substring(2))+1;
- validInput = true;
- }
- }
- if (input.length() == 3){
- if (coordsStringX.indexOf(input.substring(2)) >=0)
- coordX = coordsStringX.indexOf(input.substring(2))+1;
- validInput = true;
- }
- if (coordsY.indexOf(coordY) >=0){
- valueY = coordsY.indexOf(coordY) + 1;
- validInput = true;
- }
- else{
- validInput = false;
- }
- if (validInput = true){
- if (realCompGrid[valueY][coordX].equals(" *")){
- System.out.println("You already attacked this ship!");
- repeatCount--;
- }
- else if (realCompGrid[valueY][coordX].equals(" X")){
- System.out.println("You have already attacked and missed this spot!");
- repeatCount--;
- }
- else if (realCompGrid[valueY][coordX].equalsIgnoreCase(" ")){
- fakeCompGrid[valueY][coordX] = " X";
- realCompGrid[valueY][coordX] = " X";
- logHuman ="You have missed!";
- splash.play();
- currentPlayer ++;
- INPUT.pause();
- break;
- }
- else if (!realCompGrid[valueY][coordX].equals(" ") && !realCompGrid[valueY][coordX].equals(" X") && !realCompGrid[valueY][coordX].equals(" *") && coordX != 0 && valueY != 0){
- if (realCompGrid[valueY][coordX].equals(" A")){
- logHuman = "You hit a ship!";
- airShipComputer --;
- if (airShipComputer == 0){
- fakeCompGrid[2][12] = " Dead ║";
- realCompGrid[2][12] = " Dead ║";
- logHuman = "You destroyed the enemy aircraft carrier!";
- }
- }
- else if (realCompGrid[valueY][coordX].equals(" B")){
- logHuman = "You hit a ship!";
- battleshipComputer --;
- if (battleshipComputer == 0){
- fakeCompGrid[4][12] = " Dead ║";
- realCompGrid[4][12] = " Dead ║";
- logHuman = "You destroyed the enemy battleship!";
- }
- }
- else if (realCompGrid[valueY][coordX].equals(" D")){
- destroyerComputer --;
- logHuman = "You hit a ship!";
- if (destroyerComputer == 0){
- fakeCompGrid[6][12] = " Dead ║";
- fakeCompGrid[6][12] = " Dead ║";
- logHuman = "You destroyed the enemy destroyer!";
- }
- }
- else if (realCompGrid[valueY][coordX].equals(" S")){
- submarineComputer--;
- logHuman = "You hit a ship!";
- if (submarineComputer == 0){
- fakeCompGrid[8][12] = " Dead ║";
- fakeCompGrid[8][12] = " Dead ║";
- logHuman = "You destroyed the enemy submarine!";
- }
- }
- else if (realCompGrid[valueY][coordX].equals(" P")){
- patrolBoatComputer--;
- logHuman = "You hit a ship!";
- if (patrolBoatComputer == 0){
- fakeCompGrid[10][12] = " Dead ║";
- fakeCompGrid[10][12] = " Dead ║";
- logHuman = "You destroyed the enemy patrol boat!";
- }
- }
- explosion.play();
- fakeCompGrid[valueY][coordX] = " *";
- realCompGrid[valueY][coordX] = " *";
- compTotalHitsTaken ++;
- currentPlayer ++;
- INPUT.pause();
- break;
- }
- }
- }
- }
- }
- public static void computerPlayerTakesTurn(){
- int compCoordX = (int) (Math.random()*10)+1;
- int compCoordY = (int) (Math.random()*10)+1;
- while (true){
- compCoordX = (int) (Math.random()*10)+1;
- compCoordY = (int) (Math.random()*10)+1;
- if (humanGrid[compCoordY][compCoordX].equals(" ")){
- currentPlayer--;
- humanGrid[compCoordY][compCoordX] = " X";
- logComputer = "Computer.EXE has missed";
- break;
- }
- else if (!humanGrid[compCoordY][compCoordX].equals(" ") && !humanGrid[compCoordY][compCoordX].equals(" X") && !humanGrid[compCoordY][compCoordX].equals(" *")){
- if (humanGrid[compCoordY][compCoordX].equals(" A")){
- logComputer = "Computer.EXE hit an aircraft carrier!";
- airshipHuman --;
- if (airshipHuman == 0){
- logComputer = "Computer.EXE has destroyed your aircraft.";
- humanGrid[2][12] = " dead #";
- }
- }
- else if (humanGrid[compCoordY][compCoordX].equals(" B")){
- logComputer = "Computer.EXE hit a battleship!";
- battleshipHuman --;
- if (airshipHuman == 0){
- logComputer = "Computer.EXE has destroyed your battleship.";
- humanGrid[4][12] = " dead #";
- }
- }
- else if (humanGrid[compCoordY][compCoordX].equals(" D")){
- logComputer = "Computer.EXE hit a destroyer!";
- destroyerHuman --;
- if (airshipHuman == 0){
- logComputer = "Computer.EXE has destroyed your destroyer.";
- humanGrid[6][12] = " dead #";
- }
- }
- else if (humanGrid[compCoordY][compCoordX].equals(" S")){
- logComputer = "Computer.EXE hit a submarine!";
- submarineHuman --;
- if (airshipHuman == 0){
- logComputer = "Computer.EXE has destroyed your submarine.";
- humanGrid[8][12] = " dead #";
- }
- }
- else if (humanGrid[compCoordY][compCoordX].equals(" P")){
- logComputer = "Computer.EXE hit a patrol boat!";
- patrolBoatHuman --;
- if (airshipHuman == 0){
- logComputer = "Computer.EXE has destroyed your patrol boat.";
- humanGrid[10][12] = " dead #";
- }
- }
- humanGrid[compCoordY][compCoordX] = " *";
- humanTotalHitsTaken ++;
- currentPlayer --;
- break;
- }
- else if (humanGrid[compCoordY][compCoordX].equals(" *")){
- }
- }
- }
- public static void congratulateWinner(){
- System.out.println("╔═╗╔═╗╔╗╔╔═╗╦═╗╔═╗╔╦╗╦ ╦╦ ╔═╗╔╦╗╦╔═╗╔╗╔╔═╗║");
- System.out.println("║ ║ ║║║║║ ╦╠╦╝╠═╣ ║ ║ ║║ ╠═╣ ║ ║║ ║║║║╚═╗║");
- System.out.println("╚═╝╚═╝╝╚╝╚═╝╩╚═╩ ╩ ╩ ╚═╝╩═╝╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝o");
- if (humanTotalHitsTaken > compTotalHitsTaken){
- System.out.println("Computer.EXE! You are the winner!");
- }
- else if (humanTotalHitsTaken < compTotalHitsTaken){
- System.out.println(name+"! You are the winner!");
- }
- }
- private static void placeShips (String[][] grid){
- int orientation = (int) (Math.random()*2)+1;
- int horizontal = 2;
- int vertical = 1;
- int occupiedSpace = 0;
- int size = 5;
- if (orientation == vertical && size == 5){
- row = (int) (Math.random()*5)+1;
- column = (int) (Math.random()*10)+1;
- for (int i = 0; i < size; i++)
- grid[row+i][column] = " A";
- size --;
- }
- else if (orientation == horizontal && size == 5){
- row = (int) (Math.random()*10)+1;
- column = (int) (Math.random()*5)+1;
- for (int i = 0; i < size; i++)
- grid[row][column+i] = " A";
- size --;
- }
- orientation = (int) (Math.random()*2)+1;
- if (orientation == vertical && size == 4){
- while (size == 4){
- occupiedSpace *= 0;
- row = (int) (Math.random()*6)+1;
- column = (int) (Math.random()*10)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row+i][column];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int k = 0; k < size; k++)
- grid[row+k][column] = " B";
- size --;
- }
- }
- }
- else if (orientation == horizontal && size == 4){
- while (size == 4){
- occupiedSpace *= 0;
- row = (int) (Math.random()*10)+1;
- column = (int) (Math.random()*6)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row][column+i];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int z = 0; z < size; z++)
- grid[row][column+z] = " B";
- size --;
- }
- }
- }
- orientation = (int) (Math.random()*2)+1;
- if (orientation == vertical && size == 3){
- while (size == 3){
- occupiedSpace *= 0;
- row = (int) (Math.random()*7)+1;
- column = (int) (Math.random()*10)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row+i][column];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int k = 0; k < size; k++)
- grid[row+k][column] = " D";
- break;
- }
- }
- }
- else if (orientation == horizontal && size == 3){
- while (size == 3){
- occupiedSpace *= 0;
- row = (int) (Math.random()*10)+1;
- column = (int) (Math.random()*7)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row][column+i];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int z = 0; z < size; z++)
- grid[row][column+z] = " D";
- break;
- }
- }
- }
- orientation = (int) (Math.random()*2)+1;
- if (orientation == vertical && size == 3){
- while (size == 3){
- occupiedSpace *= 0;
- row = (int) (Math.random()*7)+1;
- column = (int) (Math.random()*10)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row+i][column];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int k = 0; k < size; k++)
- grid[row+k][column] = " S";
- size --;
- }
- }
- }
- else if (orientation == horizontal && size == 3){
- while (size == 3){
- occupiedSpace *= 0;
- row = (int) (Math.random()*10)+1;
- column = (int) (Math.random()*7)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row][column+i];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int z = 0; z < size; z++)
- grid[row][column+z] = " S";
- size --;
- }
- }
- }
- orientation = (int) (Math.random()*2)+1;
- if (orientation == vertical && size == 2){
- while (size == 2){
- occupiedSpace *= 0;
- row = (int) (Math.random()*8)+1;
- column = (int) (Math.random()*10)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row+i][column];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int k = 0; k < size; k++)
- grid[row+k][column] = " P";
- size --;
- }
- }
- }
- else if (orientation == horizontal && size == 2){
- while (size == 2){
- occupiedSpace *= 0;
- row = (int) (Math.random()*10)+1;
- column = (int) (Math.random()*8)+1;
- String test = grid[row][column];
- for (int i = 0; i < size; i++){
- test = grid[row][column+i];
- if (!test.equals(" ")){
- occupiedSpace ++;
- }
- }
- if (occupiedSpace == 0){
- for (int z = 0; z < size; z++)
- grid[row][column+z] = " P";
- size --;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement