Browse Source

Implement checkGameWin Method

AIPlayer
Jonas Wagner 3 years ago
parent
commit
b5b1feeb74
  1. 8
      src/main/java/Game.java
  2. 20
      src/main/java/Player.java
  3. 8
      src/test/java/GameTest.java
  4. 66
      src/test/java/PlayerTest.java

8
src/main/java/Game.java

@ -13,9 +13,9 @@ public class Game {
public Game() { public Game() {
this.gb = new Gameboard(); this.gb = new Gameboard();
gb.initGameboard(); gb.initGameboard();
p1 = new Player("Rot");
p2 = new Player("Blau");
p3 = new Player("Gelb");
p4 = new Player("Grün");
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);
} }
} }

20
src/main/java/Player.java

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

8
src/test/java/GameTest.java

@ -12,10 +12,10 @@ public class GameTest {
void setup() { void setup() {
this.gb = new Gameboard(); this.gb = new Gameboard();
gb.initGameboard(); gb.initGameboard();
p1 = new Player("Rot");
p2 = new Player("Blau");
p3 = new Player("Gelb");
p4 = new Player("Grün");
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);
} }
} }

66
src/test/java/PlayerTest.java

@ -2,6 +2,14 @@ import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.stream.Stream;
public class PlayerTest { public class PlayerTest {
@ -9,7 +17,7 @@ public class PlayerTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
p = new Player("Rot");
p = new Player("Rot", 40, 43);
} }
@Test @Test
@ -39,4 +47,60 @@ public class PlayerTest {
assertThat(calculatedResult).describedAs("Starting postion of Figures").isEqualTo(expectedResult); assertThat(calculatedResult).describedAs("Starting postion of Figures").isEqualTo(expectedResult);
} }
@ParameterizedTest
@MethodSource("FieldStream")
void checkGameWin(String testname, Collection<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator();
for(;it.hasNext();) {
it.next().setPosition(it2.next());
}
boolean calculatedResult = p.checkGameWin(figures);
assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult);
}
static Stream<Arguments> FieldStream() {
return Stream.of(
Arguments.of("No Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(-1, -1, -1, -1),
false),
Arguments.of("One Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, -1, -1, -1),
false),
Arguments.of("Two Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, -1, -1),
false),
Arguments.of("Three Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, -1),
false),
Arguments.of("Four Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, 43),
true)
);
}
} }
Loading…
Cancel
Save