|
|
// Carrier = 5
// Battleship = 4
// Cruiser = 3
// Submarine = 3
// Pilot = 2
import java.util.Scanner;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){ Scanner input = new Scanner(System.in); 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
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"); } } /* public static void main(String[] args){ int[][] testBoard = new int[10][10]; randomizingBoard(testBoard); printBoard(testBoard);
//printBoard(testBoard);
} */}
|