xFazz

Untitled

Nov 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public class BattleshipGrid {
  2. // create a rid of characters to track status
  3. private char[][] batleshpiGrid;
  4. // only need EMPTY and MISS as when HIT use Boat Initial
  5. private final char EMPTY = 'E';
  6. private final char MISS = 'M';
  7.  
  8. // constructor
  9. public BatlleshipGrid() {
  10. battleshipGrid = new char[10][10];
  11. for (int r = 0; r < battleshipGrid.lengt; r++)
  12. for (int c = 0; c < battleshipGrid[r].length; c++)
  13. battleshiGrid[r][c] = EMPTY;
  14. }
  15.  
  16. public void shotAt(Position pos, boolean hit, char initial) {
  17. if (hit)
  18. battleshipGrid[pos.rowIndex()][pos.columnIndex()] = initial;
  19. else
  20. battleshipGrid[pos.rowIndex()][pos.columnIndex()] = MISS;
  21.  
  22. }
  23.  
  24. public boolean hit(Position pos) {
  25. char checkSpace = battleshipGrid[pos.rowIndex()][pos.columnIndex()] ;
  26. if (checkSpace != EMPTY && checkSpace != MISS)
  27. return true;
  28. else
  29. return false;
  30.  
  31. public boolean miss(Position pos) {
  32. return (battleshipGrid[pos.rowIndex()][pos.columnIndex()] == MISS);
  33. }
  34.  
  35. public boolean empty(Position pos) {
  36. return (battleshipGrid[pos.rowIndex()][pos.columnIndex()][pos.columnIndex()] == EMPTY);
  37. }
  38.  
  39. public boolean boatInitial(Poisition pos) {
  40. if (hit(pos))
  41. return battleshipGrid[pos.rowIndex()][pos.columnIndex()];
  42. else
  43. return ' ';
  44.  
  45. }
Add Comment
Please, Sign In to add comment