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.

174 lines
5.1 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.Scanner;
  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. Scanner input = new Scanner(System.in);
  71. int[] boundTL = {0,0};
  72. int[] boundBR = {board[0].length-1, board.length-1};
  73. int length;
  74. int[] lengths = {
  75. 2,
  76. 3,
  77. 3,
  78. 4,
  79. 5};
  80. boolean placed;
  81. Ship[] ships = new Ship[5];
  82. int start_orientation = Ship.randint(0,1);
  83. // start seed for ship
  84. ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],start_orientation == 0);
  85. for (int i = 1; i < 5; i++){
  86. length = lengths[i];
  87. placed = false;
  88. // System.out.println(length);
  89. while (!placed){
  90. Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,(start_orientation+i)%2 == 0);
  91. boolean intersects = false;
  92. boolean out_of_bounds = false;
  93. int rot = 0;
  94. // we need to clone since start is modified in place
  95. int[] stationary = cur_ship.getStart().clone();
  96. while ((rot < 4) && !placed) {
  97. // checks if ship is out of bounds
  98. out_of_bounds = !cur_ship.isInside(boundTL,boundBR);
  99. if (!out_of_bounds) {
  100. // checks if ship is intersecting with any that were previously placed on the board.
  101. for (int j = 0; j < i; j++ ){
  102. intersects = cur_ship.isIntersecting(ships[j]);
  103. // if ship intersects with one no need to check the rest
  104. if (intersects) {
  105. break;
  106. }
  107. }
  108. }
  109. // if something is wrong with the ship placement rotate it
  110. if (out_of_bounds || intersects) {
  111. cur_ship.rotate(stationary,1);
  112. // 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
  113. rot += 1;
  114. }
  115. // if nothing is wrong with it then mark it to be placed on the board
  116. else {
  117. ships[i] = cur_ship;
  118. placed = true;
  119. }
  120. }
  121. }
  122. }
  123. // we done, place those puppies on for real.
  124. for (int i = 0; i < 5; i++){
  125. ships[i].placeOnBoard(board);
  126. }
  127. }
  128. // method to make debug easier
  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. public static void main(String[] args){
  153. int[][] testBoard = new int[10][10];
  154. randomizingBoard(testBoard);
  155. printBoard(testBoard);
  156. //printBoard(testBoard);
  157. }
  158. */
  159. }