|
@ -1,16 +1,376 @@ |
|
|
package de.tims.fleetstorm.ai; |
|
|
package de.tims.fleetstorm.ai; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotEquals; |
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
import de.tims.fleetstorm.matchfield.Coordinate; |
|
|
|
|
|
import de.tims.fleetstorm.matchfield.Matchfield; |
|
|
|
|
|
|
|
|
class LogicTest { |
|
|
class LogicTest { |
|
|
Logic logic = new Logic(); |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void testFielIsNotNull() { |
|
|
|
|
|
int[] calcResult = logic.chooseField(); |
|
|
|
|
|
|
|
|
void testFieldIsNotNull() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 5; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
// ArrayList<Coordinate> everySecondField = logic.getEverySecondField(); |
|
|
|
|
|
|
|
|
|
|
|
Coordinate calcResult = logic.chooseField(); |
|
|
assertNotNull(calcResult); |
|
|
assertNotNull(calcResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testChoosenFieldHasNotStateShot() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 5; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Coordinate> everySecondField = logic.getEverySecondField(); |
|
|
|
|
|
for (int i = 0; i < everySecondField.size(); i++) { |
|
|
|
|
|
everySecondField.get(i).setState(Coordinate.SHOT); |
|
|
|
|
|
} |
|
|
|
|
|
matchfield.setState(2, 2, Coordinate.EMPTY); |
|
|
|
|
|
logic.setEverySecondField(everySecondField); |
|
|
|
|
|
|
|
|
|
|
|
int calcState = logic.chooseField().getState(); |
|
|
|
|
|
assertNotEquals(calcState, Coordinate.SHOT); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testGetEverySecondField() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Coordinate> everySecondField = logic.getEverySecondField(); |
|
|
|
|
|
ArrayList<Coordinate> expectedResult = new ArrayList<Coordinate>(); |
|
|
|
|
|
|
|
|
|
|
|
expectedResult.add(new Coordinate(0, 0)); |
|
|
|
|
|
expectedResult.add(new Coordinate(0, 2)); |
|
|
|
|
|
expectedResult.add(new Coordinate(1, 1)); |
|
|
|
|
|
expectedResult.add(new Coordinate(1, 3)); |
|
|
|
|
|
expectedResult.add(new Coordinate(2, 0)); |
|
|
|
|
|
expectedResult.add(new Coordinate(2, 2)); |
|
|
|
|
|
expectedResult.add(new Coordinate(3, 1)); |
|
|
|
|
|
expectedResult.add(new Coordinate(3, 3)); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(everySecondField, expectedResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testGetAndSetLastShot() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
|
|
|
|
|
|
Coordinate expectedResult = new Coordinate(2, 2); |
|
|
|
|
|
logic.setLastShot(expectedResult); |
|
|
|
|
|
Coordinate result = logic.getLastShot(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(result, expectedResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSetMatchfield() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
Matchfield result = logic.getMatchfield(); |
|
|
|
|
|
assertEquals(result, null); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
result = logic.getMatchfield(); |
|
|
|
|
|
assertNotNull(result); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testFindShip() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
|
|
|
|
|
|
logic.setLastShot(new Coordinate(2, 2)); |
|
|
|
|
|
logic.getLastShot().setState(Coordinate.HIT); |
|
|
|
|
|
logic.findShip(); |
|
|
|
|
|
assertEquals(logic.getFoundShip(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testGetAndsetFoundShip() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
assertEquals(logic.getFoundShip(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testClearAbove() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
Coordinate shot = new Coordinate(2, 2); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(shot); |
|
|
|
|
|
matchfield.getField(shot).setState(Coordinate.EMPTY); |
|
|
|
|
|
|
|
|
|
|
|
logic.clearAbove(shot); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedAbove(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testClearBelow() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
Coordinate shot = new Coordinate(2, 2); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(shot); |
|
|
|
|
|
matchfield.getField(shot).setState(Coordinate.EMPTY); |
|
|
|
|
|
|
|
|
|
|
|
logic.clearBelow(shot); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedBelow(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testClearRight() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
Coordinate shot = new Coordinate(2, 2); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(shot); |
|
|
|
|
|
matchfield.getField(shot).setState(Coordinate.EMPTY); |
|
|
|
|
|
|
|
|
|
|
|
logic.clearRight(shot); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedRight(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testClearLeft() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
Matchfield matchfield; |
|
|
|
|
|
Coordinate shot = new Coordinate(2, 2); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(shot); |
|
|
|
|
|
matchfield.getField(shot).setState(Coordinate.EMPTY); |
|
|
|
|
|
|
|
|
|
|
|
logic.clearLeft(shot); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedLeft(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testConstructor() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getFoundShip(), false); |
|
|
|
|
|
assertEquals(logic.getClearedAbove(), false); |
|
|
|
|
|
assertEquals(logic.getClearedBelow(), false); |
|
|
|
|
|
assertEquals(logic.getClearedLeft(), false); |
|
|
|
|
|
assertEquals(logic.getClearedRight(), false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSetterClearedAbove() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
logic.setClearedAbove(true); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedAbove(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSetterClearedBelow() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
logic.setClearedBelow(true); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedBelow(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSetterClearedRight() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
logic.setClearedRight(true); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedRight(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSetterClearedLeft() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
logic.setClearedLeft(true); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getClearedLeft(), true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testSinkShip() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
|
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
logic.setClearedAbove(true); |
|
|
|
|
|
logic.setClearedBelow(true); |
|
|
|
|
|
logic.setClearedRight(true); |
|
|
|
|
|
logic.setClearedLeft(true); |
|
|
|
|
|
|
|
|
|
|
|
logic.sinkShip(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(logic.getFoundShip(), false); |
|
|
|
|
|
assertEquals(logic.getClearedAbove(), false); |
|
|
|
|
|
assertEquals(logic.getClearedBelow(), false); |
|
|
|
|
|
assertEquals(logic.getClearedLeft(), false); |
|
|
|
|
|
assertEquals(logic.getClearedRight(), false); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testChooseFieldAboveAfterHit() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
Coordinate center = new Coordinate(2, 2); |
|
|
|
|
|
Coordinate expectedResult = new Coordinate(2, 3); |
|
|
|
|
|
Matchfield matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
matchfield.setState(center, Coordinate.HIT); |
|
|
|
|
|
matchfield.setState(expectedResult, Coordinate.HIT); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(center); |
|
|
|
|
|
logic.setTarget(center); |
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
|
|
|
|
|
|
Coordinate result = logic.chooseField(); |
|
|
|
|
|
assertEquals(result, expectedResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testChooseFieldBelowAfterHit() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
Coordinate center = new Coordinate(2, 2); |
|
|
|
|
|
Coordinate expectedResult = new Coordinate(2, 1); |
|
|
|
|
|
Matchfield matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
matchfield.setState(center, Coordinate.HIT); |
|
|
|
|
|
matchfield.setState(expectedResult, Coordinate.HIT); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(center); |
|
|
|
|
|
logic.setTarget(center); |
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
logic.chooseField();// first Shot |
|
|
|
|
|
Coordinate result = logic.chooseField(); // second Shot |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(result, expectedResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testChooseFieldRightAfterHit() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
Coordinate center = new Coordinate(2, 2); |
|
|
|
|
|
Coordinate expectedResult = new Coordinate(3, 2); |
|
|
|
|
|
Matchfield matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
matchfield.setState(center, Coordinate.HIT); |
|
|
|
|
|
matchfield.setState(expectedResult, Coordinate.HIT); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(center); |
|
|
|
|
|
logic.setTarget(center); |
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
|
|
|
|
|
|
logic.chooseField(); // first Shot |
|
|
|
|
|
logic.chooseField(); // second Shot |
|
|
|
|
|
Coordinate result = logic.chooseField(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(result, expectedResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testChooseFieldLeftAfterHit() { |
|
|
|
|
|
Logic logic = new Logic(); |
|
|
|
|
|
int size = 4; |
|
|
|
|
|
Coordinate center = new Coordinate(2, 2); |
|
|
|
|
|
Coordinate expectedResult = new Coordinate(1, 2); |
|
|
|
|
|
Matchfield matchfield = new Matchfield(size); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
matchfield.setState(center, Coordinate.HIT); |
|
|
|
|
|
matchfield.setState(expectedResult, Coordinate.HIT); |
|
|
|
|
|
logic.setMatchfield(matchfield); |
|
|
|
|
|
logic.setLastShot(center); |
|
|
|
|
|
logic.setTarget(center); |
|
|
|
|
|
logic.setFoundShip(true); |
|
|
|
|
|
|
|
|
|
|
|
logic.chooseField(); // first Shot |
|
|
|
|
|
logic.chooseField(); // second 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); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |