Advertisement
mattparks5855

Swag 1

Feb 3rd, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Random;
  3.  
  4. /**
  5. * <p>
  6. * Check M8 places its ships in the pattern more likely to last the longest, it
  7. * fires starting at a coordinate and going in a diagnol line until it hits a
  8. * ship or a board edge. If it hits a ship it searches for the rest of it until
  9. * it is sunk. When it hits a edge it looks for more instructions to run
  10. * through.
  11. * </p>
  12. *
  13. * <p>
  14. * Instruction coordinates are ordered from the highest probability of hitting a
  15. * ship, to the lowest. The probability is based off of previous matches.
  16. * Placement of the ships are based off of the last games results, if the last
  17. * game was won the same layout is keeped, if it lost it goes to a new layout.
  18. * </p>
  19. *
  20. * @author Matthew Albrecht
  21. * @version 2.2.15
  22. *
  23. */
  24.  
  25. public class CaptainCheckM8 implements Captain {
  26. private boolean LastGameWon = false;
  27. private Random Generator;
  28. private Fleet MyFleet;
  29. private int LastPlan, Plan;
  30.  
  31. private boolean Seeking = true;
  32. private int x, y;
  33. private int xModifyer, yModifyer;
  34. private int SeekingRuns;
  35. private boolean TopInitialized = false;
  36. private boolean BottomInitialized = false;
  37.  
  38. boolean up, down, left, right, hit;
  39.  
  40. @Override
  41. public void initialize(int numMatches, int numCaptains, String opponent) {
  42. Generator = new Random();
  43. MyFleet = new Fleet();
  44.  
  45. up = false;
  46. down = false;
  47. left = false;
  48. right = false;
  49. hit = false;
  50.  
  51. if (LastGameWon == true) { // If the last game was won, use the last games game plan.
  52. Plan = LastPlan;
  53. } else { // If the last game was lost use a new ship layout.
  54. Plan = Generator.nextInt(3);
  55. }
  56.  
  57. LastPlan = Plan;
  58.  
  59. if (Plan == 0 || Plan == 1) { // Random Placement.
  60. while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), AIRCRAFT_CARRIER));
  61. while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), BATTLESHIP));
  62. while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), DESTROYER));
  63. while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), SUBMARINE));
  64. while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), PATROL_BOAT));
  65. }
  66. if (Plan == 2) { // Edge Placement.
  67. MyFleet.placeShip(9, 3, VERTICAL, AIRCRAFT_CARRIER);
  68. MyFleet.placeShip(3, 0, HORIZONTAL, BATTLESHIP);
  69. MyFleet.placeShip(0, 5, VERTICAL, DESTROYER);
  70. MyFleet.placeShip(4, 9, HORIZONTAL, SUBMARINE);
  71. MyFleet.placeShip(0, 2, VERTICAL, PATROL_BOAT);
  72. }
  73. if (Plan == 3) { // Semi-Set-Location Placement.
  74. while (!MyFleet.placeShip(Generator.nextInt(10), 5, Generator.nextInt(2), AIRCRAFT_CARRIER));
  75. while (!MyFleet.placeShip(Generator.nextInt(10), 1, Generator.nextInt(2), BATTLESHIP));
  76. while (!MyFleet.placeShip(5, 7, Generator.nextInt(2), DESTROYER));
  77. while (!MyFleet.placeShip(3, 2, Generator.nextInt(2), SUBMARINE));
  78. while (!MyFleet.placeShip(1, 8, Generator.nextInt(2), PATROL_BOAT));
  79. }
  80. }
  81.  
  82. @Override
  83. public Fleet getFleet() {
  84. return MyFleet;
  85. }
  86.  
  87. public int FinishShip() {
  88. if (right) {
  89. if (x + xModifyer < 10) {
  90. xModifyer++;
  91. } else {
  92. right = false;
  93. }
  94. } else if (left) {
  95. if (x - xModifyer > 0) {
  96. xModifyer--;
  97. } else {
  98. left = false;
  99. }
  100. } else if (up) {
  101. if (y + yModifyer < 10) {
  102. yModifyer++;
  103. } else {
  104. up = false;
  105. }
  106. } else if (down) {
  107. if (y - yModifyer > 0) {
  108. yModifyer--;
  109. } else {
  110. down = false;
  111. }
  112. }
  113.  
  114. return 0;
  115. }
  116.  
  117. public int CoordsTop(int yStart) { // Called when a line is going to created off of the y-axis to the x-axis.
  118. if (!TopInitialized) {
  119. y = yStart;
  120. x = 0;
  121. TopInitialized = true;
  122. } else if (x >= 9 || y >= 9) {
  123. TopInitialized = false;
  124. SeekingRuns++;
  125. makeAttack();
  126. } else {
  127. y++;
  128. x++;
  129. }
  130.  
  131. return 0;
  132. }
  133.  
  134. public int CoordsBottom(int xStart) { // Called when a line is going to created off of the x-axis to the y-axis.
  135. if (!BottomInitialized) {
  136. x = xStart;
  137. y = 0;
  138. BottomInitialized = true;
  139. } else if (y >= 9 || x >= 9) {
  140. BottomInitialized = false;
  141. SeekingRuns++;
  142. makeAttack();
  143. } else {
  144. x++;
  145. y++;
  146. }
  147.  
  148. return 0;
  149. }
  150.  
  151. @Override
  152. public Coordinate makeAttack() { // Coordinates attacks on the enemy's ships, lines created by there probability of hitting a ship.
  153. if (!Seeking) {
  154. FinishShip();
  155. } else if (SeekingRuns == 0) {
  156. CoordsTop(0);
  157. } else if (SeekingRuns == 1) {
  158. CoordsBottom(6);
  159. } else if (SeekingRuns == 2) {
  160. CoordsTop(6);
  161. } else if (SeekingRuns == 3) {
  162. CoordsBottom(4);
  163. } else if (SeekingRuns == 4) {
  164. CoordsTop(4);
  165. } else if (SeekingRuns == 5) {
  166. CoordsBottom(8);
  167. } else if (SeekingRuns == 6) {
  168. CoordsTop(8);
  169. } else if (SeekingRuns == 7) {
  170. CoordsBottom(2);
  171. } else if (SeekingRuns == 8) {
  172. CoordsTop(2);
  173. }
  174.  
  175. return new Coordinate(x + xModifyer, y + yModifyer);
  176. }
  177.  
  178. @Override
  179. public void resultOfAttack(int result) {
  180. // TODO: One it has hit the ship and been firing at it, and it missed, it should look the opposite direction on the same axis the other parts were found.
  181.  
  182. if (result == HIT_PATROL_BOAT || result == HIT_DESTROYER || result == HIT_SUBMARINE || result == HIT_BATTLESHIP || result == HIT_AIRCRAFT_CARRIER) {
  183. Seeking = false;
  184.  
  185. if (hit) {
  186. up = true;
  187. down = true;
  188. left = true;
  189. right = true;
  190. }
  191.  
  192. hit = true;
  193.  
  194. if (x + xModifyer >= 10) {
  195. right = false;
  196. } else if (x - xModifyer < 0) {
  197. left = false;
  198. } if (y + yModifyer >= 10) {
  199. up = false;
  200. } else if (y - yModifyer < 0) {
  201. down = false;
  202. }
  203.  
  204. } else if (result == SUNK_PATROL_BOAT || result == SUNK_DESTROYER || result == SUNK_SUBMARINE || result == SUNK_BATTLESHIP || result == SUNK_AIRCRAFT_CARRIER) {
  205. yModifyer = 0;
  206. xModifyer = 0;
  207. Seeking = true;
  208.  
  209. hit = false;
  210. up = false;
  211. down = false;
  212. left = false;
  213. right = false;
  214. } else {
  215. if (hit) {
  216. yModifyer = 0; //
  217. xModifyer = 0; //
  218.  
  219. if (right) {
  220. right = false;
  221. left = true; //
  222. } else if (left) {
  223. left = false;
  224. right = true; //
  225. } else if (up) {
  226. up = false;
  227. down = true; //
  228. } else if (down) {
  229. down = false;
  230. up = true; //
  231. }
  232. }
  233. }
  234. }
  235.  
  236. @Override
  237. public void opponentAttack(Coordinate coord) {
  238. }
  239.  
  240. @Override
  241. public void resultOfGame(int result) { // Logs the results of the match.
  242. if (result == WON) {
  243. LastGameWon = true;
  244. } else if (result == LOST) {
  245. LastGameWon = false;
  246. }
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement