Advertisement
mattparks5855

10:20 || TODO: Finish Them!

Feb 1st, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 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.2.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 xStart, yStart;
  26. private int seekingRuns = 0;
  27. private boolean topInitialized = false;
  28. private boolean bottomInitialized = false;
  29.  
  30. private boolean attacking = false, searching = false;
  31. private boolean positive, miss, vertical, horazontal;
  32. private int tilesLeft = 0;
  33.  
  34. @Override
  35. public void initialize(int numMatches, int numCaptains, String opponent) {
  36. generator = new Random();
  37. myFleet = new Fleet();
  38.  
  39. if (lastGameWon == true) {
  40. plan = lastPlan;
  41. } else {
  42. plan = generator.nextInt(3);
  43. }
  44.  
  45. lastPlan = plan;
  46.  
  47. if (plan == 0 || plan == 1) { // Random Placement
  48. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), AIRCRAFT_CARRIER)) {}
  49. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), BATTLESHIP)) {}
  50. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), DESTROYER)) {}
  51. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), SUBMARINE)) {}
  52. while (!myFleet.placeShip(generator.nextInt(10), generator.nextInt(10), generator.nextInt(2), PATROL_BOAT)) {}
  53. }
  54.  
  55. if (plan == 2) { // Edge Placement
  56. myFleet.placeShip(9, 3, VERTICAL, AIRCRAFT_CARRIER);
  57. myFleet.placeShip(3, 0, HORIZONTAL, BATTLESHIP);
  58. myFleet.placeShip(0, 5, VERTICAL, DESTROYER);
  59. myFleet.placeShip(4, 9, HORIZONTAL, SUBMARINE);
  60. myFleet.placeShip(0, 2, VERTICAL, PATROL_BOAT);
  61. }
  62.  
  63. if (plan == 3) { // Semi-Set-Location Plan
  64. while (!myFleet.placeShip(generator.nextInt(10), 5, generator.nextInt(2), AIRCRAFT_CARRIER));
  65. while (!myFleet.placeShip(generator.nextInt(10), 1, generator.nextInt(2), BATTLESHIP));
  66. while (!myFleet.placeShip(5, 7, generator.nextInt(2), DESTROYER));
  67. while (!myFleet.placeShip(3, 2, generator.nextInt(2), SUBMARINE));
  68. while (!myFleet.placeShip(1, 8, generator.nextInt(2), PATROL_BOAT));
  69. }
  70. }
  71.  
  72. @Override
  73. public Fleet getFleet() {
  74. return myFleet;
  75. }
  76.  
  77. public int DestroyShip() {
  78. // TODO SEARCH AND DESTROY!
  79. return 0;
  80. }
  81.  
  82. public int CoordsTop(int yStart) {
  83. if (!topInitialized) {
  84. y = yStart;
  85. x = 0;
  86. topInitialized = true;
  87. } else if (x >= 9 || y >= 9) {
  88. topInitialized = false;
  89. seekingRuns++;
  90. makeAttack();
  91. } else {
  92. y++;
  93. x++;
  94. }
  95.  
  96. return 0;
  97. }
  98.  
  99. public int CoordsBottom(int xStart) {
  100. if (!bottomInitialized) {
  101. x = xStart;
  102. y = 0;
  103. bottomInitialized = true;
  104. } else if (y >= 9 || x >= 9) {
  105. bottomInitialized = false;
  106. seekingRuns++;
  107. makeAttack();
  108. } else {
  109. x++;
  110. y++;
  111. }
  112.  
  113. return 0;
  114. }
  115.  
  116. @Override
  117. public Coordinate makeAttack() {
  118. if (attacking)
  119. DestroyShip();
  120. else if (seekingRuns == 0)
  121. CoordsTop(0);
  122. else if (seekingRuns == 1)
  123. CoordsBottom(6);
  124. else if (seekingRuns == 2)
  125. CoordsTop(6);
  126. else if (seekingRuns == 3)
  127. CoordsBottom(4);
  128. else if (seekingRuns == 4)
  129. CoordsTop(4);
  130. else if (seekingRuns == 5)
  131. CoordsBottom(8);
  132. else if (seekingRuns == 6)
  133. CoordsTop(8);
  134. else if (seekingRuns == 7)
  135. CoordsBottom(2);
  136. else if (seekingRuns == 8)
  137. CoordsTop(2);
  138.  
  139. return new Coordinate(x + xModifyer, y + yModifyer);
  140. }
  141.  
  142.  
  143. @Override
  144. public void resultOfAttack(int result) {
  145. String shipHitType = null;
  146.  
  147. if (!attacking && result == HIT_PATROL_BOAT) {
  148. shipHitType = "PATROL_BOAT";
  149. attacking = true;
  150. searching = true;
  151. xStart = x;
  152. yStart = y;
  153. } if (!attacking && result == HIT_DESTROYER) {
  154. shipHitType = "DESTROYER";
  155. attacking = true;
  156. searching = true;
  157. xStart = x;
  158. yStart = y;
  159. } if (!attacking && result == HIT_SUBMARINE) {
  160. shipHitType = "SUBMARINE";
  161. attacking = true;
  162. searching = true;
  163. xStart = x;
  164. yStart = y;
  165. } if (!attacking && result == HIT_BATTLESHIP) {
  166. shipHitType = "BATTLESHIP";
  167. attacking = true;
  168. searching = true;
  169. xStart = x;
  170. yStart = y;
  171. } if (!attacking && result == HIT_AIRCRAFT_CARRIER) {
  172. shipHitType = "AIRCRAFT_CARRIER";
  173. attacking = true;
  174. searching = true;
  175. xStart = x;
  176. yStart = y;
  177. } if (searching && result != MISS) {
  178. if (xModifyer == 1) {
  179. horazontal = true;
  180. positive = true;
  181. } else if (xModifyer == -1) {
  182. horazontal = true;
  183. positive = false;
  184. } else if (yModifyer == 1) {
  185. vertical = true;
  186. positive = true;
  187. } else if (yModifyer == -1) {
  188. vertical = true;
  189. positive = false;
  190. }
  191. } if (searching && result == MISS) {
  192. miss = true;
  193. } if (!searching && result == MISS) {
  194. if (positive) {
  195. positive = false;
  196. } else {
  197. positive = true;
  198. }
  199.  
  200. xModifyer = 0;
  201. yModifyer = 0;
  202. } if (result == SUNK_PATROL_BOAT || result == SUNK_DESTROYER || result == SUNK_SUBMARINE || result == SUNK_BATTLESHIP || result == SUNK_AIRCRAFT_CARRIER) {
  203. attacking = false;
  204. searching = false;
  205. horazontal = false;
  206. vertical = false;
  207. xModifyer = 0;
  208. yModifyer = 0;
  209. }
  210.  
  211. switch (shipHitType) {
  212. case "PATROL_BOAT":
  213. tilesLeft = 2 - 1;
  214. break;
  215. case "DESTROYER":
  216. tilesLeft = 3 - 1;
  217. break;
  218. case "BATTLESHIP":
  219. tilesLeft = 4 - 1;
  220. break;
  221. case "SUBMARINE":
  222. tilesLeft = 3 - 1;
  223. break;
  224. case "AIRCRAFT_CARRIER":
  225. tilesLeft = 5 - 1;
  226. break;
  227. }
  228. }
  229.  
  230. @Override
  231. public void opponentAttack(Coordinate coord) {
  232. }
  233.  
  234. @Override
  235. public void resultOfGame(int result) {
  236. if (result == WON) {
  237. lastGameWon = true;
  238. } else if (result == LOST) {
  239. lastGameWon = false;
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement