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.

123 lines
3.4 KiB

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