From 1a20205bfc6c27d3ffc8a7b22601ddc31af839cf Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 30 Oct 2021 16:45:25 +0200 Subject: [PATCH] Every card is unique. --- .../aminelouveaau/cartes/service/GameService.java | 2 ++ .../cartes/CartesApplicationTests.java | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java index b10f91e..e227c26 100644 --- a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java +++ b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java @@ -48,6 +48,7 @@ public class GameService { } public void createDeck() { + deck = new Stack<>(); Arrays.stream(Shape.values()).forEach(shape -> { Arrays.stream(Number.values()).forEach(number -> { deck.push(new Card(shape, number)); @@ -72,6 +73,7 @@ public class GameService { for (int i = 0; i < this.cardHandNumber; i++) { hand.add(this.deck.pop()); } + logger.info("Hand : " + hand); return hand; } else { throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS); diff --git a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java index 9ccef2e..2249920 100644 --- a/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java +++ b/src/test/java/fr/aminelouveaau/cartes/CartesApplicationTests.java @@ -11,6 +11,7 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.HttpClientErrorException; import java.util.List; +import java.util.stream.Collectors; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @@ -47,15 +48,23 @@ class CartesApplicationTests { @Test() void testExceptionTooManyPlayers() { - int maxNumberOfPlayers = (int) Math.floor(DECK_SIZE / CARDS_PER_HAND); - for (int i = 0; i < maxNumberOfPlayers; i++) { + for (int i = 0; i < getMaxNumberOfPlayers(); i++) { gameService.cardHand(); } Assertions.assertThrows(HttpClientErrorException.class, () -> gameService.cardHand()); } + private int getMaxNumberOfPlayers() { + int maxNumberOfPlayers = (int) Math.floor(DECK_SIZE / CARDS_PER_HAND); + return maxNumberOfPlayers; + } + @Test - void contextLoads() { + void isAllUniqueCards() { + for (int i = 0; i < getMaxNumberOfPlayers(); i++) { + List hand = gameService.cardHand().stream().distinct().collect(Collectors.toList()); + assertThat(hand.size()).isEqualTo(CARDS_PER_HAND); + } } }