Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Random;
- /**
- * <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 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 looks for more instructions to run
- * through.
- * </p>
- *
- * <p>
- * Instruction coordinates are ordered from the highest probability of hitting a
- * ship, to the lowest. The probability is based off of 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, if it lost it goes to a new layout.
- * </p>
- *
- * @author Matthew Albrecht
- * @version 2.2.15
- *
- */
- public class CaptainCheckM8 implements Captain {
- private boolean LastGameWon = false;
- private Random Generator;
- private Fleet MyFleet;
- private int LastPlan, Plan;
- private boolean Seeking = true;
- private int x, y;
- private int xModifyer, yModifyer;
- private int SeekingRuns;
- private boolean TopInitialized = false;
- private boolean BottomInitialized = false;
- boolean up, down, left, right, hit;
- @Override
- public void initialize(int numMatches, int numCaptains, String opponent) {
- Generator = new Random();
- MyFleet = new Fleet();
- up = false;
- down = false;
- left = false;
- right = false;
- hit = false;
- if (LastGameWon == true) { // If the last game was won, use the last games game plan.
- Plan = LastPlan;
- } else { // If the last game was lost use a new ship layout.
- Plan = Generator.nextInt(3);
- }
- LastPlan = Plan;
- if (Plan == 0 || Plan == 1) { // Random Placement.
- while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), AIRCRAFT_CARRIER));
- while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), BATTLESHIP));
- while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), DESTROYER));
- while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), SUBMARINE));
- while (!MyFleet.placeShip(Generator.nextInt(10), Generator.nextInt(10), Generator.nextInt(2), PATROL_BOAT));
- }
- if (Plan == 2) { // Edge Placement.
- MyFleet.placeShip(9, 3, VERTICAL, AIRCRAFT_CARRIER);
- MyFleet.placeShip(3, 0, HORIZONTAL, BATTLESHIP);
- MyFleet.placeShip(0, 5, VERTICAL, DESTROYER);
- MyFleet.placeShip(4, 9, HORIZONTAL, SUBMARINE);
- MyFleet.placeShip(0, 2, VERTICAL, PATROL_BOAT);
- }
- if (Plan == 3) { // Semi-Set-Location Placement.
- while (!MyFleet.placeShip(Generator.nextInt(10), 5, Generator.nextInt(2), AIRCRAFT_CARRIER));
- while (!MyFleet.placeShip(Generator.nextInt(10), 1, Generator.nextInt(2), BATTLESHIP));
- while (!MyFleet.placeShip(5, 7, Generator.nextInt(2), DESTROYER));
- while (!MyFleet.placeShip(3, 2, Generator.nextInt(2), SUBMARINE));
- while (!MyFleet.placeShip(1, 8, Generator.nextInt(2), PATROL_BOAT));
- }
- }
- @Override
- public Fleet getFleet() {
- return MyFleet;
- }
- public int FinishShip() {
- if (right) {
- if (x + xModifyer < 10) {
- xModifyer++;
- } else {
- right = false;
- }
- } else if (left) {
- if (x - xModifyer > 0) {
- xModifyer--;
- } else {
- left = false;
- }
- } else if (up) {
- if (y + yModifyer < 10) {
- yModifyer++;
- } else {
- up = false;
- }
- } else if (down) {
- if (y - yModifyer > 0) {
- yModifyer--;
- } else {
- down = false;
- }
- }
- return 0;
- }
- public int CoordsTop(int yStart) { // Called when a line is going to created off of the y-axis to the x-axis.
- if (!TopInitialized) {
- y = yStart;
- x = 0;
- TopInitialized = true;
- } else if (x >= 9 || y >= 9) {
- TopInitialized = false;
- SeekingRuns++;
- makeAttack();
- } else {
- y++;
- x++;
- }
- return 0;
- }
- public int CoordsBottom(int xStart) { // Called when a line is going to created off of the x-axis to the y-axis.
- if (!BottomInitialized) {
- x = xStart;
- y = 0;
- BottomInitialized = true;
- } else if (y >= 9 || x >= 9) {
- BottomInitialized = false;
- SeekingRuns++;
- makeAttack();
- } else {
- x++;
- y++;
- }
- return 0;
- }
- @Override
- public Coordinate makeAttack() { // Coordinates attacks on the enemy's ships, lines created by there probability of hitting a ship.
- if (!Seeking) {
- FinishShip();
- } else if (SeekingRuns == 0) {
- CoordsTop(0);
- } else if (SeekingRuns == 1) {
- CoordsBottom(6);
- } else if (SeekingRuns == 2) {
- CoordsTop(6);
- } else if (SeekingRuns == 3) {
- CoordsBottom(4);
- } else if (SeekingRuns == 4) {
- CoordsTop(4);
- } else if (SeekingRuns == 5) {
- CoordsBottom(8);
- } else if (SeekingRuns == 6) {
- CoordsTop(8);
- } else if (SeekingRuns == 7) {
- CoordsBottom(2);
- } else if (SeekingRuns == 8) {
- CoordsTop(2);
- }
- return new Coordinate(x + xModifyer, y + yModifyer);
- }
- @Override
- public void resultOfAttack(int result) {
- // 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.
- if (result == HIT_PATROL_BOAT || result == HIT_DESTROYER || result == HIT_SUBMARINE || result == HIT_BATTLESHIP || result == HIT_AIRCRAFT_CARRIER) {
- Seeking = false;
- if (hit) {
- up = true;
- down = true;
- left = true;
- right = true;
- }
- hit = true;
- if (x + xModifyer >= 10) {
- right = false;
- } else if (x - xModifyer < 0) {
- left = false;
- } if (y + yModifyer >= 10) {
- up = false;
- } else if (y - yModifyer < 0) {
- down = false;
- }
- } else if (result == SUNK_PATROL_BOAT || result == SUNK_DESTROYER || result == SUNK_SUBMARINE || result == SUNK_BATTLESHIP || result == SUNK_AIRCRAFT_CARRIER) {
- yModifyer = 0;
- xModifyer = 0;
- Seeking = true;
- hit = false;
- up = false;
- down = false;
- left = false;
- right = false;
- } else {
- if (hit) {
- yModifyer = 0; //
- xModifyer = 0; //
- if (right) {
- right = false;
- left = true; //
- } else if (left) {
- left = false;
- right = true; //
- } else if (up) {
- up = false;
- down = true; //
- } else if (down) {
- down = false;
- up = true; //
- }
- }
- }
- }
- @Override
- public void opponentAttack(Coordinate coord) {
- }
- @Override
- public void resultOfGame(int result) { // Logs the results of the match.
- if (result == WON) {
- LastGameWon = true;
- } else if (result == LOST) {
- LastGameWon = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement