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.4.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;
- private 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 + 1 < 10) {
- System.out.print("Finishing RIGHT ship.");
- xModifyer++;
- } else {
- yModifyer = 0;
- xModifyer = 0;
- Right = false;
- Left = true;
- }
- } else if (Left) {
- if (x + xModifyer - 1 > 0) {
- System.out.print("Finishing LEFT ship.");
- xModifyer--;
- } else {
- yModifyer = 0;
- xModifyer = 0;
- Left = false;
- Right = true;
- }
- } else if (Up) {
- if (y + yModifyer + 1 < 10) {
- System.out.print("Finishing Up ship.");
- yModifyer++;
- } else {
- yModifyer = 0;
- xModifyer = 0;
- Up = false;
- Down = true;
- }
- } else if (Down) {
- if (y + yModifyer - 1 > 0) {
- System.out.print("Finishing Down ship.");
- yModifyer--;
- } else {
- yModifyer = 0;
- xModifyer = 0;
- Down = false;
- Up = true;
- }
- }
- 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) {
- System.out.print("Creating CoordsTop line starting at Y: " + yStart);
- 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) {
- System.out.print("Creating CoordsTop line starting at X: " + xStart);
- 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) {
- if (result == HIT_PATROL_BOAT || result == HIT_DESTROYER || result == HIT_SUBMARINE || result == HIT_BATTLESHIP || result == HIT_AIRCRAFT_CARRIER) {
- System.out.print("Hit ship!");
- Seeking = false;
- Hit = true;
- if (Hit) {
- Up = true;
- Down = true;
- Left = true;
- Right = true;
- }
- } else if (result == SUNK_PATROL_BOAT || result == SUNK_DESTROYER || result == SUNK_SUBMARINE || result == SUNK_BATTLESHIP || result == SUNK_AIRCRAFT_CARRIER) {
- System.out.print("Sunk ship.");
- yModifyer = 0;
- xModifyer = 0;
- Seeking = true;
- Hit = false;
- Up = false;
- Down = false;
- Left = false;
- Right = false;
- } else if (result == MISS) {
- System.out.print("Something 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