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.

203 lines
4.9 KiB

  1. import java.util.Arrays;
  2. public class Ship{
  3. public static final int X = 0;
  4. public static final int Y = 1;
  5. private boolean vertical;
  6. private int[] start;
  7. private int[] end;
  8. private int length;
  9. public Ship(int[] start,int[] end){
  10. this.start = start;
  11. this.end = end;
  12. this.correctSE();
  13. int cord;
  14. if (this.vertical) {
  15. cord = Y;
  16. }
  17. else {
  18. cord = X;
  19. }
  20. this.length = this.end[cord] - this.start[cord]+1;
  21. }
  22. public static int randint(int a, int b){
  23. int rand_dist = (int) (Math.random() * (double) (b + 1 - a));
  24. return rand_dist + a;
  25. }
  26. // creates new random ship
  27. public static Ship randomShip(int[] start, int[] end,int length, boolean vert) {
  28. int[] s_start = new int[2];
  29. s_start[X] = randint(start[X],end[X]);
  30. s_start[Y] = randint(start[Y],end[Y]);
  31. int[] s_end = s_start.clone();
  32. int cord;
  33. // what direction do we want this pointing?
  34. if (vert){
  35. cord = Y;
  36. }
  37. else {
  38. cord = X;
  39. }
  40. // check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way
  41. if (s_start[cord] + length-1 < end[cord]) {
  42. s_end[cord] += length-1;
  43. }
  44. else {
  45. s_end[cord] -= length-1;
  46. }
  47. return new Ship(s_start,s_end);
  48. }
  49. public int getLength() {
  50. return this.length;
  51. }
  52. public int[] getStart() {
  53. return this.start;
  54. }
  55. public int[] getEnd() {
  56. return this.end;
  57. }
  58. // ensures start and end are in the right place
  59. private void correctSE(){
  60. int cord;
  61. this.vertical = this.start[X] == this.end[X];
  62. if (this.vertical) {
  63. cord = Y;
  64. }
  65. else {
  66. cord = X;
  67. }
  68. int[] third_point;
  69. if (start[cord] > end[cord]){
  70. third_point = this.start;
  71. this.start = this.end;
  72. this.end = third_point;
  73. }
  74. }
  75. private void rotate(int[] point){
  76. // one point is tacked to the board and the other is moved around
  77. int[] stationary_point;
  78. int[] moving_point;
  79. // either the start or end will be stationary
  80. if (Arrays.equals(this.start,point)) {
  81. moving_point = this.end;
  82. stationary_point = this.start;
  83. }
  84. else {
  85. moving_point = this.start;
  86. stationary_point = this.end;
  87. }
  88. // get the new coordinates
  89. int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y];
  90. int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X];
  91. // set them
  92. moving_point[X] = new_x;
  93. moving_point[Y] = new_y;
  94. }
  95. public void rotate(int[] point, int n){
  96. for (int i = 0; i < n; i++){
  97. this.rotate(point);
  98. }
  99. this.correctSE();
  100. }
  101. public boolean isVertical() {
  102. return this.vertical;
  103. }
  104. public boolean isInside(int[] start, int[] end){
  105. return this.start[X] >= start[X] && this.end[X] <= end[X] &&
  106. this.start[Y] >= start[Y] && this.end[Y] <= end[Y];
  107. }
  108. public boolean isIntersecting(Ship other){
  109. boolean cond1;
  110. boolean cond2;
  111. boolean cond3;
  112. int cord1;
  113. int cord2;
  114. // <necessary bit, ensures algorithm works both when other is oriented horizontally or vertically>
  115. if (other.vertical) {
  116. cord1 = X;
  117. cord2 = Y;
  118. }
  119. else {
  120. cord1 = Y;
  121. cord2 = X;
  122. }
  123. // </necessary bit>
  124. int t_s1 = this.start[cord1];
  125. int t_e1 = this.end[cord1];
  126. int o_s1 = other.start[cord1];
  127. int o_e1 = other.end[cord1];
  128. int t_s2 = this.start[cord2];
  129. int t_e2 = this.end[cord2];
  130. int o_s2 = other.start[cord2];
  131. int o_e2 = other.end[cord2];
  132. // lines are parallel
  133. if (this.vertical == other.vertical) {
  134. // overlaps
  135. cond1 = t_s2 == o_s2;
  136. // intersects
  137. cond2 = o_e1 >= t_s1;
  138. cond3 = t_e1 >= o_s1;
  139. return cond1 && (cond2 || cond3);
  140. }
  141. // lines are perpendicular
  142. else{
  143. // other in range of this with respect to axis cord1
  144. cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1);
  145. // this in range of other with respect to axis cord2
  146. cond2 = (o_s2 >= t_s2) && (o_e2 <= t_e2);
  147. return cond1 && cond2;
  148. }
  149. }
  150. public void print() {
  151. System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")");
  152. System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")");
  153. }
  154. public void placeOnBoard(int[][] board){
  155. this.correctSE();
  156. int x_col = this.start[X];
  157. int y_col = this.start[Y];
  158. int c;
  159. if (this.vertical){
  160. for (c = this.start[Y];c <= this.end[Y];c++){
  161. System.out.println(c);
  162. board[c][x_col] = this.length;
  163. }
  164. }
  165. else {
  166. for (c = this.start[X];c <= this.end[X];c++){
  167. board[y_col][c] = this.length;
  168. }
  169. }
  170. }
  171. }