diff --git a/Battleship.java b/Battleship.java index 7443484..32c893b 100644 --- a/Battleship.java +++ b/Battleship.java @@ -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); diff --git a/Ship.java b/Ship.java index f029e7e..7291063 100644 --- a/Ship.java +++ b/Ship.java @@ -28,11 +28,11 @@ public class Ship{ } // creates new random ship - public static Ship randomShip(int[] start, int[] end,int length, boolean vert) { + public static Ship randomShip(int[] boundTL, int[] boundBR,int length, boolean vert) { int[] s_start = new int[2]; - s_start[X] = randint(start[X],end[X]); - s_start[Y] = randint(start[Y],end[Y]); + s_start[X] = randint(boundTL[X],boundBR[X]); + s_start[Y] = randint(boundTL[Y],boundBR[Y]); int[] s_end = s_start.clone(); int cord; @@ -45,8 +45,8 @@ public class Ship{ cord = X; } - // check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way - if (s_start[cord] + length-1 < end[cord]) { + // 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 { @@ -123,11 +123,13 @@ public class Ship{ return this.vertical; } - public boolean isInside(int[] start, int[] end){ - return this.start[X] >= start[X] && this.end[X] <= end[X] && - this.start[Y] >= start[Y] && this.end[Y] <= end[Y]; + 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; @@ -145,35 +147,18 @@ public class Ship{ } // - int t_s1 = this.start[cord1]; - int t_e1 = this.end[cord1]; - int o_s1 = other.start[cord1]; - int o_e1 = other.end[cord1]; - - int t_s2 = this.start[cord2]; - int t_e2 = this.end[cord2]; - int o_s2 = other.start[cord2]; - int o_e2 = other.end[cord2]; // lines are parallel if (this.vertical == other.vertical) { - // overlaps - cond1 = t_s2 == o_s2; - - // intersects - cond2 = o_e1 >= t_s1; - cond3 = t_e1 >= o_s1; - + 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{ - - // other in range of this with respect to axis cord1 - cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1); - // this in range of other with respect to axis cord2 - cond2 = (o_s2 >= t_s2) && (o_e2 <= t_e2); + 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; } } @@ -190,7 +175,6 @@ public class Ship{ int c; if (this.vertical){ for (c = this.start[Y];c <= this.end[Y];c++){ - System.out.println(c); board[c][x_col] = this.length; } }