From 10848a7b463d807a4706ce1b5438a53cad46a096 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 1 Feb 2022 10:22:00 +0100 Subject: [PATCH] Implement checkDice method --- src/main/java/Game.java | 43 +++++++++++++++++++----- src/main/java/Player.java | 7 ++-- src/test/java/GameTest.java | 63 +++++++++++++++++++++++++++++++++++ src/test/java/PlayerTest.java | 14 ++++---- 4 files changed, 109 insertions(+), 18 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index c2654d9..2f2d509 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,21 +1,48 @@ +import java.util.ArrayList; + public class Game { Gameboard gb; - Player p1; - Player p2; - Player p3; - Player p4; + ArrayList players; public static void main(String[] args) { Game g = new Game(); + /*while (checkDice() != 0) { + int dice = g.players[0].rollDice(); + System.out.println("Würfel: " + dice); + }*/ } public Game() { 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); + players = new ArrayList<>(); + players.add(new Player("Rot", 40, 43)); + players.add(new Player("Blau", 44, 47)); + players.add(new Player("Gelb", 48, 51)); + players.add(new Player("Grün", 52, 55)); + } + + public int checkDice(int dice, Player p) { + int figuresInBase = p.checkFigureInBase(p.figures); + if(figuresInBase == 4) { + if(dice == 6) { + return 1; + } else { + return 0; + } + } else if(figuresInBase > 0) { + if(dice == 6) { + return 3; + } else { + return 2; + } + } else { + if(dice == 6) { + return 5; + } else { + return 4; + } + } } } diff --git a/src/main/java/Player.java b/src/main/java/Player.java index c143347..140b144 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -41,16 +41,17 @@ public class Player { return true; } - public boolean checkFigureInBase(ArrayList
figures) { + public int checkFigureInBase(ArrayList
figures) { Iterator
it = figures.iterator(); Figure f; + int count = 0; while(it.hasNext()) { f = it.next(); if(f.getPosition() == -1) { - return true; + count++; } } - return false; + return count; } public int choose() { diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index bd47a51..bd357c6 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -1,4 +1,14 @@ +import static org.assertj.core.api.Assertions.*; + import org.junit.jupiter.api.BeforeEach; +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 GameTest { @@ -10,4 +20,57 @@ public class GameTest { g = new Game(); } + @ParameterizedTest + @MethodSource("checkDiceTestData") + void checkDiceTest(String testname, Player testPlayer, Collection positions, int dice, int expectedResult) { + Iterator
it = testPlayer.figures.iterator(); + Iterator it2 = positions.iterator(); + while(it.hasNext()) { + it.next().setPosition(it2.next()); + } + int calculatedResult = g.checkDice(dice, testPlayer); + assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); + } + + static Stream checkDiceTestData () { + return Stream.of( + Arguments.of("No figures on Field - not a 6", + new Player("Rot", 40, 43), + Arrays.asList(-1, -1, -1, -1), + 1, + 0 + ), + Arguments.of("No figures on Field - a 6", + new Player("Rot", 40, 43), + Arrays.asList(-1, -1, -1, -1), + 6, + 1 + ), + Arguments.of("Few Figures in Field - not a 6", + new Player("Rot", 40, 43), + Arrays.asList(25, -1, 13, -1), + 2, + 2 + ), + Arguments.of("Few Figures in Field - a 6", + new Player("Rot", 40, 43), + Arrays.asList(25, -1, 13, -1), + 6, + 3 + ), + Arguments.of("All Figures in Field - not a 6", + new Player("Rot", 40, 43), + Arrays.asList(2, 10, 15, 21), + 3, + 4 + ), + Arguments.of("All Figures in Field - a 6", + new Player("Rot", 40, 43), + Arrays.asList(2, 10, 15, 21), + 6, + 5 + ) + + ); + } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index c419115..5695f87 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -64,13 +64,13 @@ public class PlayerTest { @ParameterizedTest @MethodSource("BaseTestData") - void checkFigureInBase(String testname, ArrayList
figures, Collection positions, boolean expectedResult) { + void checkFigureInBase(String testname, ArrayList
figures, Collection positions, int expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); while(it.hasNext()) { it.next().setPosition(it2.next()); } - boolean calculatedResult = p.checkFigureInBase(figures); + int calculatedResult = p.checkFigureInBase(figures); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } @@ -134,7 +134,7 @@ public class PlayerTest { new Figure(), new Figure()) ), Arrays.asList(-1, -1, -1, -1), - true), + 4), Arguments.of("Three Figures in Base", new ArrayList<>( Arrays.asList( @@ -143,7 +143,7 @@ public class PlayerTest { new Figure(), new Figure()) ), Arrays.asList(40, -1, -1, -1), - true), + 3), Arguments.of("Two Figures in Base", new ArrayList<>( Arrays.asList( @@ -152,7 +152,7 @@ public class PlayerTest { new Figure(), new Figure()) ), Arrays.asList(40, 41, -1, -1), - true), + 2), Arguments.of("One Figure in Base", new ArrayList<>( Arrays.asList( @@ -161,7 +161,7 @@ public class PlayerTest { new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, -1), - true), + 1), Arguments.of("No Figures in Base", new ArrayList<>( Arrays.asList( @@ -170,7 +170,7 @@ public class PlayerTest { new Figure(), new Figure()) ), Arrays.asList(40, 41, 42, 43), - false) + 0) ); }