Advertisement
mattparks5855

Something Else

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