diff --git a/src/main/java/de/hsfulda/onses/services/GameService.java b/src/main/java/de/hsfulda/onses/services/GameService.java new file mode 100644 index 0000000..bdee4d5 --- /dev/null +++ b/src/main/java/de/hsfulda/onses/services/GameService.java @@ -0,0 +1,37 @@ +package de.hsfulda.onses.services; + +import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Game; +import de.hsfulda.onses.models.Player; + +public class GameService { + private final Game game; + public GameService(Game game) { + this.game = game; + } + public GameService() { + this(new Game()); + } + + public Game getGame() { + return game; + } + + public void playCard(Player player, Card card) + { + // add lastPlayedCard back to drawCardDeck + game.setLastPlayedCard(card); + // check for special rules (draw, colorchoose, skip,...) + } + + public boolean legalMove(Player player, Card card) + { + boolean legalMoveFound = false; + Card lastCard = game.getLastPlayedCard(); + // rules: + if (card.getColor() == lastCard.getColor()) legalMoveFound = true; // same color + + return legalMoveFound; + } + +} diff --git a/src/test/java/de/hsfulda/onses/GameServiceTest.java b/src/test/java/de/hsfulda/onses/GameServiceTest.java new file mode 100644 index 0000000..8a22108 --- /dev/null +++ b/src/test/java/de/hsfulda/onses/GameServiceTest.java @@ -0,0 +1,67 @@ + +package de.hsfulda.onses; + +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; +import de.hsfulda.onses.models.Player; +import de.hsfulda.onses.services.GameService; + +public class GameServiceTest { + @Test + @DisplayName("playCardRedEight") + public void playCardRedEight() { + // arrange + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.EIGHT); + // act + GameService gameService = new GameService(); + gameService.playCard(new Player(), input); + Card answer = gameService.getGame().getLastPlayedCard(); + // assert + assertEquals(input, answer); + } + @Test + @DisplayName("playCardBlackChoose") + public void playCardBlackChoose() { + // arrange + Card input = new Card().setColor(Card.Color.BLACK).setValue(Card.Value.CHOOSE); + // act + GameService gameService = new GameService(); + gameService.playCard(new Player(), input); + Card answer = gameService.getGame().getLastPlayedCard(); + // assert + assertEquals(input, answer); + } + + @Test + @DisplayName("legalMoveSameColorRed") + public void legalMoveSameColorRed() { + // arrange + Card input = new Card().setColor(Card.Color.RED).setValue(Card.Value.TWO); + boolean expected = true; + // act + GameService gameService = new GameService(); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE)); + + boolean answer = gameService.legalMove(new Player(), input); + // assert + assertEquals(expected, answer); + } + @Test + @DisplayName("legalMoveSameColorBlue") + public void legalMoveSameColorBlue() { + // arrange + Card input = new Card().setColor(Card.Color.BLUE).setValue(Card.Value.THREE); + boolean expected = true; + // act + GameService gameService = new GameService(); + gameService.getGame().setLastPlayedCard(new Card().setColor(Card.Color.BLUE).setValue(Card.Value.ONE)); + + boolean answer = gameService.legalMove(new Player(), input); + // assert + assertEquals(expected, answer); + } + +}