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 OK
Error, already built Error, already built
OK OK
OK Error, already built
axe
axe axe
fireplace fireplace
win win

View File

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

View File

@ -97,7 +97,15 @@ public final class Terminal {
*/ */
public static String readLine() { public static String readLine() {
try { 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) { } catch (final IOException e) {
/* /*
* The IOException will not occur during tests executed by the praktomat, therefore the * 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); 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) { public static Card parse(String input) {
switch (input) { switch (input) {
case "wood": case "wood":

View File

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

View File

@ -41,8 +41,15 @@ public enum Item {
return this.equals(STEAMBOAT) || this.equals(BALLON); return this.equals(STEAMBOAT) || this.equals(BALLON);
} }
public boolean canHaveMoreThanOne() { public int fightingBonus() {
return this.equals(AXE) || this.equals(CLUB); switch (this) {
case AXE:
return 2;
case CLUB:
return 1;
default:
return 0;
}
} }
public static Item parse(String input) { 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.model.CardGame;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.command.Command; import edu.kit.informatik.ui.command.Command;
import edu.kit.informatik.ui.command.CommandFactory; import edu.kit.informatik.ui.command.CommandFactory;
@ -36,12 +37,6 @@ public final class CommandLine {
while (true) { while (true) {
String input = Terminal.readLine(); String input = Terminal.readLine();
if (input == null) {
break; // TODO remove
}
if (input.startsWith("#")) {
continue; // TODO remove
}
if (input.startsWith("startn")) { if (input.startsWith("startn")) {
// TODO remove // TODO remove
StringJoiner j = new StringJoiner(","); StringJoiner j = new StringJoiner(",");
@ -54,15 +49,23 @@ public final class CommandLine {
Terminal.printError("input after quit command"); Terminal.printError("input after quit command");
continue; continue;
} else { } else {
break; return;
} }
} }
final Command command;
try { try {
Command command = CommandFactory.getCommand(input); command = CommandFactory.getCommand(input);
command.apply(simulation);
} catch (NumberFormatException | InvalidInputException e) { } catch (NumberFormatException | InvalidInputException e) {
Terminal.printError(e.getMessage()); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item; import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -13,14 +14,10 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILD;
public final class Build extends Command { public final class Build extends Command {
private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)"); private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)");
private final Item item; private Item item;
private Build(Item item) {
this.item = item;
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
final String result = game.build(item); final String result = game.build(item);
if (result == null) { if (result == null) {
Terminal.printError("could not build item"); Terminal.printError("could not build item");
@ -29,14 +26,8 @@ public final class Build extends Command {
} }
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(BUILD)) { if (input == null || !input.startsWith(BUILD)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
@ -44,6 +35,6 @@ public final class Build extends Command {
if (!matcher.matches()) { if (!matcher.matches()) {
throw new InvalidInputException("invalid build command"); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item; import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.Comparator; import java.util.Comparator;
@ -11,12 +12,8 @@ import java.util.Set;
import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE;
public final class Buildable extends Command { public final class Buildable extends Command {
private Buildable() {
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
final Set<Item> buildable = game.getBuildableItems(); final Set<Item> buildable = game.getBuildableItems();
if (buildable.isEmpty()) { if (buildable.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");
@ -25,20 +22,13 @@ public final class Buildable extends Command {
} }
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(BUILDABLE)) { if (input == null || !input.startsWith(BUILDABLE)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
if (!input.equals(BUILDABLE)) { if (!input.equals(BUILDABLE)) {
throw new InvalidInputException("invalid build? argument: none expected"); throw new InvalidInputException("invalid build? argument: none expected");
} }
return new Buildable();
} }
} }

View File

@ -1,6 +1,7 @@
package edu.kit.informatik.ui.command; package edu.kit.informatik.ui.command;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
/** /**
@ -14,7 +15,15 @@ public abstract class Command {
/** /**
* Apply this command to a game. * Apply this command to a game.
* @param game game to use * @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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
/** /**
* Factory used to parse user input into commands. * Factory used to parse user input into commands.
@ -13,8 +14,6 @@ import java.util.function.Function;
* @version 1.0 * @version 1.0
*/ */
public final class CommandFactory { 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 START = "start";
public static final String DRAW = "draw"; public static final String DRAW = "draw";
public static final String LIST_RESOURCES = "list-resources"; 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 ROLL_DICE = "rollD";
public static final String RESET = "reset"; 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 { static {
// initialize registered commands // initialize registered commands
COMMANDS.put(START, Start::parse); COMMANDS.put(START, Start::new);
COMMANDS.put(DRAW, Draw::parse); COMMANDS.put(DRAW, Draw::new);
COMMANDS.put(LIST_RESOURCES, ListResources::parse); COMMANDS.put(LIST_RESOURCES, ListResources::new);
COMMANDS.put(BUILD, Build::parse); COMMANDS.put(BUILD, Build::new);
COMMANDS.put(LIST_BUILDINGS, ListBuildings::parse); COMMANDS.put(LIST_BUILDINGS, ListBuildings::new);
COMMANDS.put(BUILDABLE, Buildable::parse); COMMANDS.put(BUILDABLE, Buildable::new);
COMMANDS.put(ROLL_DICE, RollDice::parse); COMMANDS.put(ROLL_DICE, RollDice::new);
COMMANDS.put(RESET, Reset::parse); COMMANDS.put(RESET, Reset::new);
} }
/** /**
@ -52,12 +51,15 @@ public final class CommandFactory {
* @throws InvalidInputException if user input is invalid * @throws InvalidInputException if user input is invalid
*/ */
public static Command getCommand(String input) throws InvalidInputException { 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) .filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue)
.findFirst().orElse(null); .findFirst().orElse(null);
if (parser != null) { if (commandSupplier != null) {
return parser.apply(input); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import static edu.kit.informatik.ui.command.CommandFactory.DRAW; import static edu.kit.informatik.ui.command.CommandFactory.DRAW;
public final class Draw extends Command { public final class Draw extends Command {
private Draw() {
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
Terminal.printLine(game.draw()); Terminal.printLine(game.draw());
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(DRAW)) { if (input == null || !input.startsWith(DRAW)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
if (!input.equals(DRAW)) { if (!input.equals(DRAW)) {
throw new InvalidInputException("invalid draw argument: none expected"); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.Item; import edu.kit.informatik.model.Item;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.List; import java.util.List;
@ -10,12 +11,8 @@ import java.util.List;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS; import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS;
public final class ListBuildings extends Command { public final class ListBuildings extends Command {
private ListBuildings() {
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
List<Item> items = game.getItems(); List<Item> items = game.getItems();
if (items.isEmpty()) { if (items.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");
@ -26,20 +23,13 @@ public final class ListBuildings extends Command {
} }
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(LIST_BUILDINGS)) { if (input == null || !input.startsWith(LIST_BUILDINGS)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
if (!input.equals(LIST_BUILDINGS)) { if (!input.equals(LIST_BUILDINGS)) {
throw new InvalidInputException("invalid list-buildings argument: none expected"); 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.Terminal;
import edu.kit.informatik.model.Card; import edu.kit.informatik.model.Card;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.Deque; import java.util.Deque;
@ -10,12 +11,8 @@ import java.util.Deque;
import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES; import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES;
public final class ListResources extends Command { public final class ListResources extends Command {
private ListResources() {
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
Deque<Card> resources = game.getResources(); Deque<Card> resources = game.getResources();
if (resources.isEmpty()) { if (resources.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");
@ -24,20 +21,13 @@ public final class ListResources extends Command {
} }
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(LIST_RESOURCES)) { if (input == null || !input.startsWith(LIST_RESOURCES)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
if (!input.equals(LIST_RESOURCES)) { if (!input.equals(LIST_RESOURCES)) {
throw new InvalidInputException("invalid list-resources argument: none expected"); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import static edu.kit.informatik.ui.command.CommandFactory.RESET; import static edu.kit.informatik.ui.command.CommandFactory.RESET;
public final class Reset extends Command { public final class Reset extends Command {
private Reset() {
}
@Override @Override
public void apply(CardGame game) { public void apply(CardGame game) throws LogicException {
game.reset(); game.reset();
Terminal.printLine("OK"); Terminal.printLine("OK");
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(RESET)) { if (input == null || !input.startsWith(RESET)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
if (!input.equals(RESET)) { if (!input.equals(RESET)) {
throw new InvalidInputException("invalid reset argument: none expected"); 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.Terminal;
import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.CardGame;
import edu.kit.informatik.model.LogicException;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Matcher; 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 { public final class RollDice extends Command {
private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)"); 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 int rolledNumber;
private RollDice(int size, int rolledNumber) {
this.size = size;
this.rolledNumber = rolledNumber;
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws LogicException {
Terminal.printLine(game.rollDice(size, rolledNumber)); Terminal.printLine(game.rollDice(size, rolledNumber));
} }
/** @Override
* Parse user input into this command object. public void parse(String input) throws InvalidInputException {
*
* @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 {
if (input == null || !input.startsWith(ROLL_DICE)) { if (input == null || !input.startsWith(ROLL_DICE)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
@ -41,8 +31,7 @@ public final class RollDice extends Command {
if (!matcher.matches()) { if (!matcher.matches()) {
throw new InvalidInputException("invalid roll dice syntax"); throw new InvalidInputException("invalid roll dice syntax");
} }
final int size = Integer.parseInt(matcher.group(1)); size = Integer.parseInt(matcher.group(1));
final int rolledNumber = Integer.parseInt(matcher.group(2)); rolledNumber = Integer.parseInt(matcher.group(2));
return new RollDice(size, rolledNumber);
} }
} }

View File

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