Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 CheckM8 implements Captain {
- private boolean LastGameWon = false;
- private Random Generator;
- private Fleet MyFleet;
- private int LastPlan, Plan;
- private int x, y;
- private int xModifyer, yModifyer;
- private int SeekingRuns;
- private boolean topInitialized = false;
- private boolean bottomInitialized = false;
- private boolean Attacking, Searching, Miss, Horazontal, Vertical, Positive;
- private int TilesLeft;
- @Override
- public void initialize(int numMatches, int numCaptains, String opponent) {
- Generator = new Random();
- MyFleet = new Fleet();
- if (LastGameWon == true) {
- Plan = LastPlan;
- } else {
- 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;
- }
- private int SearchRuns = 0;
- public int DestroyShip() {
- if (Searching && Miss) {
- Miss = false;
- if (SearchRuns == 0) {
- xModifyer = -1;
- yModifyer = 0;
- } if (SearchRuns == 1) {
- xModifyer = 2;
- yModifyer = 0;
- } if (SearchRuns == 2) {
- xModifyer = 1;
- yModifyer = -1;
- } if (SearchRuns == 3) {
- xModifyer = 0;
- yModifyer = 2;
- }
- SearchRuns++;
- }
- if (!Searching && Miss) {
- Miss = false;
- if (Vertical && Positive) {
- Positive = false;
- xModifyer = 0;
- yModifyer = 0;
- } else if (Vertical && !Positive) {
- Positive = true;
- xModifyer = 0;
- yModifyer = 0;
- } else if (Horazontal && Positive) {
- Positive = false;
- xModifyer = 0;
- yModifyer = 0;
- } else if (Horazontal && !Positive) {
- Positive = true;
- xModifyer = 0;
- yModifyer = 0;
- }
- }
- return 0;
- }
- public int CoordsTop(int yStart) {
- 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) {
- 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() {
- if (Attacking)
- DestroyShip();
- 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) {
- if (!Attacking && result == HIT_PATROL_BOAT) {
- TilesLeft = 2 - 1;
- Attacking = true;
- Searching = true;
- } else if (!Attacking && result == HIT_DESTROYER) {
- TilesLeft = 3 - 1;
- Attacking = true;
- Searching = true;
- } else if (!Attacking && result == HIT_SUBMARINE) {
- TilesLeft = 3 - 1;
- Attacking = true;
- Searching = true;
- } else if (!Attacking && result == HIT_BATTLESHIP) {
- TilesLeft = 4 - 1;
- Attacking = true;
- Searching = true;
- } else if (!Attacking && result == HIT_AIRCRAFT_CARRIER) {
- TilesLeft = 5 - 1;
- Attacking = true;
- Searching = true;
- }
- if (Searching && result == MISS) {
- Miss = true;
- } else if (Searching && result != MISS) {
- Miss = false;
- Searching = false;
- if (xModifyer >= 1) {
- Horazontal = true;
- Positive = true;
- } else if (xModifyer <= -1) {
- Horazontal = true;
- Positive = false;
- } else if (yModifyer >= 1) {
- Vertical = true;
- Positive = true;
- } else if (yModifyer <= -1) {
- Vertical = true;
- Positive = false;
- }
- }
- if (!Searching && result == MISS) {
- Miss = true;
- } else if (!Searching && result != MISS) {
- // Keep firing at 'em!
- }
- if (result == SUNK_PATROL_BOAT || result == SUNK_DESTROYER || result == SUNK_SUBMARINE || result == SUNK_BATTLESHIP || result == SUNK_AIRCRAFT_CARRIER) {
- Attacking = false;
- Searching = false;
- xModifyer = 0;
- yModifyer = 0;
- }
- }
- @Override
- public void opponentAttack(Coordinate coord) {
- }
- @Override
- public void resultOfGame(int result) {
- if (result == WON) {
- LastGameWon = true;
- } else if (result == LOST) {
- LastGameWon = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment