|
|
|
@ -3,6 +3,7 @@ |
|
|
|
// 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; |
|
|
|
@ -31,9 +32,11 @@ public class Battleship{ |
|
|
|
} |
|
|
|
|
|
|
|
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, |
|
|
|
@ -43,58 +46,71 @@ public class Battleship{ |
|
|
|
|
|
|
|
boolean placed; |
|
|
|
Ship[] ships = new Ship[5]; |
|
|
|
ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],true); |
|
|
|
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); |
|
|
|
// System.out.println(length); |
|
|
|
while (!placed){ |
|
|
|
|
|
|
|
Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,i%2 == 0); |
|
|
|
cur_ship.print(); |
|
|
|
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) { |
|
|
|
// 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; |
|
|
|
cur_ship.print(); |
|
|
|
// 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 (intersects) { |
|
|
|
// 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 (cur_ship.isInside(boundTL,boundBR)){ |
|
|
|
if (!intersects){ |
|
|
|
ships[i] = cur_ship; |
|
|
|
placed = true; |
|
|
|
} |
|
|
|
else { |
|
|
|
cur_ship.rotate(stationary,1); |
|
|
|
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].print(); |
|
|
|
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 = ""; |
|
|
|
String s = row +": "; |
|
|
|
for (int col = 0; col < arr[row].length; col ++){ |
|
|
|
s += arr[row][col]; |
|
|
|
if (arr[row][col] > 0){ |
|
|
|
s += arr[row][col]; |
|
|
|
} |
|
|
|
else { |
|
|
|
s += "|"; |
|
|
|
} |
|
|
|
if (col < arr[row].length - 1){ |
|
|
|
s += " "; |
|
|
|
} |
|
|
|
@ -105,17 +121,8 @@ public class Battleship{ |
|
|
|
} |
|
|
|
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); |
|
|
|
} |
|
|
|
randomizingBoard(testBoard); |
|
|
|
printBoard(testBoard); |
|
|
|
|
|
|
|
//printBoard(testBoard); |
|
|
|
|
|
|
|
|