Browse Source

matchfield: refactored Matchfield class structure

matchfield
Lorenz Hohmann 2 years ago
parent
commit
f6d4e8a182
  1. 13
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  2. 13
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

13
src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java → src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

@ -1,20 +1,23 @@
package de.tims.fleetstorm.matchfield;
public class MatchfieldCreation {
public class Matchfield {
private int[][] matchfield;
private int size;
public MatchfieldCreation(int size) {
public static final int EMPTY = 0;
public static final int SHIP = 1;
public static final int SHOT = 2;
public Matchfield(int size) {
this.size = size;
this.matchfield = new int[this.size][this.size];
}
public int[][] createMatchfield() {
this.matchfield = new int[this.size][this.size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.matchfield[i][j] = 1;
this.matchfield[i][j] = Matchfield.EMPTY;
}
}

13
src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

@ -14,7 +14,7 @@ class MatchfieldCreationTest {
@Test
void testMatchfieldCreateNotEmpty() {
MatchfieldCreation matchfield = new MatchfieldCreation(0);
Matchfield matchfield = new Matchfield(0);
int[][] calcResult = matchfield.createMatchfield();
assertNotNull(calcResult);
}
@ -22,7 +22,7 @@ class MatchfieldCreationTest {
@ParameterizedTest(name = "matchfield creation has correct size")
@MethodSource("testMatchfieldSize")
void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) {
MatchfieldCreation matchfield = new MatchfieldCreation(size);
Matchfield matchfield = new Matchfield(size);
matchfield.createMatchfield();
int calcResult = matchfield.getSize();
@ -36,7 +36,7 @@ class MatchfieldCreationTest {
@ParameterizedTest(name = "matchfield get field is correct")
@MethodSource("testMatchfieldGetFieldState")
void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) {
MatchfieldCreation matchfield = new MatchfieldCreation(10);
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
int calcResult = matchfield.getState(x, y);
@ -44,13 +44,13 @@ class MatchfieldCreationTest {
}
static Stream<Arguments> testMatchfieldGetFieldState() {
return Stream.of(Arguments.of("field x:0 y:0 has initial state", 0, 0, 1));
return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY));
}
@ParameterizedTest(name = "matchfield change field is correct")
@MethodSource("testMatchfieldSetStateIsCorrect")
void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) {
MatchfieldCreation matchfield = new MatchfieldCreation(10);
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setState(x, y, state);
@ -60,6 +60,7 @@ class MatchfieldCreationTest {
}
static Stream<Arguments> testMatchfieldSetStateIsCorrect() {
return Stream.of(Arguments.of("field x:0 y:0 has state 2 after setState()", 0, 0, 2, 2));
return Stream.of(
Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP));
}
}
Loading…
Cancel
Save