Browse Source

matchfield: added dynamic field size with constructor

Lorenz Hohmann 3 years ago
parent
commit
96939307ae
  1. 17
      src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java
  2. 11
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

17
src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java

@ -2,20 +2,27 @@ package de.tims.fleetstorm.matchfield;
public class MatchfieldCreation {
public int[][] createMatchfield(int size) {
int[][] matchfield = new int[size][size];
private int[][] matchfield;
private int size;
public MatchfieldCreation(int size) {
this.size = 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++) {
matchfield[i][j] = 1;
this.matchfield[i][j] = 1;
}
}
return matchfield;
return this.matchfield;
}
public int getSize() {
return 10;
return this.matchfield.length * this.matchfield[0].length;
}
}

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

@ -12,23 +12,24 @@ import org.junit.jupiter.params.provider.MethodSource;
class MatchfieldCreationTest {
MatchfieldCreation matchfield = new MatchfieldCreation();
@Test
void testMatchfieldCreateNotEmpty() {
int[][] calcResult = matchfield.createMatchfield(0);
MatchfieldCreation matchfield = new MatchfieldCreation(0);
int[][] calcResult = matchfield.createMatchfield();
assertNotNull(calcResult);
}
@ParameterizedTest(name = "matchfield creation has correct size")
@MethodSource("testMatchfieldSize")
void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) {
matchfield.createMatchfield(size);
MatchfieldCreation matchfield = new MatchfieldCreation(size);
matchfield.createMatchfield();
int calcResult = matchfield.getSize();
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldSize() {
return Stream.of(Arguments.of("field size 10x10", 10, 10));
return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225));
}
}
Loading…
Cancel
Save