Browse Source

if ShipIsOnYAxis, ai doesent check XAxis

Max Wenzel 3 years ago
parent
commit
c6acd0b4d4
  1. 46
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 4
      src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java
  3. 47
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java

46
src/main/java/de/tims/fleetstorm/ai/Logic.java

@ -10,6 +10,7 @@ public class Logic {
private Matchfield matchfield; private Matchfield matchfield;
private ArrayList<Coordinate> everySecondField; private ArrayList<Coordinate> everySecondField;
private Coordinate lastShot; private Coordinate lastShot;
private Coordinate target; private Coordinate target;
private boolean foundShip; private boolean foundShip;
@ -24,55 +25,57 @@ public class Logic {
clearedBelow = false; clearedBelow = false;
clearedRight = false; clearedRight = false;
clearedLeft = false; clearedLeft = false;
} }
public Coordinate chooseField() { public Coordinate chooseField() {
Coordinate out;
if (foundShip) { if (foundShip) {
// Clear Y-Axis
if (!clearedAbove) { if (!clearedAbove) {
target = matchfield.getAbove(target); target = matchfield.getAbove(target);
out = target;
if (target.getState() == Coordinate.EMPTY) { if (target.getState() == Coordinate.EMPTY) {
clearedAbove = true; clearedAbove = true;
target = lastShot;
} }
return target;
return out;
} }
target = lastShot;
if (!clearedBelow) { if (!clearedBelow) {
target = matchfield.getBelow(target); target = matchfield.getBelow(target);
out = target;
if (target.getState() == Coordinate.EMPTY) { if (target.getState() == Coordinate.EMPTY) {
clearedBelow = true; clearedBelow = true;
target = lastShot;
} }
return target;
isShipOnYAxis();
return out;
} }
// if()
target = lastShot;
// Clear x-Axis
if (!clearedRight) { if (!clearedRight) {
target = matchfield.getRight(target); target = matchfield.getRight(target);
out = target;
if (target.getState() == Coordinate.EMPTY) { if (target.getState() == Coordinate.EMPTY) {
clearedRight = true; clearedRight = true;
target = lastShot;
} }
return target;
return out;
} }
target = lastShot;
if (!clearedLeft) { if (!clearedLeft) {
target = matchfield.getLeft(target); target = matchfield.getLeft(target);
out = target;
if (target.getState() == Coordinate.EMPTY) { if (target.getState() == Coordinate.EMPTY) {
clearedLeft = true; clearedLeft = true;
target = lastShot;
} }
return target;
sinkShip();
return out;
} }
} }
sinkShip();
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>(); ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>();
for (int i = 0; i < everySecondField.size(); i++) { for (int i = 0; i < everySecondField.size(); i++) {
if (everySecondField.get(i).getState() != Coordinate.SHOT if (everySecondField.get(i).getState() != Coordinate.SHOT
@ -89,6 +92,15 @@ public class Logic {
return lastShot; return lastShot;
} }
public void isShipOnYAxis() {
if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot).getState() == Coordinate.HIT)
|| (matchfield.getBelow(lastShot).getState() == Coordinate.HIT))) {
clearedLeft = true;
clearedRight = true;
sinkShip();
}
}
public ArrayList<Coordinate> getEverySecondField() { public ArrayList<Coordinate> getEverySecondField() {
ArrayList<Coordinate> out = new ArrayList<Coordinate>(); ArrayList<Coordinate> out = new ArrayList<Coordinate>();
for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) {
@ -147,6 +159,10 @@ public class Logic {
// Getter And Setter // Getter And Setter
public void setEverySecondField(ArrayList<Coordinate> everySecondField) {
this.everySecondField = everySecondField;
}
public void setLastShot(Coordinate coordinate) { public void setLastShot(Coordinate coordinate) {
lastShot = this.matchfield.getField(coordinate); lastShot = this.matchfield.getField(coordinate);
} }

4
src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java

@ -40,4 +40,8 @@ public class Coordinate {
return this.x == ((Coordinate) obj).getX() && this.y == ((Coordinate) obj).getY(); return this.x == ((Coordinate) obj).getX() && this.y == ((Coordinate) obj).getY();
} }
public void print() {
System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state);
}
} }

47
src/test/java/de/tims/fleetstorm/ai/LogicTest.java

@ -21,7 +21,7 @@ class LogicTest {
matchfield = new Matchfield(size); matchfield = new Matchfield(size);
matchfield.createMatchfield(); matchfield.createMatchfield();
logic.setMatchfield(matchfield); logic.setMatchfield(matchfield);
ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
// ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
Coordinate calcResult = logic.chooseField(); Coordinate calcResult = logic.chooseField();
assertNotNull(calcResult); assertNotNull(calcResult);
@ -41,9 +41,9 @@ class LogicTest {
everySecondField.get(i).setState(Coordinate.SHOT); everySecondField.get(i).setState(Coordinate.SHOT);
} }
matchfield.setState(2, 2, Coordinate.EMPTY); matchfield.setState(2, 2, Coordinate.EMPTY);
logic.setEverySecondField(everySecondField);
Coordinate choosenField = logic.chooseField();
int calcState = matchfield.getState(choosenField.getX(), choosenField.getY());
int calcState = logic.chooseField().getState();
assertNotEquals(calcState, Coordinate.SHOT); assertNotEquals(calcState, Coordinate.SHOT);
} }
@ -288,8 +288,7 @@ class LogicTest {
logic.setLastShot(center); logic.setLastShot(center);
logic.setTarget(center); logic.setTarget(center);
logic.setFoundShip(true); logic.setFoundShip(true);
logic.chooseField(); // first Shot
logic.chooseField();// first Shot
Coordinate result = logic.chooseField(); // second Shot Coordinate result = logic.chooseField(); // second Shot
assertEquals(result, expectedResult); assertEquals(result, expectedResult);
@ -334,10 +333,44 @@ class LogicTest {
logic.chooseField(); // first Shot logic.chooseField(); // first Shot
logic.chooseField(); // second Shot logic.chooseField(); // second Shot
logic.chooseField(); // third Shot
logic.chooseField(); // third Shot Coordinate
Coordinate result = logic.chooseField(); Coordinate result = logic.chooseField();
assertEquals(result, expectedResult); assertEquals(result, expectedResult);
} }
@Test
void testShipIsOnYAxis() {
int size = 10;
Logic logic = new Logic();
Matchfield matchfield = new Matchfield(size);
Coordinate s1 = new Coordinate(5, 4);
Coordinate s2 = new Coordinate(5, 5);
Coordinate s3 = new Coordinate(5, 6);
matchfield.createMatchfield();
matchfield.setState(s1, Coordinate.SHIP);
matchfield.setState(s2, Coordinate.HIT);
matchfield.setState(s3, Coordinate.SHIP);
logic.setMatchfield(matchfield);
logic.setLastShot(s2);
logic.setTarget(s2);
logic.setFoundShip(true);
logic.chooseField(); // First shot (s3)
logic.getMatchfield().setState(s3, Coordinate.HIT); //
logic.chooseField(); // Second Shot
logic.getMatchfield().setState(new Coordinate(5, 7), Coordinate.SHOT);
logic.chooseField(); // Third Shot (s1)
logic.getMatchfield().setState(s1, Coordinate.HIT); //
logic.chooseField(); // fourth Shot
assertEquals(logic.getFoundShip(), false);
assertEquals(logic.getClearedAbove(), false);
assertEquals(logic.getClearedBelow(), false);
assertEquals(logic.getClearedLeft(), false);
assertEquals(logic.getClearedRight(), false);
}
} }
Loading…
Cancel
Save