Advertisement
xFazz

Ocean

Nov 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. public class Ocean {
  2.  
  3. // private instance variables
  4. private Boat[] boats;
  5. private int[][] ocean;
  6. private int boatIndex;
  7. private int totalBoats;
  8.  
  9. // constructor
  10. public Ocean(int numberOfBoats, in oceanSize) {
  11. boatIndex = 0
  12. totalBoats = numberOfBoats;
  13. boats = new Boat[totalBoats];
  14. ocean = new int[oceanSize][oceanSize];
  15. // initialize ocean to having no boats (-1)
  16. for (int r = 0; r < ocean.length; r++) {
  17. for (int c = 0; c < ocean[r].length; c++) {
  18. ocean[r][c] = -1;
  19. }
  20. }
  21.  
  22. }
  23.  
  24. public void placeBoat(String boatName, String direction, Position pos) throws Exception {
  25. // create temp boat
  26. Boat tempBoat = new Boat(boatNmae, pos, direction)
  27. int posRow = pos.rowIndex()
  28. int posCol = pos.columnIndex();
  29. int maxCol = ocean[0].length - 1;
  30. int maxRow = ocean.length - 1;
  31. boolean horizontal = (direction.substring(0,1).toUpperCase().equals("H"));
  32.  
  33. // check if starting position outside of ocean
  34. if (posRow < 0 || posRow > maxRow || posCol < 0 || posCol > maxCol)
  35. throw(new Exception("Starting position of Boat out of ocean"));
  36.  
  37. // check if ending position outside of ocean
  38. if (horizontal) {
  39. // horizontal placement
  40. if (posCol + tempBoat.size() - 1 > maxCol)
  41. throw(new Exception("Boat placement goes of ocean grid (col)"));
  42. } else {
  43. //vertical placement
  44. if (posRow + tempBoat.size() - 1 > maxRow)
  45. throw(new Exception("Boat placement goes of ocean grid (row)"));
  46. }
  47.  
  48. // check if boat placement overlaps another boat
  49. if (horizontal {
  50. // check columns for overlap
  51. for (int c = posCol; c < posCol + tempBoat.size(); c++) {
  52. if (ocean[posRow][c] != -1) {
  53. throw(new Exception("Boat placement overlaps existing boat (" +
  54. posRow + "," + posCol));
  55. }
  56. }
  57. } else {
  58. for (int r = posRow; r < posRow + tempBoat.size(); r++) {
  59. if (ocean[r][posCol]) != -1) {
  60. throw (new Exception("Boat placememnt overlaps exisiting boat (" +
  61. posRow + "," + posCol + ")"));
  62. }
  63.  
  64. // create my boat in the ocean - if boat to place
  65. if (boatIndex < totalBoats) {
  66. boats[boatsIndex] = new Boat(boaName, pos, direction);
  67. if (horizontal) {
  68. // mark ocean for horizontal boat
  69. for (int c = posCol; c < posCol + boats[boatIndex].size(); c++) {
  70. ocean[posRow][c] = boatIndex;
  71. }
  72. } else {
  73. // mark ocean for vertical boat
  74. for (int r = posRow; r < posRow + boats[boatIndx].size(); r++) {
  75. ocean[r][posCol] = boatIndex;
  76. }
  77. }
  78. // increment number of boats placed
  79. boatIndex++;
  80. } else {
  81. throw(new Exception("too many boats!"));
  82. }
  83.  
  84. public char boatInitial(Position pos) {
  85. if (ocean[pos.rowIndex)][pos.columnIndex()] != -1)
  86. return boats[ocean[pos.rowIndex()][pos.columnIndex()]].abbreviation()
  87. else
  88. return '?';
  89. }
  90.  
  91. public String boatName(Position pos) {
  92. if (ocean[pos.rowIndex()][pos.columnIndex()] != -1
  93. return boats[ocean[pos.rowIndex()][pos.columnIndex()]].name();
  94. else
  95. return "??????"
  96. }
  97.  
  98. public boolean allSunk() {
  99. for (int i = 0; i < boats.length; i++)
  100. if (!boats [i].sunk())
  101. return false;
  102. return true;
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement