Advertisement
mattparks5855

6:39

Feb 1st, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4. * <p>Check M8 places its ships in the pattern more likely to last the longest, it fires starting at a coordinate and going in a diagnol
  5. * line until it hits a ship or a board edge. If it hits a ship it searches for the rest of it until it is sunk. When it hits a edge it
  6. * looks for more instructions to run through.</p>
  7. *
  8. * <p>Instruction coordinates are ordered from the highest probability of hitting a ship, to the lowest. The probability is based off of
  9. * previous matches. Placement of the ships are based off of the last games results, if the last game was won the same layout is keeped,
  10. * if it lost it goes to a new layout.</p>
  11. *
  12. * @author Matthew Albrecht
  13. * @version 2.1.15
  14. *
  15. */
  16.  
  17. public class CheckM8 implements Captain {
  18. private boolean lastGameWon = false;
  19. private Random generator;
  20. private Fleet myFleet;
  21. private int lastPlan, plan;
  22.  
  23. private int x, y;
  24. private int xModifyer, yModifyer;
  25. private int seekingRuns = 0;
  26. private boolean topInitialized = false;
  27. private boolean bottomInitialized = false;
  28.  
  29. @Override
  30. public void initialize(int numMatches, int numCaptains, String opponent) {
  31. generator = new Random();
  32. myFleet = new Fleet();
  33.  
  34. if (lastGameWon == true) {
  35. plan = lastPlan;
  36. } else {
  37. plan = generator.nextInt(3);
  38. }
  39.  
  40. lastPlan = plan;
  41.  
  42. if (plan == 0 || plan == 1) { // Random Placement
  43. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), AIRCRAFT_CARRIER)) {}
  44. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), BATTLESHIP)) {}
  45. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), DESTROYER)) {}
  46. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), SUBMARINE)) {}
  47. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), PATROL_BOAT)) {}
  48. }
  49.  
  50. if (plan == 2) { // Edge Placement
  51. myFleet.placeShip(9, 3, VERTICAL, AIRCRAFT_CARRIER);
  52. myFleet.placeShip(3, 0, HORIZONTAL, BATTLESHIP);
  53. myFleet.placeShip(0, 5, VERTICAL, DESTROYER);
  54. myFleet.placeShip(4, 9, HORIZONTAL, SUBMARINE);
  55. myFleet.placeShip(0, 2, VERTICAL, PATROL_BOAT);
  56. }
  57.  
  58. if (plan == 3) { // Semi-Set-Location Plan
  59. while (!myFleet.placeShip(generator.nextInt(10), 5, generator.nextInt(2), AIRCRAFT_CARRIER));
  60. while (!myFleet.placeShip(generator.nextInt(10), 1, generator.nextInt(2), BATTLESHIP));
  61. while (!myFleet.placeShip(5, 7, generator.nextInt(2), DESTROYER));
  62. while (!myFleet.placeShip(3, 2, generator.nextInt(2), SUBMARINE));
  63. while (!myFleet.placeShip(1, 8, generator.nextInt(2), PATROL_BOAT));
  64. }
  65. }
  66.  
  67. @Override
  68. public Fleet getFleet() {
  69. return myFleet;
  70. }
  71.  
  72. public int CoordsTop(int startY) {
  73. if (!topInitialized) {
  74. y = startY;
  75. x = 0;
  76. topInitialized = true;
  77. } else if (x >= 9 || y >= 9) {
  78. topInitialized = false;
  79. seekingRuns++;
  80.  
  81. makeAttack();
  82. } else {
  83. y++;
  84. x++;
  85. }
  86.  
  87. return 0;
  88. }
  89.  
  90. public int CoordsBottom(int startX) {
  91. if (!bottomInitialized) {
  92. x = startX;
  93. y = 0;
  94. bottomInitialized = true;
  95. } else if (y >= 9 || x >= 9) {
  96. bottomInitialized = false;
  97. seekingRuns++;
  98.  
  99. makeAttack();
  100. } else {
  101. x++;
  102. y++;
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108. @Override
  109. public Coordinate makeAttack() {
  110. // TODO: Diagnol lines created by probability.
  111. if (seekingRuns == 0)
  112. CoordsTop(0);
  113. else if (seekingRuns == 1)
  114. CoordsBottom(6);
  115. else if (seekingRuns == 2)
  116. CoordsTop(6);
  117. else if (seekingRuns == 3)
  118. CoordsBottom(4);
  119. else if (seekingRuns == 4)
  120. CoordsTop(4);
  121. else if (seekingRuns == 5)
  122. CoordsBottom(8);
  123. else if (seekingRuns == 6)
  124. CoordsTop(8);
  125. else if (seekingRuns == 7)
  126. CoordsBottom(2);
  127. else if (seekingRuns == 8)
  128. CoordsTop(2);
  129.  
  130. return new Coordinate(x + xModifyer, y + yModifyer);
  131. }
  132.  
  133. @Override
  134. public void resultOfAttack(int result) {
  135. }
  136.  
  137. @Override
  138. public void opponentAttack(Coordinate coord) {
  139. }
  140.  
  141. @Override
  142. public void resultOfGame(int result) {
  143. if (result == WON) {
  144. lastGameWon = true;
  145. } else if (result == LOST) {
  146. lastGameWon = false;
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement