Browse Source

Merge branch 'AddMoreRules' into 'main'

Add more rules

See merge request fdai7736/onses!17
main
fdai7906 11 months ago
parent
commit
e4868bc32f
  1. 3
      src/main/java/de/hsfulda/onses/models/Game.java
  2. 49
      src/main/java/de/hsfulda/onses/services/GameService.java
  3. 94
      src/test/java/de/hsfulda/onses/GameServiceTest.java

3
src/main/java/de/hsfulda/onses/models/Game.java

@ -18,6 +18,9 @@ public class Game {
return lastPlayedCard;
}
public void changeLastPlayedCardColor(Card.Color color) {
lastPlayedCard.setColor(color);
}
public Game setLastPlayedCard(Card lastPlayedCard) {
this.lastPlayedCard = lastPlayedCard;
return this;

49
src/main/java/de/hsfulda/onses/services/GameService.java

@ -11,6 +11,8 @@ public class GameService {
public GameService(Game game) {
this.game = game;
this.game.setGameService(this);
fillDrawDeck();
shuffleDeck();
}
public GameService() {
this(new Game());
@ -20,11 +22,58 @@ public class GameService {
return game;
}
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();
}
}
public void playCard(Player player, 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();
}
// Reverse
if (card.getValue() == Card.Value.REVERSE) {
nextPlayer();
nextPlayer();
}
// Choose
if (card.getValue() == Card.Value.CHOOSE) {
// Abfrage Farbe
Card.Color color = Card.Color.BLUE; //Vorübergehend Blau
game.changeLastPlayedCardColor(color);
nextPlayer();
}
// Draw
if (card.getValue() == Card.Value.DRAWTWO) {
nextPlayer();
drawCard(2);
nextPlayer();
}
// ChooseDraw
if (card.getValue() == Card.Value.CHOOSEDRAW) {
// Abfrage Farbe
Card.Color color = Card.Color.BLUE; //Vorübergehend Blau
game.changeLastPlayedCardColor(color);
nextPlayer();
drawCard(4);
nextPlayer();
}
}
public boolean legalMove(Player player, Card card)

94
src/test/java/de/hsfulda/onses/GameServiceTest.java

@ -68,7 +68,7 @@ public class GameServiceTest {
@DisplayName("FillDrawCardDeck")
public void FillDrawCardDeck() {
GameService gameService = new GameService();
int expected = 112;
int expected = gameService.getGame().getDrawCardDeck().size() + 112;
gameService.fillDrawDeck();
int answer = gameService.getGame().getDrawCardDeck().size();
@ -185,4 +185,96 @@ public class GameServiceTest {
assertEquals(expected, answer2);
}
@Test
@DisplayName("SkipTest")
public void SkipTest ()
{
GameService gameservice = new GameService();
boolean expected = gameservice.getGame().getPlayerService().getCurrentTurn();
gameservice.playCard(new Player(), new Card().setValue(Card.Value.SKIP).setColor(Card.Color.BLACK));
assertEquals(expected, gameservice.getGame().getPlayerService().getCurrentTurn());
}
@Test
@DisplayName("ReverseTest")
public void ReverseTest ()
{
GameService gameservice = new GameService();
boolean expected = gameservice.getGame().getPlayerService().getCurrentTurn();
gameservice.playCard(new Player(), new Card().setValue(Card.Value.REVERSE).setColor(Card.Color.BLACK));
assertEquals(expected, gameservice.getGame().getPlayerService().getCurrentTurn());
}
@Test
@DisplayName("ChooseTest")
public void ChooseTest ()
{
GameService gameservice = new GameService();
boolean expected = true;
boolean answer = false;
gameservice.playCard(new Player(), new Card().setValue(Card.Value.CHOOSE).setColor(Card.Color.BLACK));
if (gameservice.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) {
answer = true;
}
assertEquals(expected, answer);
}
@Test
@DisplayName("ChooseDrawTest")
public void ChooseDrawTest ()
{
GameService gameservice = new GameService();
boolean expected = true;
boolean answer = false;
gameservice.playCard(new Player(), new Card().setValue(Card.Value.CHOOSEDRAW).setColor(Card.Color.BLACK));
if (gameservice.getGame().getLastPlayedCard().getColor() != Card.Color.BLACK) {
answer = true;
}
assertEquals(expected, answer);
}
@Test
@DisplayName("DrawCardDeckTest")
public void DrawCardDeckTest ()
{
GameService gameService = new GameService();
int before = gameService.getGame().getDrawCardDeck().size();
gameService.drawCard(4);
int after = gameService.getGame().getDrawCardDeck().size();
int expected = 4;
int answer = before - after;
assertEquals(expected, answer);
}
@Test
@DisplayName("DrawCardDeckRemoveTest")
public void DrawCardDeckRemoveTest ()
{
GameService gameService = new GameService();
Card card = gameService.getGame().getDrawCardDeck().getFirst();
gameService.drawCard(1);
int counter = 0;
int expected = 0;
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 (card.getColor() == Card.Color.BLACK) expected = 3;
else expected = 1;
assertEquals(expected, counter);
}
}
Loading…
Cancel
Save