From 2c498e42ee66c8ce8a4971fea0bf7ff21870bfbd Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Wed, 16 Feb 2022 09:25:35 +0100 Subject: [PATCH] Rewrite checkdice with new tests --- src/main/java/Game.java | 23 +++++++++++++---------- src/test/java/GameTest.java | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 6d843da..438acc0 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -29,7 +29,7 @@ public class Game { } while(figId == -1); g.setFigure(figId, dice, p, g); } - } while (g.checkDice(dice, p, c)); + } while (g.checkDice(dice, p, c, g)); if(p.checkGameWin(p.figures)) { winner = p; System.out.println("Spieler " + winner.name + " gewinnt!"); @@ -63,18 +63,21 @@ public class Game { * @param dice Value of dice got from rolldice() * @param p active Player * @param countRolls Counter how often the Player already rolled the dice + * @param g game instance * @return true if Player can roll the dice another time */ - public boolean checkDice(int dice, Player p, int countRolls) { - boolean figuresOnBoard = false; - for(Figure f : p.figures) { - if (f.getPosition() > -1 && f.getPosition() < 40) { - figuresOnBoard = true; - break; + public boolean checkDice(int dice, Player p, int countRolls, Game g) { + int figuresInBase = p.checkFigureInBase(p.figures); + if(countRolls >= 3) return false; + if(figuresInBase == 4) { + return true; + } else if (figuresInBase < 4) { + for(int i = 0; i < 4 - figuresInBase; i++) { + if(checkFieldClear(p.startHome+3-i,p,g) == 0) { + return dice == 6; + } } - } - if(!figuresOnBoard) { - return countRolls < 3; + return true; } else return dice == 6; } diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 27d63dc..8626d8c 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -37,7 +37,7 @@ public class GameTest { while(it.hasNext()) { it.next().setPosition(it2.next()); } - boolean calculatedResult = g.checkDice(dice, p1, c); + boolean calculatedResult = g.checkDice(dice, p1, c, g); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } @@ -59,7 +59,7 @@ public class GameTest { Arrays.asList(-1, -1, -1, -1), 1, 3, - true + false ), Arguments.of("No figures on Field - d: 1 - c: 4", Arrays.asList(-1, -1, -1, -1), @@ -78,6 +78,18 @@ public class GameTest { 6, 1, true + ), + Arguments.of("Figures on Field - d: 6 - c: 1", + Arrays.asList(42, -1, -1, -1), + 5, + 1, + false + ), + Arguments.of("Figures on Field - d: 6 - c: 1", + Arrays.asList(42, 43, -1, -1), + 5, + 1, + true ) ); }