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.
|
|
import java.util.Arrays;public class Ship{ public static final int X = 0; public static final int Y = 1; private boolean vertical; private int[] start; private int[] end; private int length;
public Ship(int[] start,int[] end){ this.start = start; this.end = end; this.correctSE(); int cord; if (this.vertical) { cord = Y; } else { cord = X; }
this.length = this.end[cord] - this.start[cord]+1; }
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 int[] getStart() { return this.start; } public int[] getEnd() { return this.end; }
// ensures start and end are in the right place
private void correctSE(){ int cord; this.vertical = this.start[X] == this.end[X]; if (this.vertical) { cord = Y; } else { cord = X; }
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.correctSE(); }
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; int cord1; int cord2; // <necessary bit, ensures algorithm works both when other is oriented horizontally or vertically>
if (other.vertical) { cord1 = X; cord2 = Y; } else { cord1 = Y; cord2 = X; } // </necessary bit>
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;
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); return cond1 && cond2; } }
public void print() { System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")"); System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")"); }
public void placeOnBoard(int[][] board){ this.correctSE(); int x_col = this.start[X]; int y_col = this.start[Y]; 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; } } else { for (c = this.start[X];c <= this.end[X];c++){ board[y_col][c] = this.length; } } }}
|