From 3586cfad3ec2e1119664846795030c5a9b3df7e9 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 8 Feb 2022 09:33:41 +0100 Subject: [PATCH] Create method checkFieldClear --- src/main/java/Game.java | 26 +++++++++++++++++++++++++- src/test/java/GameTest.java | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/java/Game.java b/src/main/java/Game.java index f98b288..6720ef5 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,3 +1,4 @@ +import java.sql.SQLOutput; import java.util.ArrayList; import java.util.Iterator; @@ -16,7 +17,8 @@ public class Game { do { dice = p.rollDice(); c++; - if(p.checkFigureInBase(p.figures) == 4 && dice == 6) { + //g.checkFieldClear(p.startPos, p, g) + if(p.checkFigureInBase(p.figures) > 0 && dice == 6) { int figId = p.choose() - 1; //checkIfKicked p.figures.get(figId).setPosition(p.startPos); @@ -47,4 +49,26 @@ public class Game { return countRolls <= 3; } else return dice == 6; } + + public int checkFieldClear(int posToCheck, Player p, Game g) { + int mode; + for (Player currentPlayer : g.players) { + //System.out.println("Player"); + if (currentPlayer.name.equals(p.name)) { + System.out.println("TEST"); + mode = 2; + } else { + System.out.println("TEST2"); + mode = 1; + } + for (Figure f : currentPlayer.figures) { + //System.out.println("Figure"); + System.out.println(f.getPosition()); + if (posToCheck == f.getPosition()) { + return mode; + } + } + } + return 0; + } } diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index a7b9bf7..b01581d 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -1,6 +1,7 @@ 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; @@ -13,24 +14,26 @@ import java.util.stream.Stream; public class GameTest { private Game g; - private Player p; + private Player p1; + private Player p2; @BeforeEach void setup() { g = new Game(); - p = new Player("Rot", 1, 40, 43); + p1 = new Player("Rot", 1, 40, 43); + p2 = new Player("Blau",10, 44, 47); } @ParameterizedTest @MethodSource("checkDiceTestData") void checkDiceTest(String testname, Collection positions, int dice, int c, boolean expectedResult) { - Iterator
it = p.figures.iterator(); + Iterator
it = p1.figures.iterator(); Iterator it2 = positions.iterator(); while(it.hasNext()) { it.next().setPosition(it2.next()); } - boolean calculatedResult = g.checkDice(dice, p, c); + boolean calculatedResult = g.checkDice(dice, p1, c); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } @@ -74,4 +77,27 @@ public class GameTest { ) ); } + + @Test + void checkFieldClearTestFieldNotTaken() { + int expectedResult = 0; + int calculatedResult = g.checkFieldClear(1, p1, g); + assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult); + } + + @Test + void checkFieldClearTestFieldTakenByOtherPlayer() { + int expectedResult = 1; + g.players.get(1).figures.get(0).setPosition(1); + int calculatedResult = g.checkFieldClear(1, g.players.get(0), g); + assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult); + } + + @Test + void checkFieldClearTestFieldTakenByOwnFigure() { + int expectedResult = 2; + g.players.get(0).figures.get(1).setPosition(1); + int calculatedResult = g.checkFieldClear(1, g.players.get(0), g); + assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult); + } }