7 Commits

  1. 22
      pom.xml
  2. 39
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  3. 66
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

22
pom.xml

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hs-fulda</groupId>
<artifactId>TIMS</artifactId>
@ -9,12 +10,13 @@
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.jupiter.version>5.8.1</junit.jupiter.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.jupiter.version>5.8.0</junit.jupiter.version>
<junit.platform.version>1.8.1</junit.platform.version>
<assertj.version>3.21.0</assertj.version>
<junit.version>3.8.1</junit.version>
<junit.version>4.12</junit.version>
<junit-vintage-engine>4.12.1</junit-vintage-engine>
<mockito.core.version>4.1.0</mockito.core.version>
<mockito.junit.jupiter.version>4.1.0</mockito.junit.jupiter.version>
<junit.platform.surefire.provider.version>1.0.1</junit.platform.surefire.provider.version>
@ -33,6 +35,11 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
@ -70,6 +77,11 @@
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.surefire.provider.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage-engine}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>

39
src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

@ -0,0 +1,39 @@
package de.tims.fleetstorm.matchfield;
public class Matchfield {
private int[][] matchfield;
private 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() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.matchfield[i][j] = Matchfield.EMPTY;
}
}
return this.matchfield;
}
public int getSize() {
return this.matchfield.length * this.matchfield[0].length;
}
public int getState(int x, int y) {
return this.matchfield[x][y];
}
public void setState(int x, int y, int state) {
this.matchfield[x][y] = state;
}
}

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

@ -0,0 +1,66 @@
package de.tims.fleetstorm.matchfield;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class MatchfieldCreationTest {
@Test
void testMatchfieldCreateNotEmpty() {
Matchfield matchfield = new Matchfield(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 matchfield = new Matchfield(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, 100), Arguments.of("field size 15x15", 15, 225));
}
@ParameterizedTest(name = "matchfield get field is correct")
@MethodSource("testMatchfieldGetFieldState")
void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
int calcResult = matchfield.getState(x, y);
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldGetFieldState() {
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) {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
matchfield.setState(x, y, state);
int calcResult = matchfield.getState(x, y);
assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> testMatchfieldSetStateIsCorrect() {
return Stream.of(
Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP));
}
}
Loading…
Cancel
Save