Browse Source

Small rename of varibles so it's less of an eyesore testing phase

shiptest
school 7 years ago
parent
commit
240cec17a4
  1. 61
      Ship.java
  2. 6
      ShipTest.java

61
Ship.java

@ -6,7 +6,7 @@ public class Ship{
private int[] end;
private int length;
public Ship(int[] start,int[] end){
boolean vertical = start[Y] == end[Y];
this.vertical = vertical;
int cord;
@ -26,7 +26,7 @@ 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] + ")");
@ -34,14 +34,23 @@ public class Ship{
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){
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) {
if (other.isVertical()) {
cord1 = X;
cord2 = Y;
}
@ -50,38 +59,38 @@ public class Ship{
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 = t_s2;
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) {
// System.out.println("parallel");
if (this.vertical == other.isVertical()) {
// overlaps
cond1 = this.start[cord1] == other.start[cord1];
cond1 = t_s2 == o_s2;
// intersects
cond2 = (other.start[cord2] <= this.end[cord2]) || (this.start[cord2] <= other.end[cord2]);
return cond1 && cond2;
cond2 = o_e1 <= t_s1;
cond3 = t_e1 <= o_s1;
return cond1 && (cond2 || cond3);
}
// lines are perpendicular
else{
// System.out.println("perpendicular");
// other in range of this with respect to axis cord1
cond1 = (this.start[cord1] >= other.start[cord1]) && (this.end[cord1] <= other.end[cord1]);
cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1);
// this in range of other with respect to axis cord2
cond2 = (other.start[cord2] >= this.start[cord2]) && (other.end[cord2] <= this.end[cord2]);
cond2 = (o_s2 >= t_s2) && (o_e2 <= t_e2);
return cond1 && cond2;
}
}
public static void main(String[] args){
Ship placedShip1 = new Ship(new int[]{0,0},new int[]{0,2});
Ship placedShip2 = new Ship(new int[]{1,0},new int[]{1,3});
placedShip1.print();
placedShip2.print();
System.out.println(placedShip1.intersects(placedShip2));
System.out.println(placedShip1.getLength());
System.out.println(placedShip2.intersects(placedShip1));
System.out.println(placedShip2.getLength());
}
}

6
ShipTest.java

@ -0,0 +1,6 @@
public class ShipTest{
public testShips(Ship ship1, Ship ship2){
System.out.println(ship1.intersects(ship2)+"=="+ship2.intersects(ship1));
}
public static void main(String[] args){
Ship test_intersect0 = new Ship(new int[]{
Loading…
Cancel
Save