From d40d4c1468314fa39cadb8dc9b093dda9b3545fb Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 25 Jan 2022 10:13:08 +0100 Subject: [PATCH 1/3] Imnplement checkFigureInBase method --- src/main/java/Player.java | 12 +++++++ src/test/java/PlayerTest.java | 65 ++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 8725f74..b5da374 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -38,4 +38,16 @@ public class Player { } return true; } + + public boolean checkFigureInBase(Collection
figures) { + Iterator
it = figures.iterator(); + Figure f; + for(;it.hasNext();) { + f = it.next(); + if(f.getPosition() == -1) { + return true; + } + } + return false; + } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index fc8537c..ac7e92f 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -59,9 +59,21 @@ public class PlayerTest { assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult); } + @ParameterizedTest + @MethodSource("BaseTestData") + void checkFigureInBase(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.checkFigureInBase(figures); + assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult); + } + static Stream FieldStream() { return Stream.of( - Arguments.of("No Figure in House", + Arguments.of("No Figures in House", Arrays.asList( new Figure(), new Figure(), @@ -77,7 +89,7 @@ public class PlayerTest { new Figure()), Arrays.asList(40, -1, -1, -1), false), - Arguments.of("Two Figure in House", + Arguments.of("Two Figures in House", Arrays.asList( new Figure(), new Figure(), @@ -85,7 +97,7 @@ public class PlayerTest { new Figure()), Arrays.asList(40, 41, -1, -1), false), - Arguments.of("Three Figure in House", + Arguments.of("Three Figures in House", Arrays.asList( new Figure(), new Figure(), @@ -93,7 +105,7 @@ public class PlayerTest { new Figure()), Arrays.asList(40, 41, 42, -1), false), - Arguments.of("Four Figure in House", + Arguments.of("Four Figures in House", Arrays.asList( new Figure(), new Figure(), @@ -103,4 +115,49 @@ public class PlayerTest { true) ); } + + static Stream BaseTestData() { + return Stream.of( + Arguments.of("Four Figures in Base", + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()), + Arrays.asList(-1, -1, -1, -1), + true), + Arguments.of("Three Figures in Base", + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()), + Arrays.asList(40, -1, -1, -1), + true), + Arguments.of("Two Figures in Base", + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()), + Arrays.asList(40, 41, -1, -1), + true), + Arguments.of("One Figure in Base", + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()), + Arrays.asList(40, 41, 42, -1), + true), + Arguments.of("No Figures in Base", + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()), + Arrays.asList(40, 41, 42, 43), + false) + ); + } } From 372dcc3b134efa480b2a81e8027c638d5707ec80 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 25 Jan 2022 10:17:07 +0100 Subject: [PATCH 2/3] Refactor names of source-method --- src/test/java/GameTest.java | 14 +++----------- src/test/java/PlayerTest.java | 4 ++-- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/test/java/GameTest.java b/src/test/java/GameTest.java index 6480543..bd47a51 100644 --- a/src/test/java/GameTest.java +++ b/src/test/java/GameTest.java @@ -2,20 +2,12 @@ import org.junit.jupiter.api.BeforeEach; public class GameTest { - private Gameboard gb; - private Player p1; - private Player p2; - private Player p3; - private Player p4; + private Game g; + @BeforeEach void setup() { - 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); + g = new Game(); } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index ac7e92f..fed5ccd 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -48,7 +48,7 @@ public class PlayerTest { } @ParameterizedTest - @MethodSource("FieldStream") + @MethodSource("GameWinTestData") void checkGameWin(String testname, Collection
figures, Collection positions, boolean expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); @@ -71,7 +71,7 @@ public class PlayerTest { assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult); } - static Stream FieldStream() { + static Stream GameWinTestData() { return Stream.of( Arguments.of("No Figures in House", Arrays.asList( From 8e1b4567ce714586b09a99c259ee2e0bdb5cf025 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Tue, 25 Jan 2022 10:40:46 +0100 Subject: [PATCH 3/3] Fix Up Player and PlayerTest Collection to Arraylist --- src/main/java/Player.java | 16 ++-- src/test/java/PlayerTest.java | 150 ++++++++++++++++++---------------- 2 files changed, 89 insertions(+), 77 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index b5da374..0ed8bd6 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,10 +1,10 @@ -import java.util.Collection; +import java.util.ArrayList; import java.util.Iterator; public class Player { String name; - Figure[] figures; + ArrayList
figures; int startHome; int endHome; @@ -12,9 +12,9 @@ public class Player { this.name = name; this.startHome = startHome; this.endHome = endHome; - figures = new Figure[4]; + figures = new ArrayList<>(); for(int i = 0; i < 4; i++) { - this.figures[i] = new Figure(); + figures.add(new Figure()); } } @@ -27,10 +27,10 @@ public class Player { return (int) (Math.random() * 6 + 1); } - public boolean checkGameWin(Collection
figures) { + public boolean checkGameWin(ArrayList
figures) { Iterator
it = figures.iterator(); Figure f; - for(;it.hasNext();) { + while(it.hasNext()) { f = it.next(); if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) { return false; @@ -39,10 +39,10 @@ public class Player { return true; } - public boolean checkFigureInBase(Collection
figures) { + public boolean checkFigureInBase(ArrayList
figures) { Iterator
it = figures.iterator(); Figure f; - for(;it.hasNext();) { + while(it.hasNext()) { f = it.next(); if(f.getPosition() == -1) { return true; diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index fed5ccd..07a3033 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; @@ -36,9 +37,10 @@ public class PlayerTest { @Test void checkStartPositionOfFigures() { int expectedResult = -1; - int calculatedResult = p.figures[0].position; - for(int i = 0; i < 4; i++) { - calculatedResult = p.figures[i].position; + Iterator
it = p.figures.iterator(); + int calculatedResult = it.next().position; + while(it.hasNext()) { + calculatedResult = it.next().position; if(expectedResult != calculatedResult) { calculatedResult = 1; break; @@ -49,69 +51,74 @@ public class PlayerTest { @ParameterizedTest @MethodSource("GameWinTestData") - void checkGameWin(String testname, Collection
figures, Collection positions, boolean expectedResult) { + void checkGameWin(String testname, ArrayList
figures, Collection positions, boolean expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); - for(;it.hasNext();) { + while(it.hasNext()) { it.next().setPosition(it2.next()); } boolean calculatedResult = p.checkGameWin(figures); - assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult); + assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } @ParameterizedTest @MethodSource("BaseTestData") - void checkFigureInBase(String testname, Collection
figures, Collection positions, boolean expectedResult) { + void checkFigureInBase(String testname, ArrayList
figures, Collection positions, boolean expectedResult) { Iterator
it = figures.iterator(); Iterator it2 = positions.iterator(); - for(;it.hasNext();) { + while(it.hasNext()) { it.next().setPosition(it2.next()); } boolean calculatedResult = p.checkFigureInBase(figures); - assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult); + assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); } static Stream GameWinTestData() { return Stream.of( Arguments.of("No Figures in House", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(-1, -1, -1, -1), + new ArrayList<>( + 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), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, -1, -1, -1), false), Arguments.of("Two Figures in House", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, -1, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, -1, -1), false), Arguments.of("Three Figures in House", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, 42, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, 42, -1), false), Arguments.of("Four Figures in House", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, 42, 43), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, 42, 43), true) ); } @@ -119,44 +126,49 @@ public class PlayerTest { static Stream BaseTestData() { return Stream.of( Arguments.of("Four Figures in Base", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(-1, -1, -1, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(-1, -1, -1, -1), true), Arguments.of("Three Figures in Base", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, -1, -1, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, -1, -1, -1), true), Arguments.of("Two Figures in Base", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, -1, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, -1, -1), true), Arguments.of("One Figure in Base", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, 42, -1), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, 42, -1), true), Arguments.of("No Figures in Base", - Arrays.asList( - new Figure(), - new Figure(), - new Figure(), - new Figure()), - Arrays.asList(40, 41, 42, 43), + new ArrayList<>( + Arrays.asList( + new Figure(), + new Figure(), + new Figure(), + new Figure()) + ), Arrays.asList(40, 41, 42, 43), false) ); }