Move card logic into card enum

This commit is contained in:
Arne Keller 2020-03-23 10:21:42 +01:00
parent ac50150bad
commit fe77053ac9
3 changed files with 72 additions and 23 deletions

View File

@ -39,6 +39,28 @@ public enum Card implements RequireDice {
*/
THUNDERSTORM;
/**
* Activate this card. Behaviour depends on the {@link #category category} of this card.
*
* @param game game to use card on
*/
public void activate(CardGame game) {
switch (this.category()) {
case RESOURCE: // simply add resource cards to the player's inventory
game.givePlayerCard(this);
break;
case ANIMAL: // animal cards trigger an encounter
game.startEncounter(this);
break;
case CATASTROPHE:
game.deletePlayerResources();
game.deletePlayerItem(Item.FIREPLACE);
break;
default: // implement behaviour of new card types here
throw new IllegalStateException("encountered unknown card category!");
}
}
/**
* Get the category of this card.
*

View File

@ -95,7 +95,7 @@ public class CardGame {
}
/**
* Draw a new card. Will automatically add the card to the player's resources or process the card action.
* Draw a new card. Will not process the card action ({@link Card#activate}).
*
* @return the card
* @throws LogicException if no card to draw exists or phase is not scavenge
@ -106,24 +106,49 @@ public class CardGame {
} else if (phase != Phase.SCAVENGE) {
throw new LogicException("can only draw cards in scavenge phase");
}
final Card card = cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
switch (card.category()) {
case RESOURCE: // simply add resource cards to the player's inventory
inventory.addResource(card);
break;
case ANIMAL: // animal cards trigger an encounter
requireDice = card;
phase = Phase.ENCOUNTER;
break;
case CATASTROPHE:
inventory.clearResources();
inventory.removeItem(Item.FIREPLACE);
break;
default: // implement game-related behaviour of new card types here
throw new IllegalStateException("encountered unknown card category!");
return cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
}
/**
* Give the player a card.
*
* @param card (resource) card
* @see Inventory#addResource
*/
public void givePlayerCard(Card card) {
this.inventory.addResource(card);
}
/**
* Start a new encounter. {@link #rollDice} has to be used next.
*
* @param card (animal) card
*/
public void startEncounter(Card card) {
if (!card.diceSizeNeeded().isPresent()) {
throw new IllegalArgumentException("card does not require dice");
}
checkLost();
return card;
requireDice = card;
phase = CardGame.Phase.ENCOUNTER;
}
/**
* Delete the player's resources.
*
* @see Inventory#clearResources
*/
public void deletePlayerResources() {
this.inventory.clearResources();
}
/**
* Delete one item the player owns.
*
* @param item item to remove
* @see Inventory#removeItem
*/
public void deletePlayerItem(Item item) {
this.inventory.removeItem(item);
}
/**
@ -152,7 +177,6 @@ public class CardGame {
// calculate fighting bonus, selecting the most powerful item the player owns
final int bonus = inventory.getItems().stream().mapToInt(Item::fightingBonus).max().orElse(0);
phase = Phase.SCAVENGE;
checkLost();
if (roll + bonus >= minimumNeeded) {
return SURVIVED;
} else {
@ -167,7 +191,6 @@ public class CardGame {
return WIN;
} else {
phase = Phase.SCAVENGE;
checkLost();
return LOSE;
}
}
@ -203,7 +226,6 @@ public class CardGame {
return WIN;
}
}
checkLost();
return OK;
}
}
@ -237,7 +259,8 @@ public class CardGame {
// can not roll dice
&& phase != Phase.ENCOUNTER && phase != Phase.ENDEAVOR
// can not build item
&& Arrays.stream(Item.values()).noneMatch(inventory::canBuild)) {
&& Arrays.stream(Item.values()).noneMatch(inventory::canBuild)
&& phase != Phase.WON) {
endGame();
phase = Phase.LOST;
}
@ -251,6 +274,7 @@ public class CardGame {
* @return whether the active game is lost
*/
public boolean gameLost() {
checkLost();
return phase == Phase.LOST;
}

View File

@ -1,6 +1,7 @@
package edu.kit.informatik.cardgame.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.cardgame.model.Card;
import edu.kit.informatik.cardgame.model.CardGame;
import edu.kit.informatik.cardgame.model.LogicException;
import edu.kit.informatik.cardgame.ui.InvalidInputException;
@ -19,7 +20,9 @@ public final class Draw extends Command {
@Override
public void apply(CardGame game) throws LogicException {
Terminal.printLine(game.draw());
final Card card = game.draw();
Terminal.printLine(card);
card.activate(game);
}
@Override