From e42641f87b53c3f531db2cfadae13ff599e34952 Mon Sep 17 00:00:00 2001 From: school Date: Tue, 2 Oct 2018 01:35:22 -0500 Subject: [PATCH] Battleship incomplete. Added rotate and create random methods to ship and debugged Ship.java --- Battleship.java | 81 +++++++++++++++++++++++++++---- Ship.java | 126 ++++++++++++++++++++++++++++++++++++++++++------ ShipTest.java | 46 ++++++------------ 3 files changed, 197 insertions(+), 56 deletions(-) diff --git a/Battleship.java b/Battleship.java index 4e18e55..dd4315d 100644 --- a/Battleship.java +++ b/Battleship.java @@ -4,7 +4,10 @@ // Submarine = 3 // Pilot = 2 public class Battleship{ - private int[][] playerBaoard; + 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; @@ -15,14 +18,74 @@ public class Battleship{ this.computerBoardHidden = new int[x][y]; } - public getComputerBoardHidden(){ + public int[][] getComputerBoardHidden(){ return this.computerBoardHidden; - } - - public getComputerBoardDisplay(){ + } + + public int[][] getComputerBoardDisplay(){ return this.computerBoardDisplay; - } + } + + public int[][] getPlayerBoard(){ + return this.playerBoard; + } + + + public static void main(String[] args){ + int rand = randint(9,10); + System.out.println(rand); + } + public static void randomizingBoard(int[][] board){ + boolean vert = false; + int length; + int[] lengths = { + 2, + 3, + 3, + 4, + 5} + + boolean placed; + Ship[] ships = new Ship[5]; - public getPlayerBoard(){ - return this.playerBaoard; - } \ No newline at end of file + for (int i = 1; i < 5; i++){ + + length = lengths[i] + placed = false; + while (!placed){ + int[] start = new int[2]; + + int[] end = start.clone(); + + if vert{ + end[Y] += length-1; + } + else { + end[X] += length-1; + } + + Ship cur_ship = new Ship(start,end); + int rot = 0; + while ((rot < 4) && !placed) { + if cur_ship.isInside(board[0].length,board.length) { + for (int j = 0; j < i + 1; j++){ + placed = !isIntersecting( + } + else { + rot = 4; + } + + + } + // swap starting position each time + if (vert) { + vert = false; + } + else { + vert = true + } + } + } + + +} \ No newline at end of file diff --git a/Ship.java b/Ship.java index 934e10a..f38fab1 100644 --- a/Ship.java +++ b/Ship.java @@ -1,3 +1,4 @@ +import java.util.Arrays; public class Ship{ public static final int X = 0; public static final int Y = 1; @@ -5,11 +6,12 @@ public class Ship{ private int[] start; private int[] end; private int length; + + public Ship(int[] start,int[] end){ - + int cord; boolean vertical = start[Y] == end[Y]; this.vertical = vertical; - int cord; if (vertical) { cord = X; } @@ -26,24 +28,113 @@ public class Ship{ } this.length = this.end[cord] - this.start[cord]+1; } - - public void print() { - System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")"); - System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")"); + + 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[] start, int[] end,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]); + 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 + if (s_start[cord] + length-1 < end[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 boolean isVertical() { - return this.vertical; - } public int[] getStart() { return this.start; } public int[] getEnd() { return this.end; } - public boolean intersects(Ship other){ + + // ensures start and end are in the right place + private void correntSE(){ + int cord; + if (this.vertical) { + cord = X; + } + else { + cord = Y; + } + + 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.correntSE(); + } + + public boolean isVertical() { + 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 isIntersecting(Ship other){ boolean cond1; boolean cond2; boolean cond3; @@ -59,7 +150,7 @@ public class Ship{ cord2 = X; } // - + int t_s1 = this.start[cord1]; int t_e1 = this.end[cord1]; int o_s1 = other.start[cord1]; @@ -74,8 +165,8 @@ public class Ship{ if (this.vertical == other.vertical) { // overlaps cond1 = t_s2 == o_s2; - - // intersects + + // intersectss cond2 = o_e1 >= t_s1; cond3 = t_e1 >= o_s1; @@ -84,7 +175,7 @@ public class Ship{ // 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 @@ -92,4 +183,9 @@ public class Ship{ return cond1 && cond2; } } -} \ No newline at end of file + + public void print() { + System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")"); + System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")"); + } +} \ No newline at end of file diff --git a/ShipTest.java b/ShipTest.java index 38a6fc4..970f061 100644 --- a/ShipTest.java +++ b/ShipTest.java @@ -1,39 +1,21 @@ import java.util.Scanner; public class ShipTest{ - public static void testShips(Ship ship1, Ship ship2,String name){ - System.out.println("Testing " + name); - ship1.print(); - System.out.println(); - ship2.print(); - System.out.println(ship1.intersects(ship2)+"=="+ship2.intersects(ship1)); + public static void createTestShips(int[] start,int[] end, Ship[] fill){ + for (int i = 0; i < fill.length; i++){ + fill[i] = Ship.randomShip(start,end,Ship.randint(2,4),true); + } } + public static void main(String[] args){ - int[] ship1start = new int[2]; - int[] ship1end = new int[2]; - - int[] ship2start = new int[2]; - int[] ship2end = new int[2]; - String name; - while (true) { - Scanner input = new Scanner(System.in); - System.out.print("Enter test name: "); - name = input.nextLine(); - - System.out.print("Enter ship1 start: "); - ship1start[0] = input.nextInt(); - ship1start[1] = input.nextInt(); - System.out.print("Enter ship1 end: "); - ship1end[0] = input.nextInt(); - ship1end[1] = input.nextInt(); - - System.out.print("Enter ship2 start: "); - ship2start[0] = input.nextInt(); - ship2start[1] = input.nextInt(); - System.out.print("Enter ship2 end: "); - ship2end[0] = input.nextInt(); - ship2end[1] = input.nextInt(); + Ship[] test_ships = new Ship[5]; + createTestShips(new int[] {0,0}, new int[] {5,5},test_ships); + for (int i = 0; i < test_ships.length-1;i++){ + test_ships[i].print(); + System.out.println(); + test_ships[i+1].print(); - testShips(new Ship(ship1start,ship1end),new Ship(ship2start,ship2end),name); + System.out.println(test_ships[i].isIntersecting(test_ships[i+1])); + System.out.println(); } } -} \ No newline at end of file +} \ No newline at end of file