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.

130 lines
4.0 KiB

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. this.computerBoardDisplay = new int[x][y];
  18. this.computerBoardHidden = new int[x][y];
  19. }
  20. public int[][] getComputerBoardHidden(){
  21. return this.computerBoardHidden;
  22. }
  23. public int[][] getComputerBoardDisplay(){
  24. return this.computerBoardDisplay;
  25. }
  26. public int[][] getPlayerBoard(){
  27. return this.playerBoard;
  28. }
  29. public static void randomizingBoard(int[][] board){
  30. Scanner input = new Scanner(System.in);
  31. int[] boundTL = {0,0};
  32. int[] boundBR = {board[0].length-1, board.length-1};
  33. int length;
  34. int[] lengths = {
  35. 2,
  36. 3,
  37. 3,
  38. 4,
  39. 5};
  40. boolean placed;
  41. Ship[] ships = new Ship[5];
  42. int start_orientation = Ship.randint(0,1);
  43. // start seed for ship
  44. ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],start_orientation == 0);
  45. for (int i = 1; i < 5; i++){
  46. length = lengths[i];
  47. placed = false;
  48. // System.out.println(length);
  49. while (!placed){
  50. Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,(start_orientation+i)%2 == 0);
  51. boolean intersects = false;
  52. boolean out_of_bounds = false;
  53. int rot = 0;
  54. // we need to clone since start is modified in place
  55. int[] stationary = cur_ship.getStart().clone();
  56. while ((rot < 4) && !placed) {
  57. cur_ship.print();
  58. // checks if ship is out of bounds
  59. out_of_bounds = !cur_ship.isInside(boundTL,boundBR);
  60. if (!out_of_bounds) {
  61. // checks if ship is intersecting with any that were previously placed on the board.
  62. for (int j = 0; j < i; j++ ){
  63. intersects = cur_ship.isIntersecting(ships[j]);
  64. // if ship intersects with one no need to check the rest
  65. if (intersects) {
  66. break;
  67. }
  68. }
  69. }
  70. // if something is wrong with the ship placement rotate it
  71. if (out_of_bounds || intersects) {
  72. cur_ship.rotate(stationary,1);
  73. // 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
  74. rot += 1;
  75. }
  76. // if nothing is wrong with it then mark it to be placed on the board
  77. else {
  78. ships[i] = cur_ship;
  79. placed = true;
  80. }
  81. }
  82. }
  83. }
  84. // we done, place those puppies on for real.
  85. for (int i = 0; i < 5; i++){
  86. ships[i].placeOnBoard(board);
  87. }
  88. }
  89. // method to make debug easier
  90. public static void printBoard(int[][] arr){
  91. System.out.print(" ");
  92. for (int i = 0; i < arr[0].length-1; i ++){
  93. System.out.print(i + " ");
  94. }
  95. System.out.println(arr[0].length-1);
  96. for (int row = 0; row < arr.length; row++){
  97. String s = row +": ";
  98. for (int col = 0; col < arr[row].length; col ++){
  99. if (arr[row][col] > 0){
  100. s += arr[row][col];
  101. }
  102. else {
  103. s += "|";
  104. }
  105. if (col < arr[row].length - 1){
  106. s += " ";
  107. }
  108. }
  109. System.out.println(s + "\n");
  110. }
  111. }
  112. public static void main(String[] args){
  113. int[][] testBoard = new int[10][10];
  114. randomizingBoard(testBoard);
  115. printBoard(testBoard);
  116. //printBoard(testBoard);
  117. }
  118. }