You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.4 KiB

// Carrier = 5
// Battleship = 4
// Cruiser = 3
// Submarine = 3
// Pilot = 2
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];
this.computerBoardDisplay = new int[x][y];
this.computerBoardHidden = new int[x][y];
}
public int[][] getComputerBoardHidden(){
return this.computerBoardHidden;
}
public int[][] getComputerBoardDisplay(){
return this.computerBoardDisplay;
}
public int[][] getPlayerBoard(){
return this.playerBoard;
}
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];
ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],true);
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,i%2 == 0);
cur_ship.print();
boolean intersects = false;
int rot = 0;
// we need to clone since start is modified in place
int[] stationary = cur_ship.getStart().clone();
while ((rot < 4) && !placed) {
// ensures ship doesn't intersect with any that were previously placed
for (int j = 0; j < i; j++){
intersects = cur_ship.isIntersecting(ships[j]);
if (intersects) {
break;
}
}
if (intersects) {
cur_ship.rotate(stationary,1);
rot += 1;
}
if (cur_ship.isInside(boundTL,boundBR)){
if (!intersects){
ships[i] = cur_ship;
placed = true;
}
else {
cur_ship.rotate(stationary,1);
rot += 1;
}
}
}
}
}
// we done, place those puppies on for real.
for (int i = 0; i < 5; i++){
ships[i].print();
ships[i].placeOnBoard(board);
}
}
public static void printBoard(int[][] arr){
for (int row = 0; row < arr.length; row++){
String s = "";
for (int col = 0; col < arr[row].length; col ++){
s += arr[row][col];
if (col < arr[row].length - 1){
s += " ";
}
}
System.out.println(s + "\n");
}
}
public static void main(String[] args){
int[][] testBoard = new int[10][10];
Ship test = new Ship(new int[] {5,5}, new int[] {5,9});
int[] stationary = test.getStart().clone();
test.print();
test.placeOnBoard(testBoard);
for (int i = 0; i < 4; i++){
test.print();
test.placeOnBoard(testBoard);
printBoard(testBoard);
test.rotate(stationary,1);
}
//printBoard(testBoard);
}
}