Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. public class Ocean
  2. {
  3. public Ship [][] ships=new Ship[10][10];
  4. public String [][] map=new String [10][10];
  5. public int shotsFired;
  6. public int hitCount;
  7. public int shipsSunk;
  8. public Ocean()
  9. {
  10. for(int i=0; i<=9;i++)
  11. {
  12. for(int j=0; j<=9;j++)
  13. {
  14. ships [i][j]=new EmptySea();
  15. map[i][j]=".";
  16. shotsFired=0;
  17. hitCount=0;
  18. shipsSunk = 0;
  19. }
  20. }
  21. }
  22. public void placeAllShipsRandomly()
  23. {
  24. int row=-1,column=-1;
  25. Battleship btlShip=new Battleship();
  26. do
  27. {
  28. int orient = rnd(1,4);
  29. if(orient%2==0)
  30. {
  31. btlShip.setHorizontal(true);
  32. row = rnd(0,9);
  33. column=rnd(0,6);
  34. }
  35. else
  36. {
  37. btlShip.setHorizontal(false);
  38. row = rnd(0,6);
  39. column=rnd(0,9);
  40. }
  41.  
  42. }while(!btlShip.okToPlaceShipAt(row,column,btlShip.isHorizontal(),this));
  43. btlShip.placeShipAt(row,column,btlShip.isHorizontal(),this);
  44.  
  45. Cruiser [] cruisers = new Cruiser[2];
  46. for(int i=0;i<2;i++)
  47. {
  48. do
  49. {
  50. int orient = rnd(1,4);
  51. if(orient%2==0)
  52. {
  53. cruisers[i].setHorizontal(true);
  54. row = rnd(0,9);
  55. column=rnd(0,7);
  56. }
  57. else
  58. {
  59. cruisers[i].setHorizontal(false);
  60. row = rnd(0,7);
  61. column=rnd(0,9);
  62. }
  63.  
  64. }while(!cruisers[i].okToPlaceShipAt(row,column,cruisers[i].isHorizontal(),this));
  65. cruisers[i].placeShipAt(row,column,cruisers[i].isHorizontal(),this);
  66. }
  67.  
  68. Destroyer [] destroyers = new Destroyer[3];
  69. for(int i=0;i<3;i++)
  70. {
  71. do
  72. {
  73. int orient = rnd(1,4);
  74. if(orient%2==0)
  75. {
  76. destroyers[i].setHorizontal(true);
  77. row = rnd(0,9);
  78. column=rnd(0,8);
  79. }
  80. else
  81. {
  82. destroyers[i].setHorizontal(false);
  83. row = rnd(0,8);
  84. column=rnd(0,9);
  85. }
  86.  
  87. }while(!destroyers[i].okToPlaceShipAt(row,column,destroyers[i].isHorizontal(),this));
  88. destroyers[i].placeShipAt(row,column,destroyers[i].isHorizontal(),this);
  89. }
  90.  
  91. Submarine [] submarines = new Submarine[4];
  92. for(int i=0;i<4;i++)
  93. {
  94. do
  95. {
  96. int orient = rnd(1,4);
  97. submarines[i].setHorizontal(true);
  98. row = rnd(0,9);
  99. column=rnd(0,8);
  100. }while(!submarines[i].okToPlaceShipAt(row,column,submarines[i].isHorizontal(),this));
  101. submarines[i].placeShipAt(row,column,submarines[i].isHorizontal(),this);
  102. }
  103. }
  104. public boolean isOccupied(int row,int column)
  105. {
  106. if(row>9||row<0||column>9||column<0)
  107. return false;
  108. else if(ships[row][column] instanceof EmptySea)
  109. return false;
  110. else
  111. return true;
  112. }
  113. public boolean shootAt(int row,int column)
  114. {
  115. if(ships[row][column]instanceof EmptySea)
  116. {
  117. shotsFired+=1;
  118. map[row][column]="-";
  119. return false;
  120. }
  121. else
  122. {
  123. shotsFired+=1;
  124. hitCount+=1;
  125. map[row][column]="s";
  126. if(ships[row][column].horizontal)
  127. {
  128. ships[row][column].hit[column-ships[row][column].getBowColumn()]=true;
  129. if(ships[row][column].isSunk())
  130. {
  131. for(int i=ships[row][column].getBowColumn();i<ships[row][column].getBowColumn()+ships[row][column].length;i++)
  132. {
  133. map[row][i]="x";
  134. }
  135. shipsSunk+=1;
  136. }
  137. }
  138. else
  139. {
  140. ships[row][column].hit[row-ships[row][column].getBowRow()]=true;
  141. if(ships[row][column].isSunk())
  142. {
  143. for(int i=ships[row][column].getBowRow();i<ships[row][column].getBowRow()+ships[row][column].length;i++)
  144. {
  145. map[i][column]="x";
  146. }
  147. shipsSunk+=1;
  148. }
  149. }
  150. return true;
  151. }
  152. }
  153. public int getShotsFires()
  154. {
  155. return shotsFired;
  156. }
  157. public int getHitCount()
  158. {
  159. return hitCount;
  160. }
  161. public int getShipsSunk()
  162. {
  163. return shipsSunk;
  164. }
  165. public boolean isGameOver()
  166. {
  167. if(shipsSunk==10)
  168. return true;
  169. else
  170. return false;
  171. }
  172. public Ship [][] getShipArray()
  173. {
  174. return this.ships;
  175. }
  176. public void print()
  177. {
  178. System.out.printf(" 0 1 2 3 4 5 6 7 8 9");
  179. for(int i=0; i<=9;i++)
  180. {
  181. System.out.printf(i+" ");
  182. for(int j=0; j<=9;j++)
  183. {
  184. System.out.printf(map[i][j].toString()+" ");
  185. }
  186. }
  187. }
  188. public static int rnd(int min, int max)
  189. {
  190. max -= min;
  191. return (int) (Math.random() * ++max) + min;
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement