diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java index 2e9424b..ea8824f 100644 --- a/src/main/java/de/hsfulda/onses/services/GameService.java +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -5,15 +5,30 @@ import de.hsfulda.onses.models.Game; import de.hsfulda.onses.models.Player; import java.util.Collections; +import java.util.Random; public class GameService { private final Game game; + + private boolean test = false; + + public GameService setTest(boolean test) { + this.test = test; + return this; + } + + public boolean getTest() { + return this.test; + } public GameService(Game game) { this.game = game; this.game.setGameService(this); fillDrawDeck(); shuffleDeck(); + setFirstCard(); + fillPlayerDecks(); } + public GameService() { this(new Game()); } @@ -25,60 +40,119 @@ public class GameService { public void nextPlayer() { game.getPlayerService().nextTurn(); } + public void drawCard(int amount) { - Player player = null; - if (game.getPlayerService().getCurrentTurn()) { - player = game.getPlayerService().getPlayerList().getFirst(); - } else { - player = game.getPlayerService().getPlayerList().getLast(); - } - for (int i = 0; i < amount; i++) { - player.getPlayerDeck().add(game.getDrawCardDeck().getFirst()); - game.getDrawCardDeck().removeFirst(); + if (!game.getDrawCardDeck().isEmpty()) { + Player player = null; + if (game.getPlayerService().getCurrentTurn()) { + player = game.getPlayerService().getPlayerList().getFirst(); + } else { + player = game.getPlayerService().getPlayerList().getLast(); + } + for (int i = 0; i < amount; i++) { + player.getPlayerDeck().add(game.getDrawCardDeck().getFirst()); + game.getDrawCardDeck().removeFirst(); + } + if (amount == 1 && !player.getPlayerDeck().isEmpty()) { + Card drawnCard = player.getPlayerDeck().getLast(); + + if (legalMove(drawnCard)) { + player.getPlayerService().removeCardFromPlayerDeck(drawnCard); + playCard(drawnCard); + } + else nextPlayer(); + } } if (amount != 1) { nextPlayer(); } } - public void playCard(Card card) - { - // add lastPlayedCard back to drawCardDeck - game.setLastPlayedCard(card); - // check for special rules (draw, colorchoose, skip,...) - // Skip - if (card.getValue() == Card.Value.SKIP) { - nextPlayer(); - nextPlayer(); + + public void addLastPlayedCardToDrawCardDeck() { + Card lastCard = game.getLastPlayedCard(); + if (lastCard.getValue() == Card.Value.CHOOSE || lastCard.getValue() == Card.Value.CHOOSEDRAW) { + lastCard.setColor(Card.Color.BLACK); } - // Reverse - if (card.getValue() == Card.Value.REVERSE) { - nextPlayer(); - nextPlayer(); + game.addCardToDrawCardDeck(lastCard); + } + + public void chooseColor() { + // Abfrage Farbe + Card.Color color = Card.Color.BLUE; //Vorübergehend Blau + game.changeLastPlayedCardColor(color); + + if (!game.getPlayerService().getCurrentTurn()){ + int min = 0; + int max = 3; + Random rand = new Random(); + int randomNum = rand.nextInt((max - min) + 1) + min; + switch (randomNum){ + case 0: + color = Card.Color.BLUE; + game.changeLastPlayedCardColor(color); + break; + case 1: + color = Card.Color.RED; + game.changeLastPlayedCardColor(color); + break; + case 2: + color = Card.Color.YELLOW; + game.changeLastPlayedCardColor(color); + break; + case 3: + color = Card.Color.GREEN; + game.changeLastPlayedCardColor(color); + break; + } } - // Choose - if (card.getValue() == Card.Value.CHOOSE) { - // Abfrage Farbe - Card.Color color = Card.Color.BLUE; //Vorübergehend Blau - game.changeLastPlayedCardColor(color); - nextPlayer(); + } + + public void checkForWin() { + if (this.game.getPlayerService().getPlayerList().getFirst().getPlayerDeck().isEmpty()) { + System.out.println("Player wins"); + } else if (this.game.getPlayerService().getPlayerList().getLast().getPlayerDeck().isEmpty()) { + System.out.println("Bot wins"); } - // Draw - if (card.getValue() == Card.Value.DRAWTWO) { - nextPlayer(); - drawCard(2); + } + + public void playCard(Card card) { + // add lastPlayedCard back to drawCardDeck + this.addLastPlayedCardToDrawCardDeck(); + game.setLastPlayedCard(card); + // check for special rules (draw, colorchoose, skip,...) + // Skip + + switch (card.getValue()) { + case SKIP, REVERSE: + nextPlayer(); + nextPlayer(); + break; + case CHOOSE: + chooseColor(); + nextPlayer(); + break; + case CHOOSEDRAW: + chooseColor(); + nextPlayer(); + drawCard(4); + break; + case DRAWTWO: + nextPlayer(); + drawCard(2); + break; + default: + nextPlayer(); + break; } - // ChooseDraw - if (card.getValue() == Card.Value.CHOOSEDRAW) { - // Abfrage Farbe - Card.Color color = Card.Color.BLUE; //Vorübergehend Blau - game.changeLastPlayedCardColor(color); - nextPlayer(); - drawCard(4); + checkForWin(); + if (!this.test) { + if (!game.getPlayerService().getCurrentTurn()) { + game.getPlayerService().botMove(); + } } } - public boolean legalMove(Card card) - { + public boolean legalMove(Card card) { boolean legalMoveFound = false; Card lastCard = game.getLastPlayedCard(); // rules: @@ -92,16 +166,16 @@ public class GameService { } public void fillDrawDeck() { - for (Card.Color i : Card.Color.values()){ - for (Card.Value j : Card.Value.values()){ - if (i != Card.Color.BLACK && j != Card.Value.CHOOSE && j != Card.Value.CHOOSEDRAW){ + for (Card.Color i : Card.Color.values()) { + for (Card.Value j : Card.Value.values()) { + if (i != Card.Color.BLACK && j != Card.Value.CHOOSE && j != Card.Value.CHOOSEDRAW) { game.addCardToDrawCardDeck(new Card().setColor(i).setValue(j)); game.addCardToDrawCardDeck(new Card().setColor(i).setValue(j)); } } } - for (int i = 0; i != 4; i++){ + for (int i = 0; i != 4; i++) { game.addCardToDrawCardDeck(new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE)); game.addCardToDrawCardDeck(new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSEDRAW)); } @@ -111,4 +185,20 @@ public class GameService { Collections.shuffle(game.getDrawCardDeck()); } + public void setFirstCard() { + for (int i = 0; i < this.game.getDrawCardDeck().size(); i++) { + Card card = this.game.getDrawCardDeck().get(i); + if (card.getColor() != Card.Color.BLACK) { + this.game.setLastPlayedCard(card); + this.game.getDrawCardDeck().remove(card); + break; + } + } + } + + public void fillPlayerDecks() { + this.drawCard(7); + this.drawCard(7); + } + } diff --git a/src/main/java/de/hsfulda/onses/services/PlayerService.java b/src/main/java/de/hsfulda/onses/services/PlayerService.java index 3b32e3a..14382d3 100644 --- a/src/main/java/de/hsfulda/onses/services/PlayerService.java +++ b/src/main/java/de/hsfulda/onses/services/PlayerService.java @@ -2,6 +2,8 @@ package de.hsfulda.onses.services; import de.hsfulda.onses.models.Game; import de.hsfulda.onses.models.Player; +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.services.GameService; import java.util.ArrayList; @@ -15,12 +17,12 @@ public class PlayerService { public ArrayList getPlayerList() { return playerList; } + public void addPlayerToList(Player player) { playerList.add(player); } - public void nextTurn() - { + public void nextTurn() { currentTurn = !currentTurn; } @@ -43,9 +45,33 @@ public class PlayerService { return this; } - public PlayerService() - { + public PlayerService() { addPlayerToList(new Player().setPlayerService(this)); addPlayerToList(new Player().setPlayerService(this)); } + + public void removeCardFromPlayerDeck(Card card) { + Player player; + if (this.currentTurn) { + player = playerList.getFirst(); + } else { + player = playerList.getLast(); + } + player.getPlayerDeck().remove(card); + } + + public void botMove() { + Card lastPlayedCard = game.getLastPlayedCard(); + for (int i = 0; i < this.playerList.getLast().getPlayerDeck().size(); i++) { + if (getGame().getGameService().legalMove(this.playerList.getLast().getPlayerDeck().get(i))) { + Card playCard = this.playerList.getLast().getPlayerDeck().get(i); + this.removeCardFromPlayerDeck(playCard); + getGame().getGameService().playCard(playCard); + break; + } + } + if (lastPlayedCard == game.getLastPlayedCard()) { + game.getGameService().drawCard(1); + } + } } diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java index 81ab433..de47a5e 100644 --- a/src/test/java/de/hsfulda/onses/GameServiceTest.java +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -1,8 +1,10 @@ package de.hsfulda.onses; +import de.hsfulda.onses.models.Game; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.*; import de.hsfulda.onses.models.Card; @@ -17,11 +19,13 @@ public class GameServiceTest { Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.EIGHT); // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.playCard(input); Card answer = gameService.getGame().getLastPlayedCard(); // assert assertEquals(input, answer); } + @Test @DisplayName("playCardBlackChoose") public void playCardBlackChoose() { @@ -29,6 +33,7 @@ public class GameServiceTest { Card input = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE); // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.playCard(input); Card answer = gameService.getGame().getLastPlayedCard(); // assert @@ -43,12 +48,14 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); boolean answer = gameService.legalMove(input); // assert assertEquals(expected, answer); } + @Test @DisplayName("legalMoveSameColorBlue") public void legalMoveSameColorBlue() { @@ -57,16 +64,19 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.ONE)); boolean answer = gameService.legalMove(input); // assert assertEquals(expected, answer); } + @Test @DisplayName("FillDrawCardDeck") - public void FillDrawCardDeck() { + public void FillDrawCardDeck() { GameService gameService = new GameService(); + gameService.setTest(true); int expected = gameService.getGame().getDrawCardDeck().size() + 112; gameService.fillDrawDeck(); @@ -83,6 +93,7 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.ONE)); boolean answer = gameService.legalMove(input); @@ -98,6 +109,7 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.THREE)); boolean answer = gameService.legalMove(input); @@ -113,6 +125,7 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.THREE)); boolean answer = gameService.legalMove(input); @@ -128,12 +141,14 @@ public class GameServiceTest { boolean expected = true; // act GameService gameService = new GameService(); + gameService.setTest(true); gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.FIVE)); boolean answer = gameService.legalMove(input); // assert assertEquals(expected, answer); } + @Test @DisplayName("ShuffelDeck") public void ShuffelDeck() { @@ -149,7 +164,7 @@ public class GameServiceTest { for (int i = 0; i < notshuffled.getGame().getDrawCardDeck().size(); i++) { if (shuffled.getGame().getDrawCardDeck().get(i).getValue() == notshuffled.getGame().getDrawCardDeck().get(i).getValue() && shuffled.getGame().getDrawCardDeck().get(i).getColor() == notshuffled.getGame().getDrawCardDeck().get(i).getColor()) { - counter ++; + counter++; } if (counter == notshuffled.getGame().getDrawCardDeck().size()) { fail("Deck wurde nicht richtig gemischt"); @@ -159,8 +174,7 @@ public class GameServiceTest { @Test @DisplayName("RelationshipGameService_Game") - public void RelationshipGameService_Game() - { + public void RelationshipGameService_Game() { // arrange GameService expected = new GameService(); //act @@ -168,10 +182,10 @@ public class GameServiceTest { // assert assertEquals(expected, answer); } + @Test @DisplayName("allRelationsTest") - public void allRelationsTest () - { + public void allRelationsTest() { // arrange GameService expected = new GameService(); //act @@ -186,39 +200,39 @@ public class GameServiceTest { @Test @DisplayName("SkipTest") - public void SkipTest () - { - GameService gameservice = new GameService(); - boolean expected = gameservice.getGame().getPlayerService().getCurrentTurn(); + public void SkipTest() { + GameService gameService = new GameService(); + gameService.setTest(true); + boolean expected = gameService.getGame().getPlayerService().getCurrentTurn(); - gameservice.playCard(new Card().setValue(Card.Value.SKIP).setColor(Card.Color.BLACK)); + gameService.playCard(new Card().setValue(Card.Value.SKIP).setColor(Card.Color.BLACK)); - assertEquals(expected, gameservice.getGame().getPlayerService().getCurrentTurn()); + assertEquals(expected, gameService.getGame().getPlayerService().getCurrentTurn()); } @Test @DisplayName("ReverseTest") - public void ReverseTest () - { - GameService gameservice = new GameService(); - boolean expected = gameservice.getGame().getPlayerService().getCurrentTurn(); + public void ReverseTest() { + GameService gameService = new GameService(); + gameService.setTest(true); + boolean expected = gameService.getGame().getPlayerService().getCurrentTurn(); - gameservice.playCard(new Card().setValue(Card.Value.REVERSE).setColor(Card.Color.BLACK)); + gameService.playCard(new Card().setValue(Card.Value.REVERSE).setColor(Card.Color.BLACK)); - assertEquals(expected, gameservice.getGame().getPlayerService().getCurrentTurn()); + assertEquals(expected, gameService.getGame().getPlayerService().getCurrentTurn()); } @Test @DisplayName("ChooseTest") - public void ChooseTest () - { - GameService gameservice = new GameService(); + public void ChooseTest() { + GameService gameService = new GameService(); + gameService.setTest(true); boolean expected = true; boolean answer = false; - gameservice.playCard(new Card().setValue(Card.Value.CHOOSE).setColor(Card.Color.BLACK)); + gameService.playCard(new Card().setValue(Card.Value.CHOOSE).setColor(Card.Color.BLACK)); - if (gameservice.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { + if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { answer = true; } @@ -228,27 +242,61 @@ public class GameServiceTest { @Test @DisplayName("ChooseDrawTest") - public void ChooseDrawTest () - { - GameService gameservice = new GameService(); + public void ChooseDrawTest() { + GameService gameService = new GameService(); + gameService.setTest(true); boolean expected = true; boolean answer = false; - gameservice.playCard(new Card().setValue(Card.Value.CHOOSEDRAW).setColor(Card.Color.BLACK)); + gameService.playCard(new Card().setValue(Card.Value.CHOOSEDRAW).setColor(Card.Color.BLACK)); - if (gameservice.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { + if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) { answer = true; } assertEquals(expected, answer); } + @Test + @DisplayName("DrawByChoiceForceKeep") + public void DrawByChoiceForceKeep() { + + GameService gameService = new GameService(); + gameService.setTest(true); + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); + + gameService.getGame().getDrawCardDeck().clear(); + gameService.getGame().getPlayerService().setCurrentTurn(true); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.GREEN).setValue(Card.Value.SEVEN)); + gameService.getGame().addCardToDrawCardDeck(input); + gameService.getGame().getPlayerService().getPlayerList().getFirst().addCardToPlayerDeck(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.TWO)); + gameService.drawCard(1); + + Card answer = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().getLast(); + + assertEquals(input, answer); + } + + @Test + @DisplayName("DrawCardPlayerDeckBot") + public void DrawCardPlayerDeckBot() { + GameService gameService = new GameService(); + gameService.setTest(true); + gameService.getGame().getPlayerService().setCurrentTurn(false); + int before = gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().size(); + gameService.drawCard(2); + int after = gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().size(); + + int expected = 2; + int answer = after - before; + assertEquals(expected, answer); + } @Test @DisplayName("DrawCardDeckTest") - public void DrawCardDeckTest () - { + public void DrawCardDeckTest() { GameService gameService = new GameService(); + gameService.setTest(true); int before = gameService.getGame().getDrawCardDeck().size(); gameService.drawCard(4); int after = gameService.getGame().getDrawCardDeck().size(); @@ -257,23 +305,173 @@ public class GameServiceTest { int answer = before - after; assertEquals(expected, answer); } + @Test + @DisplayName("DrawCardPlayerDeck") + public void DrawCardPlayerDeck() { + GameService gameService = new GameService(); + gameService.setTest(true); + gameService.getGame().getPlayerService().setCurrentTurn(true); + int before = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().size(); + gameService.drawCard(4); + int after = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().size(); + + int expected = 4; + int answer = after - before; + assertEquals(expected, answer); + } @Test - @DisplayName("DrawCardDeckRemoveTest") - public void DrawCardDeckRemoveTest () - { + @DisplayName("DrawByChoiceForcePlay") + public void DrawByChoiceForcePlay() { + GameService gameService = new GameService(); - Card card = gameService.getGame().getDrawCardDeck().getFirst(); + gameService.setTest(true); + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); + + gameService.getGame().getDrawCardDeck().clear(); + gameService.getGame().getPlayerService().setCurrentTurn(true); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.SEVEN)); + gameService.getGame().addCardToDrawCardDeck(input); + gameService.getGame().getPlayerService().getPlayerList().getFirst().addCardToPlayerDeck(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.TWO)); gameService.drawCard(1); - int counter = 0; - int expected = 0; + + Card answer = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().getLast(); + + assertNotEquals(input, answer); + } + + @Test + @DisplayName("CheckThatBotDeckHasSevenCardsAfterGameStart") + public void CheckThatBotDeckHasSevenCardsAfterGameStart() { + GameService gameService = new GameService(); + + gameService.setTest(true); + int expected = 7; + int actual = gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().size(); + + assertEquals(expected, actual); + } + + @Test + @DisplayName("CheckFirstCardIsSet") + public void CheckFirstCardIsSet() { + GameService gameService = new GameService(); + gameService.setTest(true); + Card card = gameService.getGame().getLastPlayedCard(); + + boolean answer = card != null; + + assertTrue(answer); + } + + @Test + @DisplayName("CheckFirstCardIsNotBlack") + public void CheckFirstCardIsNotBlack() { + GameService gameService = new GameService(); + gameService.setTest(true); + Card card = gameService.getGame().getLastPlayedCard(); + + boolean answer = card.getColor() != Card.Color.BLACK; + + assertTrue(answer); + } + + @Test + @DisplayName("CheckResetOnBlackCards") + public void CheckResetOnBlackCards() { + GameService gameService = new GameService(); + gameService.setTest(true); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); + gameService.playCard(new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE)); + gameService.playCard(new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.FIVE)); + + Card card = gameService.getGame().getDrawCardDeck().getLast(); + + assertEquals(Card.Color.BLACK, card.getColor()); + } + + @Test + @DisplayName("CheckThatPlayerReceivedSevenCards") + public void CheckThatPlayerReceivedSevenCards() { + GameService gameService = new GameService(); + + gameService.setTest(true); + int numberOfCards = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().size(); + + assertEquals(7, numberOfCards); + } + + @Test + @DisplayName("CheckThatFirstCardGetsRemovedFromDrawCardDeck") + public void CheckThatFirstCardGetsRemovedFromDrawCardDeck() { + GameService gameService = new GameService(); + + gameService.setTest(true); + Card lastPlayedCard = gameService.getGame().getLastPlayedCard(); + + boolean lastPlayedCardDoesNotExistInDrawCardDeck = true; + for (int i = 0; i < gameService.getGame().getDrawCardDeck().size(); i++) { - if (gameService.getGame().getDrawCardDeck().get(i).getColor() == card.getColor() && gameService.getGame().getDrawCardDeck().get(i).getValue() == card.getValue()) counter++; + if (gameService.getGame().getDrawCardDeck().get(i) == lastPlayedCard) { + lastPlayedCardDoesNotExistInDrawCardDeck = false; + break; + } } - if (card.getColor() == Card.Color.BLACK) expected = 3; - else expected = 1; - assertEquals(expected, counter); + assertTrue(lastPlayedCardDoesNotExistInDrawCardDeck); } + @Test + @DisplayName("ChooseColorBotTest") + public void ChooseColorBotTest() { + GameService gameService = new GameService(); + gameService.setTest(true); + Card card = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE); + boolean answer = false; + + gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().clear(); + gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().add(card); + + gameService.getGame().getPlayerService().setCurrentTurn(false); + gameService.playCard(card); + + if (gameService.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK){ + answer = true; + } + + assertTrue(answer); + } + + @Test + @DisplayName("CheckIfTestWasInitializedWithFalse") + public void CheckIfTestWasInitializedWithFalse() { + GameService gameService = new GameService(); + + assertFalse(gameService.getTest()); + } + + @Test + @DisplayName("CheckIfTestCanBeSetToTrue") + public void CheckIfTestCanBeSetToTrue() { + GameService gameService = new GameService(); + + gameService.setTest(true); + + assertTrue(gameService.getTest()); + } + + @Test + @DisplayName("CheckIfBotMovesAutomaticallyAfterPlayer") + public void CheckIfBotMovesAutomaticallyAfterPlayer() { + GameService gameService = new GameService(); + + Card card = new Card().setColor(Card.Color.BLUE).setValue(Card.Value.ONE); + gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().add(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.FIVE)); + + gameService.playCard(card); + + boolean answer = gameService.getGame().getLastPlayedCard() != card; + + assertTrue(answer); + } } diff --git a/src/test/java/de/hsfulda/onses/PlayerServiceTest.java b/src/test/java/de/hsfulda/onses/PlayerServiceTest.java index 5dc2fd2..c629c71 100644 --- a/src/test/java/de/hsfulda/onses/PlayerServiceTest.java +++ b/src/test/java/de/hsfulda/onses/PlayerServiceTest.java @@ -1,6 +1,9 @@ package de.hsfulda.onses; +import com.sun.tools.jconsole.JConsoleContext; +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.services.GameService; import de.hsfulda.onses.services.PlayerService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -90,4 +93,65 @@ public class PlayerServiceTest { assertEquals(expected, answer1); assertEquals(expected, answer2); } + + @Test + @DisplayName("CheckForCardRemoval") + void CheckForCardRemoval() { + GameService gameService = new GameService(); + + Card card1 = new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.TWO); + Card card2 = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); + + gameService.getGame().getPlayerService().setCurrentTurn(true); + + gameService.getGame().getPlayerService().getPlayerList().getFirst().addCardToPlayerDeck(card1); + gameService.getGame().getPlayerService().getPlayerList().getFirst().addCardToPlayerDeck(card2); + int before = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().size(); + + gameService.getGame().getPlayerService().removeCardFromPlayerDeck(card1); + int after = gameService.getGame().getPlayerService().getPlayerList().getFirst().getPlayerDeck().size(); + + assertEquals(before - 1, after); + } + + @Test + @DisplayName("CheckCardRemovalFromBotDeck") + void CheckCardRemovalFromBotDeck() { + GameService gameService = new GameService(); + + Card card1 = new Card().setColor(Card.Color.YELLOW).setValue(Card.Value.TWO); + Card card2 = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); + + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); + gameService.setTest(true); + gameService.getGame().getPlayerService().setCurrentTurn(false); + + gameService.getGame().getPlayerService().getPlayerList().getLast().addCardToPlayerDeck(card1); + gameService.getGame().getPlayerService().getPlayerList().getLast().addCardToPlayerDeck(card2); + int before = gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().size(); + + gameService.getGame().getPlayerService().botMove(); + int after = gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().size(); + + assertEquals(before - 1, after); + } + + @Test + @DisplayName("CheckThatLastPlayedCardWasPlayedByBot") + void CheckThatLastPlayedCardWasPlayedByBot() { + GameService gameService = new GameService(); + + Card card1 = new Card().setColor(Card.Color.RED).setValue(Card.Value.ONE); + + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); + gameService.getGame().getPlayerService().setCurrentTurn(false); + + gameService.getGame().getPlayerService().getPlayerList().getLast().getPlayerDeck().clear(); + gameService.getGame().getPlayerService().getPlayerList().getLast().addCardToPlayerDeck(card1); + + gameService.getGame().getPlayerService().botMove(); + Card lastPlayedCard = gameService.getGame().getLastPlayedCard(); + + assertEquals(card1, lastPlayedCard); + } }