From b5b1feeb7493e2acb72c4f540a9c5c7791cce88d Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 25 Jan 2022 09:53:07 +0100 Subject: [PATCH] Implement checkGameWin Method --- src/main/java/Game.java | 8 ++--- src/main/java/Player.java | 20 ++++++++++- src/test/java/GameTest.java | 8 ++--- src/test/java/PlayerTest.java | 66 ++++++++++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 0252238..c2654d9 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -13,9 +13,9 @@ public class Game { 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/Player.java b/src/main/java/Player.java index 8a4f438..8725f74 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,11 +1,17 @@ +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(); @@ -20,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 994f82b..6480543 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -12,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 337ddab..fc8537c 100644 --- a/src/test/java/PlayerTest.java +++ b/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.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 { @@ -9,7 +17,7 @@ public class PlayerTest { @BeforeEach void setup() { - p = new Player("Rot"); + p = new Player("Rot", 40, 43); } @Test @@ -39,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) + ); + } }