Advertisement
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.1.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 = 0;
- private boolean topInitialized = false;
- private boolean bottomInitialized = false;
- @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 Plan
- 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 CoordsTop(int startY) {
- if (!topInitialized) {
- y = startY;
- x = 0;
- topInitialized = true;
- } else if (x >= 9 || y >= 9) {
- topInitialized = false;
- seekingRuns++;
- makeAttack();
- } else {
- y++;
- x++;
- }
- return 0;
- }
- public int CoordsBottom(int startX) {
- if (!bottomInitialized) {
- x = startX;
- y = 0;
- bottomInitialized = true;
- } else if (y >= 9 || x >= 9) {
- bottomInitialized = false;
- seekingRuns++;
- makeAttack();
- } else {
- x++;
- y++;
- }
- return 0;
- }
- @Override
- public Coordinate makeAttack() {
- // TODO: Diagnol lines created by probability.
- 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) {
- }
- @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
Advertisement