Advertisement
Guest User

sadgf

a guest
Feb 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. public class Grid
  2. {
  3. // Write your Grid class here
  4. private Location[][] grid;
  5. public static final int NUM_ROWS = 10;
  6. public static final int NUM_COLS = 10;
  7. private String[][] board;
  8. private String[][] ships;
  9. private String[][] shipBoard;
  10. private int [][] statusBoard;
  11.  
  12. public Grid() {
  13. board = new String[NUM_ROWS][NUM_COLS];
  14. for(int i = 0; i < board.length; i ++) {
  15. for(int j = 0; j < board[0].length; j ++) {
  16. board[i][j] = "- ";
  17. }
  18. }
  19.  
  20. ships = new String[NUM_ROWS][NUM_COLS];
  21. for(int i = 0; i < ships.length; i ++) {
  22. for(int j = 0; j < board[0].length; j ++) {
  23. ships[i][j] = "O";
  24. }
  25. }
  26.  
  27. statusBoard = new int[NUM_ROWS][NUM_COLS];
  28. for(int i = 0; i < ships.length; i ++) {
  29. for(int j = 0; j < board[0].length; j ++) {
  30. ships[i][j] = "0";
  31. }
  32. }
  33.  
  34. shipBoard = new String[NUM_ROWS][NUM_COLS];
  35. for(int i = 0; i < shipBoard.length; i ++) {
  36. for(int j = 0; j < shipBoard[0].length; j ++) {
  37. shipBoard[i][j] = "- ";
  38. }
  39. }
  40. }
  41.  
  42. public void markHit(int row, int col) {
  43. board[row][col] = "X ";
  44. statusBoard[row][col] = 1;
  45. }
  46.  
  47. public void markMiss(int row, int col) {
  48. board[row][col] = "O ";
  49. statusBoard[row][col] = 2;
  50. }
  51.  
  52. public void setStatus(int row, int col, int status) {
  53. statusBoard[row][col] = status;
  54. }
  55.  
  56. public int getStatus(int row, int col) {
  57. return statusBoard[row][col];
  58. }
  59.  
  60. public boolean alreadyGuessed(int row, int col) {
  61. return true;
  62. }
  63.  
  64. public void setShip(int row, int col, boolean val) {
  65. ships[row][col] = " X ";
  66. }
  67.  
  68. public boolean hasShip(int row, int col) {
  69. return true;
  70. }
  71.  
  72. public Location get(int row, int col) {
  73. Location l = new Location();
  74. return l;
  75. }
  76.  
  77. public int numRows() {
  78. return NUM_ROWS;
  79. }
  80.  
  81. public int numCols() {
  82. return NUM_COLS;
  83. }
  84.  
  85. public void printStatus() {
  86. for(int j = 0; j < board[0].length - 1; j ++) {
  87. System.out.print(j + 1 + " ");
  88. }
  89. System.out.print("10");
  90. System.out.print("\nA ");
  91. for(int j = 0; j < board[0].length; j ++) {
  92. System.out.print(board[0][j]);
  93. }
  94. System.out.print("\nB ");
  95. for(int j = 0; j < board[0].length; j ++) {
  96. System.out.print(board[1][j]);
  97. }
  98. System.out.print("\nC ");
  99. for(int j = 0; j < board[0].length; j ++) {
  100. System.out.print(board[2][j]);
  101. }
  102. System.out.print("\nD ");
  103. for(int j = 0; j < board[0].length; j ++) {
  104. System.out.print(board[3][j]);
  105. }
  106. System.out.print("\nE ");
  107. for(int j = 0; j < board[0].length; j ++) {
  108. System.out.print(board[4][j]);
  109. }
  110. System.out.print("\nF ");
  111. for(int j = 0; j < board[0].length; j ++) {
  112. System.out.print(board[5][j]);
  113. }
  114. System.out.print("\nG ");
  115. for(int j = 0; j < board[0].length; j ++) {
  116. System.out.print(board[6][j]);
  117. }
  118. System.out.print("\nH ");
  119. for(int j = 0; j < board[0].length; j ++) {
  120. System.out.print(board[7][j]);
  121. }
  122. System.out.print("\nI ");
  123. for(int j = 0; j < board[0].length; j ++) {
  124. System.out.print(board[8][j]);
  125. }
  126. System.out.print("\nJ ");
  127. for(int j = 0; j < board[0].length; j ++) {
  128. System.out.print(board[9][j]);
  129. }
  130. }
  131.  
  132. public void printShips() {
  133. for(int i = 0; i < ships.length; i ++) {
  134. for(int j = 0; j < shipBoard[0].length; j ++) {
  135. if(ships[i][j].equals(" X ")) {
  136. shipBoard[i][j] = "X ";
  137. }
  138. }
  139. }
  140.  
  141. //////////////////////////////////////////////////////////////////////
  142.  
  143. for(int j = 0; j < board[0].length - 1; j ++) {
  144. System.out.print(j + 1 + " ");
  145. }
  146. System.out.print("10");
  147. System.out.print("\nA ");
  148. for(int j = 0; j < board[0].length; j ++) {
  149. System.out.print(shipBoard[0][j]);
  150. }
  151. System.out.print("\nB ");
  152. for(int j = 0; j < board[0].length; j ++) {
  153. System.out.print(shipBoard[1][j]);
  154. }
  155. System.out.print("\nC ");
  156. for(int j = 0; j < board[0].length; j ++) {
  157. System.out.print(shipBoard[2][j]);
  158. }
  159. System.out.print("\nD ");
  160. for(int j = 0; j < board[0].length; j ++) {
  161. System.out.print(shipBoard[3][j]);
  162. }
  163. System.out.print("\nE ");
  164. for(int j = 0; j < board[0].length; j ++) {
  165. System.out.print(shipBoard[4][j]);
  166. }
  167. System.out.print("\nF ");
  168. for(int j = 0; j < board[0].length; j ++) {
  169. System.out.print(shipBoard[5][j]);
  170. }
  171. System.out.print("\nG ");
  172. for(int j = 0; j < board[0].length; j ++) {
  173. System.out.print(shipBoard[6][j]);
  174. }
  175. System.out.print("\nH ");
  176. for(int j = 0; j < board[0].length; j ++) {
  177. System.out.print(shipBoard[7][j]);
  178. }
  179. System.out.print("\nI ");
  180. for(int j = 0; j < board[0].length; j ++) {
  181. System.out.print(shipBoard[8][j]);
  182. }
  183. System.out.print("\nJ ");
  184. for(int j = 0; j < board[0].length; j ++) {
  185. System.out.print(shipBoard[9][j]);
  186. }
  187.  
  188. /////////////////////////////////////////////////////////////
  189. }
  190.  
  191. public void addShip(Ship s) {
  192. ships[s.getRow()][s.getCol()] = " X ";
  193. int counter = 0;
  194. int length = s.getLength();
  195. int direction = s.getDirection();
  196. if(direction == 0) {
  197. while(counter < length) {
  198. ships[s.getRow()][s.getCol() + counter] = " X ";
  199. counter ++;
  200. }
  201. } else if(direction == 1) {
  202. while(counter < length) {
  203. ships[s.getRow() + counter][s.getCol()] = " X ";
  204. counter ++;
  205. }
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement