Browse Source

Merge commit 'd81070a0cfe7f6d3a05f9f035568bedb72ca109f' into HEAD

master
Jenkins 3 years ago
parent
commit
2253946c8f
  1. 24
      src/main/java/Player.java
  2. 14
      src/test/java/GameTest.java
  3. 113
      src/test/java/PlayerTest.java

24
src/main/java/Player.java

@ -1,10 +1,10 @@
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class Player {
String name;
Figure[] figures;
ArrayList<Figure> figures;
int startHome;
int endHome;
@ -12,9 +12,9 @@ public class Player {
this.name = name;
this.startHome = startHome;
this.endHome = endHome;
figures = new Figure[4];
figures = new ArrayList<>();
for(int i = 0; i < 4; i++) {
this.figures[i] = new Figure();
figures.add(new Figure());
}
}
@ -27,10 +27,10 @@ public class Player {
return (int) (Math.random() * 6 + 1);
}
public boolean checkGameWin(Collection<Figure> figures) {
public boolean checkGameWin(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator();
Figure f;
for(;it.hasNext();) {
while(it.hasNext()) {
f = it.next();
if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) {
return false;
@ -38,4 +38,16 @@ public class Player {
}
return true;
}
public boolean checkFigureInBase(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator();
Figure f;
while(it.hasNext()) {
f = it.next();
if(f.getPosition() == -1) {
return true;
}
}
return false;
}
}

14
src/test/java/GameTest.java

@ -2,20 +2,12 @@ import org.junit.jupiter.api.BeforeEach;
public class GameTest {
private Gameboard gb;
private Player p1;
private Player p2;
private Player p3;
private Player p4;
private Game g;
@BeforeEach
void setup() {
this.gb = new Gameboard();
gb.initGameboard();
p1 = new Player("Rot", 40, 43);
p2 = new Player("Blau", 44, 47);
p3 = new Player("Gelb", 48, 51);
p4 = new Player("Grün", 52, 55);
g = new Game();
}
}

113
src/test/java/PlayerTest.java

@ -6,6 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@ -36,9 +37,10 @@ public class PlayerTest {
@Test
void checkStartPositionOfFigures() {
int expectedResult = -1;
int calculatedResult = p.figures[0].position;
for(int i = 0; i < 4; i++) {
calculatedResult = p.figures[i].position;
Iterator<Figure> it = p.figures.iterator();
int calculatedResult = it.next().position;
while(it.hasNext()) {
calculatedResult = it.next().position;
if(expectedResult != calculatedResult) {
calculatedResult = 1;
break;
@ -48,59 +50,126 @@ public class PlayerTest {
}
@ParameterizedTest
@MethodSource("FieldStream")
void checkGameWin(String testname, Collection<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
@MethodSource("GameWinTestData")
void checkGameWin(String testname, ArrayList<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator();
for(;it.hasNext();) {
while(it.hasNext()) {
it.next().setPosition(it2.next());
}
boolean calculatedResult = p.checkGameWin(figures);
assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
static Stream<Arguments> FieldStream() {
@ParameterizedTest
@MethodSource("BaseTestData")
void checkFigureInBase(String testname, ArrayList<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator();
while(it.hasNext()) {
it.next().setPosition(it2.next());
}
boolean calculatedResult = p.checkFigureInBase(figures);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
static Stream<Arguments> GameWinTestData() {
return Stream.of(
Arguments.of("No Figure in House",
Arguments.of("No Figures in House",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(-1, -1, -1, -1),
new Figure())
), Arrays.asList(-1, -1, -1, -1),
false),
Arguments.of("One Figure in House",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, -1, -1, -1),
new Figure())
), Arrays.asList(40, -1, -1, -1),
false),
Arguments.of("Two Figure in House",
Arguments.of("Two Figures in House",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, -1, -1),
new Figure())
), Arrays.asList(40, 41, -1, -1),
false),
Arguments.of("Three Figure in House",
Arguments.of("Three Figures in House",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, -1),
new Figure())
), Arrays.asList(40, 41, 42, -1),
false),
Arguments.of("Four Figure in House",
Arguments.of("Four Figures in House",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, 43),
new Figure())
), Arrays.asList(40, 41, 42, 43),
true)
);
}
static Stream<Arguments> BaseTestData() {
return Stream.of(
Arguments.of("Four Figures in Base",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(-1, -1, -1, -1),
true),
Arguments.of("Three Figures in Base",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, -1, -1, -1),
true),
Arguments.of("Two Figures in Base",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, -1, -1),
true),
Arguments.of("One Figure in Base",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, -1),
true),
Arguments.of("No Figures in Base",
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, 43),
false)
);
}
}
Loading…
Cancel
Save