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.

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