Update game logic to changed requirements and refactor command parsing

This commit is contained in:
Arne Keller 2020-03-11 10:51:29 +01:00
parent 8e3bd2bd25
commit 45f05292b4
17 changed files with 134 additions and 182 deletions

View File

@ -107,8 +107,7 @@ Error, need fireplace to build
OK
Error, already built
OK
OK
axe
Error, already built
axe
fireplace
win

View File

@ -56,9 +56,7 @@ shack
OK
OK
OK
axe
ballon
club
hangglider
sailingraft
shack

View File

@ -97,7 +97,15 @@ public final class Terminal {
*/
public static String readLine() {
try {
return IN.readLine();
String line = IN.readLine();
while (line != null && line.startsWith("#")) {
line = IN.readLine();
}
if (line == null) {
return "quit";
} else {
return line;
}
} catch (final IOException e) {
/*
* The IOException will not occur during tests executed by the praktomat, therefore the

View File

@ -47,6 +47,23 @@ public enum Card {
return this.equals(THUNDERSTORM);
}
public int requiredAmount() {
switch (this) {
case WOOD:
case METAL:
case PLASTIC:
return 16;
case SPIDER:
case SNAKE:
case TIGER:
return 5;
case THUNDERSTORM:
return 1;
default:
return 0;
}
}
public static Card parse(String input) {
switch (input) {
case "wood":

View File

@ -1,7 +1,5 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@ -12,16 +10,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import static edu.kit.informatik.model.Card.METAL;
import static edu.kit.informatik.model.Card.PLASTIC;
import static edu.kit.informatik.model.Card.SNAKE;
import static edu.kit.informatik.model.Card.SPIDER;
import static edu.kit.informatik.model.Card.THUNDERSTORM;
import static edu.kit.informatik.model.Card.TIGER;
import static edu.kit.informatik.model.Card.WOOD;
import static edu.kit.informatik.model.Item.AXE;
import static edu.kit.informatik.model.Item.BALLON;
import static edu.kit.informatik.model.Item.CLUB;
import static edu.kit.informatik.model.Item.FIREPLACE;
import static edu.kit.informatik.model.Item.HANG_GLIDER;
import static edu.kit.informatik.model.Item.SAILING_RAFT;
@ -31,8 +20,8 @@ import static edu.kit.informatik.model.Item.STEAMBOAT;
public class CardGame {
private Deque<Card> cardStack;
private Deque<Card> currentCardStack;
private Deque<Card> resources = new ArrayDeque<>();
private List<Item> items = new ArrayList<>();
private final Deque<Card> resources = new ArrayDeque<>();
private final List<Item> items = new ArrayList<>();
private boolean fightingAnimal = false;
private Integer awaitedDiceSize = null;
private Integer minimumDiceRoll = null;
@ -44,19 +33,19 @@ public class CardGame {
* @return whether the game could be successfully started
*/
public boolean start(Deque<Card> cardStack) throws LogicException {
if (cardStack == null || currentCardStack == null) {
if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16
|| Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5
|| Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5
|| Collections.frequency(cardStack, THUNDERSTORM) != 1) {
throw new LogicException("invalid deck: missing or surplus cards");
}
this.cardStack = new ArrayDeque<>(cardStack);
reset();
return true;
} else {
return false; // game already started
if (cardStack == null) {
throw new LogicException("can not initialize game with null cards");
}
if (gameStarted()) {
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 ArrayDeque<>(cardStack);
reset();
return true;
}
public Card draw() throws LogicException {
@ -94,7 +83,7 @@ public class CardGame {
final int minimumNeeded = minimumDiceRoll;
minimumDiceRoll = null;
if (fightingAnimal) {
final int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0;
final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0);
if (roll + bonus >= minimumNeeded) {
return "survived";
} else {
@ -125,7 +114,7 @@ public class CardGame {
throw new LogicException("can not build item");
} else if (item.requiresFireplace() && !items.contains(FIREPLACE)) {
throw new LogicException("need fireplace to build");
} else if (items.contains(item) && !item.canHaveMoreThanOne()) {
} else if (items.contains(item)) {
throw new LogicException("already built");
} else if (canBuild(item)) {
// remove used resources
@ -156,7 +145,7 @@ public class CardGame {
if (item.requiresFireplace() && !items.contains(FIREPLACE)) {
return false;
}
if (!item.canHaveMoreThanOne() && items.contains(item)) {
if (items.contains(item)) {
return false;
}
final Card[] resourcesNeeded = item.resourcesNeeded();

View File

@ -41,8 +41,15 @@ public enum Item {
return this.equals(STEAMBOAT) || this.equals(BALLON);
}
public boolean canHaveMoreThanOne() {
return this.equals(AXE) || this.equals(CLUB);
public int fightingBonus() {
switch (this) {
case AXE:
return 2;
case CLUB:
return 1;
default:
return 0;
}
}
public static Item parse(String input) {

View File

@ -2,6 +2,7 @@ package edu.kit.informatik.ui;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.command.Command;
import edu.kit.informatik.ui.command.CommandFactory;
@ -36,12 +37,6 @@ public final class CommandLine {
while (true) {
String input = Terminal.readLine();
if (input == null) {
break; // TODO remove
}
if (input.startsWith("#")) {
continue; // TODO remove
}
if (input.startsWith("startn")) {
// TODO remove
StringJoiner j = new StringJoiner(",");
@ -54,15 +49,23 @@ public final class CommandLine {
Terminal.printError("input after quit command");
continue;
} else {
break;
return;
}
}
final Command command;
try {
Command command = CommandFactory.getCommand(input);
command.apply(simulation);
command = CommandFactory.getCommand(input);
} catch (NumberFormatException | InvalidInputException e) {
Terminal.printError(e.getMessage());
continue;
}
try {
command.apply(simulation);
} catch (LogicException e) {
Terminal.printError(e.getMessage());
}
}
}

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher;
@ -13,14 +14,10 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILD;
public final class Build extends Command {
private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)");
private final Item item;
private Build(Item item) {
this.item = item;
}
private Item item;
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
final String result = game.build(item);
if (result == null) {
Terminal.printError("could not build item");
@ -29,14 +26,8 @@ public final class Build extends Command {
}
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(BUILD)) {
throw new InvalidInputException("unknown command");
}
@ -44,6 +35,6 @@ public final class Build extends Command {
if (!matcher.matches()) {
throw new InvalidInputException("invalid build command");
}
return new Build(Item.parse(matcher.group(1)));
item = Item.parse(matcher.group(1));
}
}

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.Comparator;
@ -11,12 +12,8 @@ import java.util.Set;
import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE;
public final class Buildable extends Command {
private Buildable() {
}
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
final Set<Item> buildable = game.getBuildableItems();
if (buildable.isEmpty()) {
Terminal.printLine("EMPTY");
@ -25,20 +22,13 @@ public final class Buildable extends Command {
}
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(BUILDABLE)) {
throw new InvalidInputException("unknown command");
}
if (!input.equals(BUILDABLE)) {
throw new InvalidInputException("invalid build? argument: none expected");
}
return new Buildable();
}
}

View File

@ -1,6 +1,7 @@
package edu.kit.informatik.ui.command;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
/**
@ -14,7 +15,15 @@ public abstract class Command {
/**
* Apply this command to a game.
* @param game game to use
* @throws InvalidInputException on invalid user input
* @throws LogicException on invalid user input
*/
public abstract void apply(CardGame game) throws InvalidInputException;
public abstract void apply(CardGame game) throws LogicException;
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @throws InvalidInputException if user input is in any way invalid
*/
public abstract void parse(String input) throws InvalidInputException;
}

View File

@ -5,6 +5,7 @@ import edu.kit.informatik.ui.InvalidInputException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Factory used to parse user input into commands.
@ -13,8 +14,6 @@ import java.util.function.Function;
* @version 1.0
*/
public final class CommandFactory {
public static final String CARD = "wood|metal|plastic|spider|snake|tiger|thunderstorm";
public static final String START = "start";
public static final String DRAW = "draw";
public static final String LIST_RESOURCES = "list-resources";
@ -24,18 +23,18 @@ public final class CommandFactory {
public static final String ROLL_DICE = "rollD";
public static final String RESET = "reset";
private static final Map<String, Function<String, Command>> COMMANDS = new HashMap<>();
private static final Map<String, Supplier<Command>> COMMANDS = new HashMap<>();
static {
// initialize registered commands
COMMANDS.put(START, Start::parse);
COMMANDS.put(DRAW, Draw::parse);
COMMANDS.put(LIST_RESOURCES, ListResources::parse);
COMMANDS.put(BUILD, Build::parse);
COMMANDS.put(LIST_BUILDINGS, ListBuildings::parse);
COMMANDS.put(BUILDABLE, Buildable::parse);
COMMANDS.put(ROLL_DICE, RollDice::parse);
COMMANDS.put(RESET, Reset::parse);
COMMANDS.put(START, Start::new);
COMMANDS.put(DRAW, Draw::new);
COMMANDS.put(LIST_RESOURCES, ListResources::new);
COMMANDS.put(BUILD, Build::new);
COMMANDS.put(LIST_BUILDINGS, ListBuildings::new);
COMMANDS.put(BUILDABLE, Buildable::new);
COMMANDS.put(ROLL_DICE, RollDice::new);
COMMANDS.put(RESET, Reset::new);
}
/**
@ -52,12 +51,15 @@ public final class CommandFactory {
* @throws InvalidInputException if user input is invalid
*/
public static Command getCommand(String input) throws InvalidInputException {
Function<String, Command> parser = COMMANDS.entrySet().stream()
Supplier<Command> commandSupplier = COMMANDS.entrySet().stream()
.filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue)
.findFirst().orElse(null);
if (parser != null) {
return parser.apply(input);
if (commandSupplier != null) {
Command command = commandSupplier.get();
command.parse(input);
return command;
} else {
throw new InvalidInputException("unknown command");
}
throw new InvalidInputException("unknown command");
}
}

View File

@ -2,34 +2,24 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import static edu.kit.informatik.ui.command.CommandFactory.DRAW;
public final class Draw extends Command {
private Draw() {
}
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
Terminal.printLine(game.draw());
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(DRAW)) {
throw new InvalidInputException("unknown command");
}
if (!input.equals(DRAW)) {
throw new InvalidInputException("invalid draw argument: none expected");
}
return new Draw();
}
}

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
@ -10,12 +11,8 @@ import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS;
public final class ListBuildings extends Command {
private ListBuildings() {
}
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
List<Item> items = game.getItems();
if (items.isEmpty()) {
Terminal.printLine("EMPTY");
@ -26,20 +23,13 @@ public final class ListBuildings extends Command {
}
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(LIST_BUILDINGS)) {
throw new InvalidInputException("unknown command");
}
if (!input.equals(LIST_BUILDINGS)) {
throw new InvalidInputException("invalid list-buildings argument: none expected");
}
return new ListBuildings();
}
}

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.Card;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.Deque;
@ -10,12 +11,8 @@ import java.util.Deque;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES;
public final class ListResources extends Command {
private ListResources() {
}
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
Deque<Card> resources = game.getResources();
if (resources.isEmpty()) {
Terminal.printLine("EMPTY");
@ -24,20 +21,13 @@ public final class ListResources extends Command {
}
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(LIST_RESOURCES)) {
throw new InvalidInputException("unknown command");
}
if (!input.equals(LIST_RESOURCES)) {
throw new InvalidInputException("invalid list-resources argument: none expected");
}
return new ListResources();
}
}

View File

@ -2,35 +2,25 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import static edu.kit.informatik.ui.command.CommandFactory.RESET;
public final class Reset extends Command {
private Reset() {
}
@Override
public void apply(CardGame game) {
public void apply(CardGame game) throws LogicException {
game.reset();
Terminal.printLine("OK");
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(RESET)) {
throw new InvalidInputException("unknown command");
}
if (!input.equals(RESET)) {
throw new InvalidInputException("invalid reset argument: none expected");
}
return new Reset();
}
}

View File

@ -2,6 +2,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher;
@ -12,28 +13,17 @@ import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE;
public final class RollDice extends Command {
private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)");
private final int size;
private int size;
private final int rolledNumber;
private RollDice(int size, int rolledNumber) {
this.size = size;
this.rolledNumber = rolledNumber;
}
private int rolledNumber;
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
Terminal.printLine(game.rollDice(size, rolledNumber));
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(ROLL_DICE)) {
throw new InvalidInputException("unknown command");
}
@ -41,8 +31,7 @@ public final class RollDice extends Command {
if (!matcher.matches()) {
throw new InvalidInputException("invalid roll dice syntax");
}
final int size = Integer.parseInt(matcher.group(1));
final int rolledNumber = Integer.parseInt(matcher.group(2));
return new RollDice(size, rolledNumber);
size = Integer.parseInt(matcher.group(1));
rolledNumber = Integer.parseInt(matcher.group(2));
}
}

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.Card;
import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.ArrayDeque;
@ -13,29 +14,19 @@ import java.util.regex.Pattern;
public final class Start extends Command {
private static final Pattern START_ARGUMENTS = Pattern.compile("start ((\\w+,){63}(\\w+))");
private final Deque<Card> cards;
private Start(Deque<Card> cards) {
this.cards = cards;
}
private Deque<Card> cards;
@Override
public void apply(CardGame game) throws InvalidInputException {
public void apply(CardGame game) throws LogicException {
if (game.start(cards)) {
Terminal.printLine("OK");
} else {
throw new InvalidInputException("could not start game");
throw new LogicException("could not start game");
}
}
/**
* Parse user input into this command object.
*
* @param input one line of user input
* @return fully initialized command
* @throws InvalidInputException if user input is in any way invalid
*/
public static Command parse(String input) throws InvalidInputException {
@Override
public void parse(String input) throws InvalidInputException {
if (input == null || !input.startsWith(CommandFactory.START)) {
throw new InvalidInputException("unknown command");
}
@ -43,7 +34,7 @@ public final class Start extends Command {
if (!matcher.matches()) {
throw new InvalidInputException("invalid start arguments");
}
final Deque<Card> cards = new ArrayDeque<>();
cards = new ArrayDeque<>();
for (final String s : matcher.group(1).split(",")) {
final Card card = Card.parse(s);
if (card == null) {
@ -51,7 +42,6 @@ public final class Start extends Command {
}
cards.add(card);
}
return new Start(cards);
}
/*
Dieser Befehl ermöglicht es dem Benutzer, ein neues Spiel zu starten. Dieser Befehl kann nur