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.

105 lines
3.0 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. while (!placed){
  45. Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,i%2 == 0);
  46. boolean intersects = false;
  47. int rot = 0;
  48. int[] stationary = cur_ship.getStart().clone();
  49. while ((rot < 4) && !placed) {
  50. // ensures ship doesn't intersect with any that were previously placed
  51. for (int j = 0; j < i; j++){
  52. intersects = cur_ship.isIntersecting(ships[j]);
  53. if (intersects){
  54. cur_ship.rotate(stationary,1);
  55. rot += 1;
  56. break;
  57. }
  58. }
  59. // ensures ship is still in bounds after rotation
  60. if (!cur_ship.isInside(boundTL,boundBR)) {
  61. cur_ship.rotate(stationary,1);
  62. rot += 1;
  63. }
  64. // nothing wrong with ship placement? place it!
  65. else if (!intersects){
  66. ships[i] = cur_ship;
  67. placed = true;
  68. }
  69. }
  70. }
  71. }
  72. // we done, place those puppies on for real.
  73. for (int i = 0; i < 5; i++){
  74. ships[i].print();
  75. ships[i].placeOnBoard(board);
  76. }
  77. }
  78. public static void printBoard(int[][] arr){
  79. for (int row = 0; row < arr.length; row++){
  80. String s = "";
  81. for (int col = 0; col < arr[row].length; col ++){
  82. s += arr[row][col];
  83. if (col < arr[row].length - 1){
  84. s += " ";
  85. }
  86. }
  87. System.out.println(s + "\n");
  88. }
  89. }
  90. public static void main(String[] args){
  91. int[][] testBoard = new int[10][10];
  92. randomizingBoard(testBoard);
  93. printBoard(testBoard);
  94. }
  95. }