// Carrier = 5 // Battleship = 4 // Cruiser = 3 // Submarine = 3 // Pilot = 2 import java.util.Arrays; public class Battleship{ public static final int X = 0; public static final int Y = 0; private int[][] playerBoard; private int[][] computerBoardDisplay; private int[][] computerBoardHidden; private static int playerHits; private static int computerHits; public Battleship(int x, int y){ this.playerBoard = new int[x][y]; randomizingBoard(this.playerBoard); this.computerBoardHidden = new int[x][y]; randomizingBoard(this.computerBoardHidden); this.computerBoardDisplay = new int[x][y]; } public int[][] getComputerBoardHidden(){ return this.computerBoardHidden; } public int[][] getComputerBoardDisplay(){ return this.computerBoardDisplay; } public int[][] getPlayerBoard(){ return this.playerBoard; } public boolean checkPlayerWin() { return this.playerHits == 17; } public boolean checkComputerWin() { // 2 + 3 + 3 + 4 + 5 = 17 return this.computerHits == 17; } public void computerTurn() { int x; int y; boolean done = false; while (!done) { x = Ship.randint(0,this.playerBoard[0].length-1); y = Ship.randint(0,this.playerBoard.length-1); if (playerBoard[x][y] == 3 || playerBoard[x][y] == 0) { done = true; if (playerBoard[x][y] == 3) { playerBoard[x][y] = 2; computerHits += 1; } else { playerBoard[x][y] = 1; } } } } public void updateComputerBoards(int x, int y,int[][] computerActual,int[][] computerDisplay) { boolean hit = computerActual[x][y] == 3; if (hit) { computerDisplay[x][y] = 2; computerActual[x][y] = 2; playerHits += 1; } else { computerDisplay[x][y] = 1; computerActual[x][y] = 1; } } public static void randomizingBoard(int[][] board){ int[] boundTL = {0,0}; int[] boundBR = {board[0].length-1, board.length-1}; int length; int[] lengths = { 2, 3, 3, 4, 5}; boolean placed; Ship[] ships = new Ship[5]; int start_orientation = Ship.randint(0,1); // start seed for ship ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],start_orientation == 0); for (int i = 1; i < 5; i++){ length = lengths[i]; placed = false; // System.out.println(length); while (!placed){ Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,(start_orientation+i)%2 == 0); boolean intersects = false; boolean out_of_bounds = false; int rot = 0; // we need to clone since start is modified in place int[] stationary = cur_ship.getStart().clone(); while ((rot < 4) && !placed) { // checks if ship is out of bounds out_of_bounds = !cur_ship.isInside(boundTL,boundBR); if (!out_of_bounds) { // checks if ship is intersecting with any that were previously placed on the board. for (int j = 0; j < i; j++ ){ intersects = cur_ship.isIntersecting(ships[j]); // if ship intersects with one no need to check the rest if (intersects) { break; } } } // if something is wrong with the ship placement rotate it if (out_of_bounds || intersects) { cur_ship.rotate(stationary,1); // if we rotate it 4 times it's the same as doing nothing, so to avoid infinite loop we cap rot at 4 before choosing new ship placement rot += 1; } // if nothing is wrong with it then mark it to be placed on the board else { ships[i] = cur_ship; placed = true; } } } } // we done, place those puppies on for real. for (int i = 0; i < 5; i++){ ships[i].placeOnBoard(board); } } // method to make debug easier // NOTE: THIS GAME HAS THE X Y AXIS FLIPPED. Makes no difference all things still work public static void printBoard(int[][] arr){ System.out.print(" "); for (int i = 0; i < arr[0].length-1; i ++){ System.out.print(i + " "); } System.out.println(arr[0].length-1); for (int row = 0; row < arr.length; row++){ String s = row +": "; for (int col = 0; col < arr[row].length; col ++){ if (arr[row][col] > 0){ s += arr[row][col]; } else { s += "|"; } if (col < arr[row].length - 1){ s += " "; } } System.out.println(s + "\n"); } } } class Ship{ public static final int X = 0; public static final int Y = 1; private static final boolean DEBUG = false; private boolean vertical; private int[] start; private int[] end; private int length; public Ship(int[] start,int[] end){ this.start = start; this.end = end; this.correctSE(); int cord; if (this.vertical) { cord = Y; } else { cord = X; } this.length = this.end[cord] - this.start[cord]+1; } public static int randint(int a, int b){ int rand_dist = (int) (Math.random() * (double) (b + 1 - a)); return rand_dist + a; } // creates new random ship public static Ship randomShip(int[] boundTL, int[] boundBR,int length, boolean vert) { int[] s_start = new int[2]; s_start[X] = randint(boundTL[X],boundBR[X]); s_start[Y] = randint(boundTL[Y],boundBR[Y]); int[] s_end = s_start.clone(); int cord; // what direction do we want this pointing? if (vert){ cord = Y; } else { cord = X; } // check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way, unless the board cannot fit the ship (impossible with 10x10) if (s_start[cord] + length-1 < boundBR[cord]) { s_end[cord] += length-1; } else { s_end[cord] -= length-1; } return new Ship(s_start,s_end); } public int getLength() { return this.length; } public int[] getStart() { return this.start; } public int[] getEnd() { return this.end; } // ensures start and end are in the right place private void correctSE(){ int cord; this.vertical = this.start[X] == this.end[X]; if (this.vertical) { cord = Y; } else { cord = X; } int[] third_point; if (start[cord] > end[cord]){ third_point = this.start; this.start = this.end; this.end = third_point; } } private void rotate(int[] point){ // one point is tacked to the board and the other is moved around int[] stationary_point; int[] moving_point; // either the start or end will be stationary if (Arrays.equals(this.start,point)) { moving_point = this.end; stationary_point = this.start; } else { moving_point = this.start; stationary_point = this.end; } // get the new coordinates int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y]; int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X]; // set them moving_point[X] = new_x; moving_point[Y] = new_y; } public void rotate(int[] point, int n){ for (int i = 0; i < n; i++){ this.rotate(point); } this.correctSE(); } public boolean isVertical() { return this.vertical; } public boolean isInside(int[] boundTL, int[] boundBR){ return this.start[X] >= boundTL[X] && this.end[X] <= boundBR[X] && this.start[Y] >= boundTL[Y] && this.end[Y] <= boundBR[Y]; } private static boolean inbetween (int a, int b, int c){ return a <= b && b <= c; } public boolean isIntersecting(Ship other){ boolean cond1; boolean cond2; boolean cond3; int cord1; int cord2; // if (other.vertical) { cord1 = X; cord2 = Y; } else { cord1 = Y; cord2 = X; } // // lines are parallel if (this.vertical == other.vertical) { cond1 = this.start[cord1] == other.end[cord1]; cond2 = inbetween(this.start[cord2],other.start[cord2],this.end[cord2]); cond3 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]); return cond1 && (cond2 || cond3); } // lines are perpendicular else{ cond1 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]); cond2 = inbetween(this.start[cord1],other.start[cord1],this.end[cord1]); return cond1 && cond2; } } public void print() { System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")"); System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")"); } public void placeOnBoard(int[][] board){ this.correctSE(); int x_col = this.start[X]; int y_col = this.start[Y]; int c; if (this.vertical){ for (c = this.start[Y];c <= this.end[Y];c++){ if (DEBUG) { board[c][x_col] = this.length; } else { board[c][x_col] = 3; } } } else { for (c = this.start[X];c <= this.end[X];c++){ if (DEBUG) { board[y_col][c] = this.length; } else { board[y_col][c] = 3; } } } } }