mattparks5855

Early Morning Coding

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