mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-08 18:00:37 +00:00
Various stylistic changes
This commit is contained in:
parent
cc4e75f64e
commit
64f517d201
@ -77,20 +77,18 @@ public enum Card implements RequireDice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String activate(CardGame game, int diceSize, int roll) throws LogicException {
|
public Optional<String> activate(CardGame game, int diceSize, int roll) {
|
||||||
if (!this.minimumDiceRollNeeded().isPresent()) {
|
if (!this.diceSizeNeeded().isPresent()) {
|
||||||
throw new IllegalStateException("can not process dice roll");
|
throw new IllegalStateException("can not process dice roll");
|
||||||
} else if (this.diceSizeNeeded().get() != diceSize) {
|
} else if (this.diceSizeNeeded().get() != diceSize || roll > diceSize || roll < 1) {
|
||||||
throw new LogicException("unexpected dice size");
|
return Optional.empty();
|
||||||
} else if (roll > diceSize || roll < 1) {
|
|
||||||
throw new LogicException("impossible roll");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roll + game.getFightingBonus() >= this.minimumDiceRollNeeded().get()) {
|
if (roll + game.getFightingBonus() >= this.minimumDiceRollNeeded().get()) {
|
||||||
return SURVIVED;
|
return Optional.of(SURVIVED);
|
||||||
} else {
|
} else {
|
||||||
game.deletePlayerResources();
|
game.deletePlayerResources();
|
||||||
return LOSE;
|
return Optional.of(LOSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,8 @@ package edu.kit.informatik.cardgame.model;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -59,10 +59,6 @@ public class CardGame {
|
|||||||
// do not start a new game if one is active already
|
// do not start a new game if one is active already
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!Arrays.stream(Card.values())
|
|
||||||
.allMatch(card -> Collections.frequency(cardStack, card) == card.requiredAmount())) {
|
|
||||||
throw new LogicException("invalid deck: missing or surplus cards");
|
|
||||||
}
|
|
||||||
this.cardStack = new CardStack(cardStack);
|
this.cardStack = new CardStack(cardStack);
|
||||||
this.inventory = new Inventory();
|
this.inventory = new Inventory();
|
||||||
this.phase = Phase.SCAVENGE;
|
this.phase = Phase.SCAVENGE;
|
||||||
@ -72,7 +68,7 @@ public class CardGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw a new card. Will not process the card action ({@link Card#activate}).
|
* Draw a new card. Will automatically process the card action ({@link Card#activate}).
|
||||||
*
|
*
|
||||||
* @return the card
|
* @return the card
|
||||||
* @throws LogicException if no card to draw exists or phase is not scavenge
|
* @throws LogicException if no card to draw exists or phase is not scavenge
|
||||||
@ -83,7 +79,9 @@ public class CardGame {
|
|||||||
} else if (phase != Phase.SCAVENGE) {
|
} else if (phase != Phase.SCAVENGE) {
|
||||||
throw new LogicException("can only draw cards in scavenge phase");
|
throw new LogicException("can only draw cards in scavenge phase");
|
||||||
}
|
}
|
||||||
return cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
|
final Card card = cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
|
||||||
|
card.activate(this);
|
||||||
|
return card;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,20 +135,23 @@ public class CardGame {
|
|||||||
* @throws LogicException if not expecting dice roll or dice roll is invalid
|
* @throws LogicException if not expecting dice roll or dice roll is invalid
|
||||||
*/
|
*/
|
||||||
public String rollDice(int size, int roll) throws LogicException {
|
public String rollDice(int size, int roll) throws LogicException {
|
||||||
if (requireDice == null) {
|
if (phase != Phase.AWAITING_DICE_ROLL) {
|
||||||
throw new LogicException("not expecting dice roll");
|
throw new LogicException("not expecting dice roll");
|
||||||
} else if (requireDice.diceSizeNeeded().get() != size) {
|
|
||||||
throw new LogicException("unexpected dice size");
|
|
||||||
} else if (roll > size || roll < 1) {
|
|
||||||
throw new LogicException("impossible roll");
|
|
||||||
}
|
}
|
||||||
// leave encounter/endeavor phase
|
|
||||||
phase = Phase.SCAVENGE;
|
|
||||||
// compute the result of the dice roll
|
// compute the result of the dice roll
|
||||||
final String result = requireDice.activate(this, size, roll);
|
final Optional<String> result = requireDice.activate(this, size, roll);
|
||||||
// no longer waiting
|
if (result.isPresent()) {
|
||||||
requireDice = null;
|
// leave encounter/endeavor phase (only if player didn't win)
|
||||||
return result;
|
if (phase == Phase.AWAITING_DICE_ROLL) {
|
||||||
|
phase = Phase.SCAVENGE;
|
||||||
|
}
|
||||||
|
// no longer waiting
|
||||||
|
requireDice = null;
|
||||||
|
return result.get();
|
||||||
|
} else {
|
||||||
|
// invalid user input
|
||||||
|
throw new LogicException("invalid dice input");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -182,14 +183,6 @@ public class CardGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* End the game by forcing the player to win.
|
|
||||||
*/
|
|
||||||
public void winGame() {
|
|
||||||
endGame();
|
|
||||||
phase = Phase.WON;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether this game ever received a {@link #start start} command.
|
* Check whether this game ever received a {@link #start start} command.
|
||||||
*
|
*
|
||||||
@ -228,6 +221,22 @@ public class CardGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End the game by forcing the player to win.
|
||||||
|
*/
|
||||||
|
public void winGame() {
|
||||||
|
endGame();
|
||||||
|
phase = Phase.WON;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End the current game, deleting the active card stack.
|
||||||
|
* Player resources and items are not deleted.
|
||||||
|
*/
|
||||||
|
private void endGame() {
|
||||||
|
cardStack.clearActiveStack();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the active game is lost.
|
* Check whether the active game is lost.
|
||||||
* A game is lost if no more player actions ({@link #draw}, {@link #build}, {@link #rollDice}) are possible and
|
* A game is lost if no more player actions ({@link #draw}, {@link #build}, {@link #rollDice}) are possible and
|
||||||
@ -281,14 +290,6 @@ public class CardGame {
|
|||||||
return Arrays.stream(Item.values()).filter(inventory::canBuild).collect(Collectors.toSet());
|
return Arrays.stream(Item.values()).filter(inventory::canBuild).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* End the current game, deleting the active card stack.
|
|
||||||
* Player resources and items are not deleted.
|
|
||||||
*/
|
|
||||||
private void endGame() {
|
|
||||||
cardStack.clearActiveStack();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the game, restoring the {@link #cardStack original card stack} and clearing the player's inventory.
|
* Reset the game, restoring the {@link #cardStack original card stack} and clearing the player's inventory.
|
||||||
*
|
*
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ -28,8 +30,13 @@ public class CardStack {
|
|||||||
* The first element will be {@link #draw drawn} first.
|
* The first element will be {@link #draw drawn} first.
|
||||||
*
|
*
|
||||||
* @param cards (ordered) card collection
|
* @param cards (ordered) card collection
|
||||||
|
* @throws LogicException if card stack has wrong distribution of cards
|
||||||
*/
|
*/
|
||||||
public CardStack(Collection<Card> cards) {
|
public CardStack(Collection<Card> cards) throws LogicException {
|
||||||
|
if (!Arrays.stream(Card.values())
|
||||||
|
.allMatch(card -> Collections.frequency(cards, card) == card.requiredAmount())) {
|
||||||
|
throw new LogicException("invalid deck: missing or surplus cards");
|
||||||
|
}
|
||||||
this.stack = new ArrayDeque<>(cards);
|
this.stack = new ArrayDeque<>(cards);
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
@ -91,20 +91,18 @@ public enum Item implements RequireDice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String activate(CardGame game, int diceSize, int roll) throws LogicException {
|
public Optional<String> activate(CardGame game, int diceSize, int roll) {
|
||||||
if (!this.minimumDiceRollNeeded().isPresent()) {
|
if (!this.diceSizeNeeded().isPresent()) {
|
||||||
throw new IllegalStateException("can not process dice roll");
|
throw new IllegalStateException("can not process dice roll");
|
||||||
} else if (this.diceSizeNeeded().get() != diceSize) {
|
} else if (this.diceSizeNeeded().get() != diceSize || roll > diceSize || roll < 1) {
|
||||||
throw new LogicException("unexpected dice size");
|
return Optional.empty();
|
||||||
} else if (roll > diceSize || roll < 1) {
|
|
||||||
throw new LogicException("impossible roll");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roll >= this.minimumDiceRollNeeded().get()) {
|
if (roll >= this.minimumDiceRollNeeded().get()) {
|
||||||
game.winGame();
|
game.winGame();
|
||||||
return WIN;
|
return Optional.of(WIN);
|
||||||
} else {
|
} else {
|
||||||
return LOSE;
|
return Optional.of(LOSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +28,8 @@ public interface RequireDice {
|
|||||||
*
|
*
|
||||||
* @param game card game to use
|
* @param game card game to use
|
||||||
* @param diceSize size of the dice rolled
|
* @param diceSize size of the dice rolled
|
||||||
* @param rolled result of the dice roll
|
* @param roll result of the dice roll
|
||||||
* @return activation result
|
* @return activation result (empty if dice roll is incorrect)
|
||||||
* @throws LogicException if dice roll is incorrect
|
|
||||||
*/
|
*/
|
||||||
String activate(CardGame game, int diceSize, int rolled) throws LogicException;
|
Optional<String> activate(CardGame game, int diceSize, int roll);
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,9 @@ public final class CommandLine {
|
|||||||
if (input.startsWith(QUIT)) {
|
if (input.startsWith(QUIT)) {
|
||||||
if (input.length() != QUIT.length()) {
|
if (input.length() != QUIT.length()) {
|
||||||
Terminal.printError("input after quit command");
|
Terminal.printError("input after quit command");
|
||||||
continue;
|
continue; // skip invalid quit command
|
||||||
} else {
|
} else {
|
||||||
return;
|
return; // quit main loop otherwise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package edu.kit.informatik.cardgame.ui.command;
|
package edu.kit.informatik.cardgame.ui.command;
|
||||||
|
|
||||||
import edu.kit.informatik.Terminal;
|
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.CardGame;
|
||||||
import edu.kit.informatik.cardgame.model.LogicException;
|
import edu.kit.informatik.cardgame.model.LogicException;
|
||||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||||
@ -20,9 +19,7 @@ public final class Draw extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws LogicException {
|
public void apply(CardGame game) throws LogicException {
|
||||||
final Card card = game.draw();
|
Terminal.printLine(game.draw());
|
||||||
Terminal.printLine(card);
|
|
||||||
card.activate(game);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user