You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
7.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. import static org.assertj.core.api.Assertions.*;
  2. import org.junit.jupiter.api.BeforeEach;
  3. import org.junit.jupiter.api.Test;
  4. import org.junit.jupiter.params.ParameterizedTest;
  5. import org.junit.jupiter.params.provider.Arguments;
  6. import org.junit.jupiter.params.provider.MethodSource;
  7. import java.util.*;
  8. import java.util.stream.Stream;
  9. public class GameTest {
  10. private Game g;
  11. private Player p1;
  12. private Game g2;
  13. private Player p2;
  14. @BeforeEach
  15. void setup() {
  16. g = new Game();
  17. p1 = g.players.get(0);
  18. g2 = new Game();
  19. p2 = g2.players.get(0);
  20. }
  21. @ParameterizedTest
  22. @MethodSource("checkDiceTestData")
  23. void checkDiceTest(String testname, Collection<Integer> positions, int dice, int c, boolean expectedResult) {
  24. Iterator<Figure> it = p1.figures.iterator();
  25. Iterator<Integer> it2 = positions.iterator();
  26. while(it.hasNext()) {
  27. it.next().setPosition(it2.next());
  28. }
  29. boolean calculatedResult = g.checkDice(dice, p1, c);
  30. assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
  31. }
  32. static Stream<Arguments> checkDiceTestData () {
  33. return Stream.of(
  34. Arguments.of("No figures on Field - d: 1 - c: 1",
  35. Arrays.asList(-1, -1, -1, -1),
  36. 1,
  37. 1,
  38. true
  39. ),
  40. Arguments.of("No figures on Field - d: 1 - c: 2",
  41. Arrays.asList(-1, -1, -1, -1),
  42. 1,
  43. 2,
  44. true
  45. ),
  46. Arguments.of("No figures on Field - d: 1 - c: 3",
  47. Arrays.asList(-1, -1, -1, -1),
  48. 1,
  49. 3,
  50. false
  51. ),
  52. Arguments.of("No figures on Field - d: 1 - c: 4",
  53. Arrays.asList(-1, -1, -1, -1),
  54. 1,
  55. 4,
  56. false
  57. ),
  58. Arguments.of("Figures on Field - d: 1 - c: 1",
  59. Arrays.asList(10, -1, 2, -1),
  60. 1,
  61. 1,
  62. false
  63. ),
  64. Arguments.of("Figures on Field - d: 6 - c: 1",
  65. Arrays.asList(10, -1, 2, -1),
  66. 6,
  67. 1,
  68. true
  69. )
  70. );
  71. }
  72. @Test
  73. void checkFieldClearTestFieldNotTaken() {
  74. int expectedResult = 0;
  75. int calculatedResult = g.checkFieldClear(1, p1, g);
  76. assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
  77. }
  78. @Test
  79. void checkFieldClearTestFieldTakenByOtherPlayer() {
  80. int expectedResult = 1;
  81. g.players.get(1).figures.get(0).setPosition(1);
  82. int calculatedResult = g.checkFieldClear(1, p1, g);
  83. assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
  84. }
  85. @Test
  86. void checkFieldClearTestFieldTakenByOwnFigure() {
  87. int expectedResult = 2;
  88. p1.figures.get(1).setPosition(1);
  89. int calculatedResult = g.checkFieldClear(1, p1, g);
  90. assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
  91. }
  92. @ParameterizedTest
  93. @MethodSource("usableFiguresData")
  94. void getUsableFiguresTest(String testname, int dice, Collection<Integer> positions, ArrayList<Integer> expectedResult) {
  95. Iterator<Figure> figureIt = p1.figures.iterator();
  96. Iterator<Integer> posIt = positions.iterator();
  97. while(figureIt.hasNext()) {
  98. Figure f = figureIt.next();
  99. f.setPosition(posIt.next());
  100. }
  101. List<Integer> calculatedResult = g.getUsableFigures(dice, p1, g);
  102. assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
  103. }
  104. static Stream<Arguments> usableFiguresData () {
  105. return Stream.of(
  106. Arguments.of( //Würfel 1-6 - Keine Figur im Ziel - Alle Figuren auf dem Spielfeld - StartFeld frei - Keine Figur vor Ziel
  107. "Alle Figuren",
  108. 1,
  109. Arrays.asList(10, 25, 2, 17),
  110. new ArrayList<>(Arrays.asList(0, 1, 2, 3))
  111. ),
  112. Arguments.of( //Würfel 6 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld frei - Keine Figur vor Ziel
  113. "Figuren die nicht am Spielfeld stehen",
  114. 6,
  115. Arrays.asList(10, -1, -1, -1),
  116. new ArrayList<>(Arrays.asList(1, 2, 3))
  117. ),
  118. Arguments.of( //Würfel 6 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Keine Figur vor Ziel
  119. "Figur die das Startfeld besetzt",
  120. 6,
  121. Arrays.asList(10, 0, -1, -1),
  122. new ArrayList<>(List.of(1))
  123. ),
  124. Arguments.of( //Würfel 5 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel
  125. "Figur vor dem Ziel - kann man benutzen",
  126. 5,
  127. Arrays.asList(10, 37, -1, -1),
  128. new ArrayList<>(Arrays.asList(0,1))
  129. ),
  130. Arguments.of( //Würfel 5 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel
  131. "Figur vor dem Ziel - kann man nicht benutzen",
  132. 5,
  133. Arrays.asList(10, 39, -1, -1),
  134. new ArrayList<>(List.of(0))
  135. ),
  136. Arguments.of( //Würfel 5 - Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel
  137. "Nur Figuren die auf ein freies Feld kommen",
  138. 2,
  139. Arrays.asList(10, 12, -1, -1),
  140. new ArrayList<>(List.of(1))
  141. ),
  142. Arguments.of( //Würfel 6 - 1 Figur auf dem Spielfeld - StartFeld besetzt
  143. "Figur auf Startfeld",
  144. 6,
  145. Arrays.asList(-1, -1, -1, 0),
  146. new ArrayList<>(List.of(3))
  147. ),
  148. Arguments.of( //Würfel 6 - 1 Figur auf dem Spielfeld - StartFeld besetzt
  149. "Figur auf Startfeld",
  150. 5,
  151. Arrays.asList(-1, 41, -1, 0),
  152. new ArrayList<>(List.of(3))
  153. )
  154. );
  155. }
  156. @ParameterizedTest
  157. @MethodSource("setFigureData")
  158. void setFigureTest(String testname, int figId, int dice, int currentPosition, int expectedResult) {
  159. p2.figures.get(figId).setPosition(currentPosition);
  160. int calculatedResult = g2.setFigure(figId, dice, p2, g2);
  161. assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
  162. }
  163. static Stream<Arguments> setFigureData () {
  164. return Stream.of(
  165. Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 5, 9),
  166. Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 10, 14),
  167. Arguments.of("Figur wird ins Haus gesetzt", 3, 4, 38, 42),
  168. Arguments.of("Figur wird im Haus gesetzt", 3, 3, 41, 44)
  169. );
  170. }
  171. }