Browse Source

Refactoring: Implements Coordinate into Matchfield

fleetstorm
Max Wenzel 3 years ago
committed by Lorenz Hohmann
parent
commit
f8a8179650
  1. 5
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 16
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  3. 4
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java
  4. 6
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

5
src/main/java/de/tims/fleetstorm/ai/Logic.java

@ -3,6 +3,7 @@ package de.tims.fleetstorm.ai;
import java.util.ArrayList;
import java.util.Random;
import de.tims.fleetstorm.matchfield.Coordinate;
import de.tims.fleetstorm.matchfield.Matchfield;
public class Logic {
@ -11,14 +12,14 @@ public class Logic {
ArrayList<int[]> possibleFields = new ArrayList<int[]>();
for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) {
for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) {
if (matchfield.getState(x, y) == Matchfield.EMPTY || matchfield.getState(x, y) == Matchfield.SHIP) {
if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) {
possibleFields.add(new int[] { x, y });
}
}
}
Random randy = new Random();
return possibleFields.get(randy.nextInt(possibleFields.size() - 1));
return possibleFields.get(randy.nextInt(possibleFields.size()));
}
}

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

@ -2,22 +2,18 @@ package de.tims.fleetstorm.matchfield;
public class Matchfield {
private int[][] matchfield;
private Coordinate[][] 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];
this.matchfield = new Coordinate[this.size][this.size];
}
public int[][] createMatchfield() {
public Coordinate[][] createMatchfield() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
this.matchfield[i][j] = Matchfield.EMPTY;
this.matchfield[i][j] = new Coordinate(i, j);
}
}
@ -29,11 +25,11 @@ public class Matchfield {
}
public int getState(int x, int y) {
return this.matchfield[x][y];
return this.matchfield[x][y].getState();
}
public void setState(int x, int y, int state) {
this.matchfield[x][y] = state;
this.matchfield[x][y].setState(state);
}
}

4
src/test/java/de/tims/fleetstorm/ai/LogicTest.java

@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import de.tims.fleetstorm.matchfield.Coordinate;
import de.tims.fleetstorm.matchfield.Matchfield;
class LogicTest {
@ -14,6 +15,7 @@ class LogicTest {
@Test
void testFieldIsNotNull() {
matchfield.createMatchfield();
int[] calcResult = logic.chooseField(matchfield);
assertNotNull(calcResult);
}
@ -30,6 +32,6 @@ class LogicTest {
int[] choosenField = logic.chooseField(matchfield);
int calcState = matchfield.getState(choosenField[0], choosenField[1]);
assertNotEquals(calcState, Matchfield.SHOT);
assertNotEquals(calcState, Coordinate.SHOT);
}
}

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

@ -15,7 +15,7 @@ class MatchfieldCreationTest {
@Test
void testMatchfieldCreateNotEmpty() {
Matchfield matchfield = new Matchfield(0);
int[][] calcResult = matchfield.createMatchfield();
Coordinate[][] calcResult = matchfield.createMatchfield();
assertNotNull(calcResult);
}
@ -44,7 +44,7 @@ class MatchfieldCreationTest {
}
static Stream<Arguments> testMatchfieldGetFieldState() {
return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY));
return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Coordinate.EMPTY));
}
@ParameterizedTest(name = "matchfield change field is correct")
@ -61,6 +61,6 @@ class MatchfieldCreationTest {
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));
Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP));
}
}
Loading…
Cancel
Save