Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. class Ship {
  2. Position p;
  3. int size;
  4. boolean o; //true if ship is horizontal
  5. boolean[] destroyed;
  6. int totalDestroyed;
  7.  
  8. public boolean isDestroyed() {
  9. return totalDestroyed == size;
  10. }
  11.  
  12. public boolean destroyOneCoordinate(Position guess) {
  13. System.out.println(guess.r + "," + guess.c);
  14. if (o) {
  15. if (guess.r != p.r) {
  16. return false;
  17. }
  18. if (guess.c - p.c > size) {
  19. return false;
  20. }
  21. if (destroyed[guess.c - p.c]) {
  22. return false;
  23. }
  24. destroyed[guess.c - p.c] = true;
  25. totalDestroyed++;
  26. System.out.println("destroyed" + guess.r + "," + guess.c);
  27. return true;
  28. }
  29. else {
  30. if (guess.c != p.c) {
  31. return false;
  32. }
  33. if (guess.r - p.r > size) {
  34. return false;
  35. }
  36. if (destroyed[guess.r - p.r]) {
  37. return false;
  38. }
  39. System.out.println("destroyed" + guess.r + "," + guess.c);
  40. destroyed[guess.c - p.c] = true;
  41. totalDestroyed++;
  42. return true;
  43.  
  44. }
  45. }
  46.  
  47. Ship(Position p, int size, boolean o) {
  48. this.p = p;
  49. this.o = o;
  50. this.size = size;
  51. destroyed = new boolean[size];
  52. totalDestroyed = 0;
  53. }
  54. }
  55.  
  56. class Position {
  57. int r;
  58. int c;
  59. Position(int r, int c) {
  60. /*if (r <0 || c <0 || r > 9 || c >9) {
  61. throw new Exception("position not defined");
  62. }*/
  63. this.r = r;
  64. this.c = c;
  65. }
  66. }
  67.  
  68.  
  69.  
  70. class Solution {
  71. public static void main(String[] args) {
  72.  
  73. Ship a = new Ship(new Position(0,0), 2, true);
  74. Ship b = new Ship(new Position(1,0), 2, true);
  75. int turn = 0; // tell me who's turn
  76. Position[] guesses = new Position[5];
  77. guesses[0] = new Position(0, 0);
  78. guesses[1] = new Position(0, 0);
  79. guesses[2] = new Position(0, 1);
  80. guesses[3] = new Position(0, 1);
  81. guesses[4] = new Position(0, 2);
  82.  
  83.  
  84. int idx = 0;
  85. while (!a.isDestroyed() && !b.isDestroyed()) {
  86. idx ++;
  87. if (idx > 4) {
  88. break;
  89. }
  90. if (turn == 0) {
  91. a.destroyOneCoordinate(guesses[idx]);
  92. } else {
  93. b.destroyOneCoordinate(guesses[idx]);
  94. }
  95. turn = (turn + 1)%2;
  96. }
  97. if (a.isDestroyed()) {
  98. System.out.println("A destroyed");
  99. }
  100. if (b.isDestroyed()) {
  101. System.out.println("B destroyed");
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement