Browse Source

if ShipIsOnYAxis, ai doesent check XAxis

fleetstorm
Max Wenzel 2 years ago
committed by Lorenz Hohmann
parent
commit
79a7556286
  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 ArrayList<Coordinate> everySecondField;
private Coordinate lastShot;
private Coordinate target;
private boolean foundShip;
@ -24,55 +25,57 @@ public class Logic {
clearedBelow = false;
clearedRight = false;
clearedLeft = false;
}
public Coordinate chooseField() {
Coordinate out;
if (foundShip) {
// Clear Y-Axis
if (!clearedAbove) {
target = matchfield.getAbove(target);
out = target;
if (target.getState() == Coordinate.EMPTY) {
clearedAbove = true;
target = lastShot;
}
return target;
return out;
}
target = lastShot;
if (!clearedBelow) {
target = matchfield.getBelow(target);
out = target;
if (target.getState() == Coordinate.EMPTY) {
clearedBelow = true;
target = lastShot;
}
return target;
isShipOnYAxis();
return out;
}
// if()
target = lastShot;
// Clear x-Axis
if (!clearedRight) {
target = matchfield.getRight(target);
out = target;
if (target.getState() == Coordinate.EMPTY) {
clearedRight = true;
target = lastShot;
}
return target;
return out;
}
target = lastShot;
if (!clearedLeft) {
target = matchfield.getLeft(target);
out = target;
if (target.getState() == Coordinate.EMPTY) {
clearedLeft = true;
target = lastShot;
}
return target;
sinkShip();
return out;
}
}
sinkShip();
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>();
for (int i = 0; i < everySecondField.size(); i++) {
if (everySecondField.get(i).getState() != Coordinate.SHOT
@ -89,6 +92,15 @@ public class Logic {
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() {
ArrayList<Coordinate> out = new ArrayList<Coordinate>();
for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) {
@ -147,6 +159,10 @@ public class Logic {
// Getter And Setter
public void setEverySecondField(ArrayList<Coordinate> everySecondField) {
this.everySecondField = everySecondField;
}
public void setLastShot(Coordinate 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();
}
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.createMatchfield();
logic.setMatchfield(matchfield);
ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
// ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
Coordinate calcResult = logic.chooseField();
assertNotNull(calcResult);
@ -41,9 +41,9 @@ class LogicTest {
everySecondField.get(i).setState(Coordinate.SHOT);
}
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);
}
@ -288,8 +288,7 @@ class LogicTest {
logic.setLastShot(center);
logic.setTarget(center);
logic.setFoundShip(true);
logic.chooseField(); // first Shot
logic.chooseField();// first Shot
Coordinate result = logic.chooseField(); // second Shot
assertEquals(result, expectedResult);
@ -334,10 +333,44 @@ class LogicTest {
logic.chooseField(); // first Shot
logic.chooseField(); // second Shot
logic.chooseField(); // third Shot
logic.chooseField(); // third Shot Coordinate
Coordinate result = logic.chooseField();
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