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.

363 lines
10 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // Carrier = 5
  2. // Battleship = 4
  3. // Cruiser = 3
  4. // Submarine = 3
  5. // Pilot = 2
  6. import java.util.Arrays;
  7. public class Battleship{
  8. public static final int X = 0;
  9. public static final int Y = 0;
  10. private int[][] playerBoard;
  11. private int[][] computerBoardDisplay;
  12. private int[][] computerBoardHidden;
  13. private static int playerHits;
  14. private static int computerHits;
  15. public Battleship(int x, int y){
  16. this.playerBoard = new int[x][y];
  17. randomizingBoard(this.playerBoard);
  18. this.computerBoardHidden = new int[x][y];
  19. randomizingBoard(this.computerBoardHidden);
  20. this.computerBoardDisplay = new int[x][y];
  21. }
  22. public int[][] getComputerBoardHidden(){
  23. return this.computerBoardHidden;
  24. }
  25. public int[][] getComputerBoardDisplay(){
  26. return this.computerBoardDisplay;
  27. }
  28. public int[][] getPlayerBoard(){
  29. return this.playerBoard;
  30. }
  31. public boolean checkPlayerWin() {
  32. return this.playerHits == 17;
  33. }
  34. public boolean checkComputerWin() {
  35. // 2 + 3 + 3 + 4 + 5 = 17
  36. return this.computerHits == 17;
  37. }
  38. public void computerTurn() {
  39. int x;
  40. int y;
  41. boolean done = false;
  42. while (!done) {
  43. x = Ship.randint(0,this.playerBoard[0].length-1);
  44. y = Ship.randint(0,this.playerBoard.length-1);
  45. if (playerBoard[x][y] == 3 || playerBoard[x][y] == 0) {
  46. done = true;
  47. if (playerBoard[x][y] == 3) {
  48. playerBoard[x][y] = 2;
  49. computerHits += 1;
  50. }
  51. else {
  52. playerBoard[x][y] = 1;
  53. }
  54. }
  55. }
  56. }
  57. public void updateComputerBoards(int x, int y,int[][] computerActual,int[][] computerDisplay) {
  58. boolean hit = computerActual[x][y] == 3;
  59. if (hit) {
  60. computerDisplay[x][y] = 2;
  61. computerActual[x][y] = 2;
  62. playerHits += 1;
  63. }
  64. else {
  65. computerDisplay[x][y] = 1;
  66. computerActual[x][y] = 1;
  67. }
  68. }
  69. public static void randomizingBoard(int[][] board){
  70. int[] boundTL = {0,0};
  71. int[] boundBR = {board[0].length-1, board.length-1};
  72. int length;
  73. int[] lengths = {
  74. 2,
  75. 3,
  76. 3,
  77. 4,
  78. 5};
  79. boolean placed;
  80. Ship[] ships = new Ship[5];
  81. int start_orientation = Ship.randint(0,1);
  82. // start seed for ship
  83. ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],start_orientation == 0);
  84. for (int i = 1; i < 5; i++){
  85. length = lengths[i];
  86. placed = false;
  87. // System.out.println(length);
  88. while (!placed){
  89. Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,(start_orientation+i)%2 == 0);
  90. boolean intersects = false;
  91. boolean out_of_bounds = false;
  92. int rot = 0;
  93. // we need to clone since start is modified in place
  94. int[] stationary = cur_ship.getStart().clone();
  95. while ((rot < 4) && !placed) {
  96. // checks if ship is out of bounds
  97. out_of_bounds = !cur_ship.isInside(boundTL,boundBR);
  98. if (!out_of_bounds) {
  99. // checks if ship is intersecting with any that were previously placed on the board.
  100. for (int j = 0; j < i; j++ ){
  101. intersects = cur_ship.isIntersecting(ships[j]);
  102. // if ship intersects with one no need to check the rest
  103. if (intersects) {
  104. break;
  105. }
  106. }
  107. }
  108. // if something is wrong with the ship placement rotate it
  109. if (out_of_bounds || intersects) {
  110. cur_ship.rotate(stationary,1);
  111. // if we rotate it 4 times it's the same as doing nothing, so to avoid infinite loop we cap rot at 4 before choosing new ship placement
  112. rot += 1;
  113. }
  114. // if nothing is wrong with it then mark it to be placed on the board
  115. else {
  116. ships[i] = cur_ship;
  117. placed = true;
  118. }
  119. }
  120. }
  121. }
  122. // we done, place those puppies on for real.
  123. for (int i = 0; i < 5; i++){
  124. ships[i].placeOnBoard(board);
  125. }
  126. }
  127. // method to make debug easier
  128. // NOTE: THIS GAME HAS THE X Y AXIS FLIPPED. Makes no difference all things still work
  129. public static void printBoard(int[][] arr){
  130. System.out.print(" ");
  131. for (int i = 0; i < arr[0].length-1; i ++){
  132. System.out.print(i + " ");
  133. }
  134. System.out.println(arr[0].length-1);
  135. for (int row = 0; row < arr.length; row++){
  136. String s = row +": ";
  137. for (int col = 0; col < arr[row].length; col ++){
  138. if (arr[row][col] > 0){
  139. s += arr[row][col];
  140. }
  141. else {
  142. s += "|";
  143. }
  144. if (col < arr[row].length - 1){
  145. s += " ";
  146. }
  147. }
  148. System.out.println(s + "\n");
  149. }
  150. }
  151. }
  152. class Ship{
  153. public static final int X = 0;
  154. public static final int Y = 1;
  155. private static final boolean DEBUG = false;
  156. private boolean vertical;
  157. private int[] start;
  158. private int[] end;
  159. private int length;
  160. public Ship(int[] start,int[] end){
  161. this.start = start;
  162. this.end = end;
  163. this.correctSE();
  164. int cord;
  165. if (this.vertical) {
  166. cord = Y;
  167. }
  168. else {
  169. cord = X;
  170. }
  171. this.length = this.end[cord] - this.start[cord]+1;
  172. }
  173. public static int randint(int a, int b){
  174. int rand_dist = (int) (Math.random() * (double) (b + 1 - a));
  175. return rand_dist + a;
  176. }
  177. // creates new random ship
  178. public static Ship randomShip(int[] boundTL, int[] boundBR,int length, boolean vert) {
  179. int[] s_start = new int[2];
  180. s_start[X] = randint(boundTL[X],boundBR[X]);
  181. s_start[Y] = randint(boundTL[Y],boundBR[Y]);
  182. int[] s_end = s_start.clone();
  183. int cord;
  184. // what direction do we want this pointing?
  185. if (vert){
  186. cord = Y;
  187. }
  188. else {
  189. cord = X;
  190. }
  191. // check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way, unless the board cannot fit the ship (impossible with 10x10)
  192. if (s_start[cord] + length-1 < boundBR[cord]) {
  193. s_end[cord] += length-1;
  194. }
  195. else {
  196. s_end[cord] -= length-1;
  197. }
  198. return new Ship(s_start,s_end);
  199. }
  200. public int getLength() {
  201. return this.length;
  202. }
  203. public int[] getStart() {
  204. return this.start;
  205. }
  206. public int[] getEnd() {
  207. return this.end;
  208. }
  209. // ensures start and end are in the right place
  210. private void correctSE(){
  211. int cord;
  212. this.vertical = this.start[X] == this.end[X];
  213. if (this.vertical) {
  214. cord = Y;
  215. }
  216. else {
  217. cord = X;
  218. }
  219. int[] third_point;
  220. if (start[cord] > end[cord]){
  221. third_point = this.start;
  222. this.start = this.end;
  223. this.end = third_point;
  224. }
  225. }
  226. private void rotate(int[] point){
  227. // one point is tacked to the board and the other is moved around
  228. int[] stationary_point;
  229. int[] moving_point;
  230. // either the start or end will be stationary
  231. if (Arrays.equals(this.start,point)) {
  232. moving_point = this.end;
  233. stationary_point = this.start;
  234. }
  235. else {
  236. moving_point = this.start;
  237. stationary_point = this.end;
  238. }
  239. // get the new coordinates
  240. int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y];
  241. int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X];
  242. // set them
  243. moving_point[X] = new_x;
  244. moving_point[Y] = new_y;
  245. }
  246. public void rotate(int[] point, int n){
  247. for (int i = 0; i < n; i++){
  248. this.rotate(point);
  249. }
  250. this.correctSE();
  251. }
  252. public boolean isVertical() {
  253. return this.vertical;
  254. }
  255. public boolean isInside(int[] boundTL, int[] boundBR){
  256. return this.start[X] >= boundTL[X] && this.end[X] <= boundBR[X] &&
  257. this.start[Y] >= boundTL[Y] && this.end[Y] <= boundBR[Y];
  258. }
  259. private static boolean inbetween (int a, int b, int c){
  260. return a <= b && b <= c;
  261. }
  262. public boolean isIntersecting(Ship other){
  263. boolean cond1;
  264. boolean cond2;
  265. boolean cond3;
  266. int cord1;
  267. int cord2;
  268. // <necessary bit, ensures algorithm works both when other is oriented horizontally or vertically>
  269. if (other.vertical) {
  270. cord1 = X;
  271. cord2 = Y;
  272. }
  273. else {
  274. cord1 = Y;
  275. cord2 = X;
  276. }
  277. // </necessary bit>
  278. // lines are parallel
  279. if (this.vertical == other.vertical) {
  280. cond1 = this.start[cord1] == other.end[cord1];
  281. cond2 = inbetween(this.start[cord2],other.start[cord2],this.end[cord2]);
  282. cond3 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
  283. return cond1 && (cond2 || cond3);
  284. }
  285. // lines are perpendicular
  286. else{
  287. cond1 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
  288. cond2 = inbetween(this.start[cord1],other.start[cord1],this.end[cord1]);
  289. return cond1 && cond2;
  290. }
  291. }
  292. public void print() {
  293. System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")");
  294. System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")");
  295. }
  296. public void placeOnBoard(int[][] board){
  297. this.correctSE();
  298. int x_col = this.start[X];
  299. int y_col = this.start[Y];
  300. int c;
  301. if (this.vertical){
  302. for (c = this.start[Y];c <= this.end[Y];c++){
  303. if (DEBUG) {
  304. board[c][x_col] = this.length;
  305. }
  306. else {
  307. board[c][x_col] = 3;
  308. }
  309. }
  310. }
  311. else {
  312. for (c = this.start[X];c <= this.end[X];c++){
  313. if (DEBUG) {
  314. board[y_col][c] = this.length;
  315. }
  316. else {
  317. board[y_col][c] = 3;
  318. }
  319. }
  320. }
  321. }
  322. }