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.

165 lines
4.9 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. public class Battleship{
  7. public static final int X = 0;
  8. public static final int Y = 0;
  9. private int[][] playerBoard;
  10. private int[][] computerBoardDisplay;
  11. private int[][] computerBoardHidden;
  12. private static int playerHits;
  13. private static int computerHits;
  14. public Battleship(int x, int y){
  15. this.playerBoard = new int[x][y];
  16. randomizingBoard(this.playerBoard);
  17. this.computerBoardHidden = new int[x][y];
  18. randomizingBoard(this.computerBoardHidden);
  19. this.computerBoardDisplay = new int[x][y];
  20. }
  21. public int[][] getComputerBoardHidden(){
  22. return this.computerBoardHidden;
  23. }
  24. public int[][] getComputerBoardDisplay(){
  25. return this.computerBoardDisplay;
  26. }
  27. public int[][] getPlayerBoard(){
  28. return this.playerBoard;
  29. }
  30. public boolean checkPlayerWin() {
  31. return this.playerHits == 17;
  32. }
  33. public boolean checkComputerWin() {
  34. // 2 + 3 + 3 + 4 + 5 = 17
  35. return this.computerHits == 17;
  36. }
  37. public void computerTurn() {
  38. int x;
  39. int y;
  40. boolean done = false;
  41. while (!done) {
  42. x = Ship.randint(0,this.playerBoard[0].length-1);
  43. y = Ship.randint(0,this.playerBoard.length-1);
  44. if (playerBoard[x][y] == 3 || playerBoard[x][y] == 0) {
  45. done = true;
  46. if (playerBoard[x][y] == 3) {
  47. playerBoard[x][y] = 2;
  48. computerHits += 1;
  49. }
  50. else {
  51. playerBoard[x][y] = 1;
  52. }
  53. }
  54. }
  55. }
  56. public void updateComputerBoards(int x, int y,int[][] computerActual,int[][] computerDisplay) {
  57. boolean hit = computerActual[x][y] == 3;
  58. if (hit) {
  59. computerDisplay[x][y] = 2;
  60. computerActual[x][y] = 2;
  61. playerHits += 1;
  62. }
  63. else {
  64. computerDisplay[x][y] = 1;
  65. computerActual[x][y] = 1;
  66. }
  67. }
  68. public static void randomizingBoard(int[][] board){
  69. int[] boundTL = {0,0};
  70. int[] boundBR = {board[0].length-1, board.length-1};
  71. int length;
  72. int[] lengths = {
  73. 2,
  74. 3,
  75. 3,
  76. 4,
  77. 5};
  78. boolean placed;
  79. Ship[] ships = new Ship[5];
  80. int start_orientation = Ship.randint(0,1);
  81. // start seed for ship
  82. ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],start_orientation == 0);
  83. for (int i = 1; i < 5; i++){
  84. length = lengths[i];
  85. placed = false;
  86. // System.out.println(length);
  87. while (!placed){
  88. Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,(start_orientation+i)%2 == 0);
  89. boolean intersects = false;
  90. boolean out_of_bounds = false;
  91. int rot = 0;
  92. // we need to clone since start is modified in place
  93. int[] stationary = cur_ship.getStart().clone();
  94. while ((rot < 4) && !placed) {
  95. // checks if ship is out of bounds
  96. out_of_bounds = !cur_ship.isInside(boundTL,boundBR);
  97. if (!out_of_bounds) {
  98. // checks if ship is intersecting with any that were previously placed on the board.
  99. for (int j = 0; j < i; j++ ){
  100. intersects = cur_ship.isIntersecting(ships[j]);
  101. // if ship intersects with one no need to check the rest
  102. if (intersects) {
  103. break;
  104. }
  105. }
  106. }
  107. // if something is wrong with the ship placement rotate it
  108. if (out_of_bounds || intersects) {
  109. cur_ship.rotate(stationary,1);
  110. // 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
  111. rot += 1;
  112. }
  113. // if nothing is wrong with it then mark it to be placed on the board
  114. else {
  115. ships[i] = cur_ship;
  116. placed = true;
  117. }
  118. }
  119. }
  120. }
  121. // we done, place those puppies on for real.
  122. for (int i = 0; i < 5; i++){
  123. ships[i].placeOnBoard(board);
  124. }
  125. }
  126. // method to make debug easier
  127. // NOTE: THIS GAME HAS THE X Y AXIS FLIPPED. Makes no difference all things still work
  128. public static void printBoard(int[][] arr){
  129. System.out.print(" ");
  130. for (int i = 0; i < arr[0].length-1; i ++){
  131. System.out.print(i + " ");
  132. }
  133. System.out.println(arr[0].length-1);
  134. for (int row = 0; row < arr.length; row++){
  135. String s = row +": ";
  136. for (int col = 0; col < arr[row].length; col ++){
  137. if (arr[row][col] > 0){
  138. s += arr[row][col];
  139. }
  140. else {
  141. s += "|";
  142. }
  143. if (col < arr[row].length - 1){
  144. s += " ";
  145. }
  146. }
  147. System.out.println(s + "\n");
  148. }
  149. }
  150. }