Browse Source
Merge branch 'AddGameService' into 'main'
Merge branch 'AddGameService' into 'main'
Add GameService See merge request fdai7736/onses!12main
fdai7736
11 months ago
2 changed files with 104 additions and 0 deletions
-
37src/main/java/de/hsfulda/onses/services/GameService.java
-
67src/test/java/de/hsfulda/onses/GameServiceTest.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; |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue