diff --git a/src/main/java/Figure.java b/src/main/java/Figure.java index 8cf72b9..9b7ffa9 100644 --- a/src/main/java/Figure.java +++ b/src/main/java/Figure.java @@ -6,4 +6,11 @@ public class Figure { this.position = -1; } + public void setPosition(int position) { + this.position = position; + } + + public int getPosition() { + return position; + } } diff --git a/src/main/java/Game.java b/src/main/java/Game.java index da68c76..c2654d9 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -8,15 +8,14 @@ public class Game { public static void main(String[] args) { Game g = new Game(); - g.startGame(); } - public void startGame() { + public Game() { this.gb = new Gameboard(); 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); } } diff --git a/src/main/java/Gameboard.java b/src/main/java/Gameboard.java index 03d5fdb..5bb535b 100644 --- a/src/main/java/Gameboard.java +++ b/src/main/java/Gameboard.java @@ -1,4 +1,3 @@ -import java.lang.reflect.Array; import java.util.Arrays; public class Gameboard { diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 5e5d9a9..8725f74 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,13 +1,17 @@ -import javax.sound.midi.Soundbank; -import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; public class Player { String name; Figure[] figures; + int startHome; + int endHome; - public Player (String name) { + public Player (String name, int startHome, int endHome) { this.name = name; + this.startHome = startHome; + this.endHome = endHome; figures = new Figure[4]; for(int i = 0; i < 4; i++) { this.figures[i] = new Figure(); @@ -22,4 +26,16 @@ public class Player { public int rollDice() { return (int) (Math.random() * 6 + 1); } + + public boolean checkGameWin(Collection
figures) { + Iterator
it = figures.iterator(); + Figure f; + for(;it.hasNext();) { + f = it.next(); + if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) { + return false; + } + } + return true; + } } diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 122fbe6..6480543 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -1,10 +1,4 @@ -import static org.assertj.core.api.Assertions.*; - import org.junit.jupiter.api.BeforeEach; -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; public class GameTest { @@ -18,10 +12,10 @@ public class GameTest { void setup() { this.gb = new Gameboard(); 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); } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index 5000e0b..fc8537c 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -1,19 +1,23 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; 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 { private Player p; @BeforeEach void setup() { - p = new Player("Rot"); + p = new Player("Rot", 40, 43); } @Test @@ -43,4 +47,60 @@ public class PlayerTest { assertThat(calculatedResult).describedAs("Starting postion of Figures").isEqualTo(expectedResult); } + @ParameterizedTest + @MethodSource("FieldStream") + void checkGameWin(String testname, Collection
figures, Collection positions, boolean expectedResult) { + Iterator
it = figures.iterator(); + Iterator 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 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) + ); + } }