Checkstyle

This commit is contained in:
Arne Keller 2020-02-29 17:05:29 +01:00
parent 554a3a062e
commit 17e79b751a
12 changed files with 155 additions and 86 deletions

View File

@ -38,13 +38,13 @@ public class CardGame {
private Integer minimumDiceRoll = null; private Integer minimumDiceRoll = null;
/** /**
* * Start a new game with the specified stack of cards.
* @param cardStack order: 1st element = 1st to take * @param cardStack stack of cards to use, where the first element is the first to take
* @throws InvalidInputException if card stack has wrong distribution of cards * @throws InvalidInputException if card stack has wrong distribution of cards
* @return * @return whether the game could be successfully started
*/ */
public boolean start(Deque<Card> cardStack) throws InvalidInputException { public boolean start(Deque<Card> cardStack) throws InvalidInputException {
if (this.cardStack == null) { if (!gameStarted()) {
if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16 if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16
|| Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5 || Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5
|| Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5 || Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5
@ -65,7 +65,7 @@ public class CardGame {
} else if (awaitedDiceSize != null) { } else if (awaitedDiceSize != null) {
throw new InvalidInputException("roll dice, please"); throw new InvalidInputException("roll dice, please");
} }
Card card = currentCardStack.removeFirst(); final Card card = currentCardStack.removeFirst();
if (card.isResource()) { if (card.isResource()) {
resources.add(card); resources.add(card);
} else if (card.needsDiceRoll()) { } else if (card.needsDiceRoll()) {
@ -91,10 +91,10 @@ public class CardGame {
throw new InvalidInputException("impossible roll"); throw new InvalidInputException("impossible roll");
} }
awaitedDiceSize = null; awaitedDiceSize = null;
int minimumNeeded = minimumDiceRoll; final int minimumNeeded = minimumDiceRoll;
minimumDiceRoll = null; minimumDiceRoll = null;
if (fightingAnimal) { if (fightingAnimal) {
int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0; final int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0;
if (roll + bonus >= minimumNeeded) { if (roll + bonus >= minimumNeeded) {
return "survived"; return "survived";
} else { } else {
@ -113,7 +113,7 @@ public class CardGame {
} }
public void clearResources() { public void clearResources() {
int keepLastResources = items.contains(SHACK) ? 5 : 0; final int keepLastResources = items.contains(SHACK) ? 5 : 0;
while (resources.size() > keepLastResources) { while (resources.size() > keepLastResources) {
resources.removeFirst(); resources.removeFirst();
} }
@ -128,7 +128,7 @@ public class CardGame {
throw new InvalidInputException("already built"); throw new InvalidInputException("already built");
} else if (canBuild(item)) { } else if (canBuild(item)) {
// remove used resources // remove used resources
for (Card resource : item.resourcesNeeded()) { for (final Card resource : item.resourcesNeeded()) {
resources.removeLastOccurrence(resource); resources.removeLastOccurrence(resource);
} }
items.add(item); items.add(item);
@ -151,8 +151,8 @@ public class CardGame {
} }
private boolean canBuild(Item item) { private boolean canBuild(Item item) {
Card[] resourcesNeeded = item.resourcesNeeded(); final Card[] resourcesNeeded = item.resourcesNeeded();
for (Card resource : resources) { for (final Card resource : resources) {
for (int j = 0; j < resourcesNeeded.length; j++) { for (int j = 0; j < resourcesNeeded.length; j++) {
if (resourcesNeeded[j] != null && resourcesNeeded[j] == resource) { if (resourcesNeeded[j] != null && resourcesNeeded[j] == resource) {
resourcesNeeded[j] = null; resourcesNeeded[j] = null;

View File

@ -1,6 +1,6 @@
package edu.kit.informatik.ui; package edu.kit.informatik.ui;
public class InvalidInputException extends Exception { public class InvalidInputException extends RuntimeException {
public InvalidInputException(String message) { public InvalidInputException(String message) {
super(message); super(message);
} }

View File

@ -10,10 +10,14 @@ import java.util.regex.Pattern;
import static edu.kit.informatik.ui.command.CommandFactory.BUILD; import static edu.kit.informatik.ui.command.CommandFactory.BUILD;
public 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 Item item; private final Item item;
private Build(Item item) {
this.item = item;
}
@Override @Override
public void apply(CardGame game) throws InvalidInputException { public void apply(CardGame game) throws InvalidInputException {
@ -24,15 +28,21 @@ public class Build extends Command {
} }
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
if (input == null || !input.startsWith(BUILD)) { if (input == null || !input.startsWith(BUILD)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
Matcher matcher = BUILD_ARGUMENT.matcher(input); final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input);
if (!matcher.matches()) { if (!matcher.matches()) {
throw new InvalidInputException("invalid build command"); throw new InvalidInputException("invalid build command");
} }
item = Item.parse(matcher.group(1)); return new Build(Item.parse(matcher.group(1)));
} }
} }

View File

@ -10,10 +10,14 @@ import java.util.Set;
import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE;
public 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 InvalidInputException {
Set<Item> buildable = game.getBuildableItems(); final Set<Item> buildable = game.getBuildableItems();
if (buildable.isEmpty()) { if (buildable.isEmpty()) {
Terminal.printLine("EMPTY"); Terminal.printLine("EMPTY");
} else { } else {
@ -21,13 +25,20 @@ public class Buildable extends Command {
} }
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
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

@ -17,11 +17,4 @@ public abstract class Command {
* @throws InvalidInputException on invalid user input * @throws InvalidInputException on invalid user input
*/ */
public abstract void apply(CardGame game) throws InvalidInputException; public abstract void apply(CardGame game) throws InvalidInputException;
/**
* 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

@ -4,7 +4,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.Supplier; import java.util.function.Function;
/** /**
* Factory used to parse user input into commands. * Factory used to parse user input into commands.
@ -24,18 +24,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, Supplier<Command>> COMMANDS = new HashMap<>(); private static final Map<String, Function<String, Command>> COMMANDS = new HashMap<>();
static { static {
// initialize registered commands // initialize registered commands
COMMANDS.put(START, Start::new); COMMANDS.put(START, Start::parse);
COMMANDS.put(DRAW, Draw::new); COMMANDS.put(DRAW, Draw::parse);
COMMANDS.put(LIST_RESOURCES, ListResources::new); COMMANDS.put(LIST_RESOURCES, ListResources::parse);
COMMANDS.put(BUILD, Build::new); COMMANDS.put(BUILD, Build::parse);
COMMANDS.put(LIST_BUILDINGS, ListBuildings::new); COMMANDS.put(LIST_BUILDINGS, ListBuildings::parse);
COMMANDS.put(BUILDABLE, Buildable::new); COMMANDS.put(BUILDABLE, Buildable::parse);
COMMANDS.put(ROLL_DICE, RollDice::new); COMMANDS.put(ROLL_DICE, RollDice::parse);
COMMANDS.put(RESET, Reset::new); COMMANDS.put(RESET, Reset::parse);
} }
/** /**
@ -52,12 +52,11 @@ 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 {
for (Map.Entry<String, Supplier<Command>> entry : COMMANDS.entrySet()) { Function<String, Command> parser = COMMANDS.entrySet().stream()
if (input.startsWith(entry.getKey())) { .filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue)
Command command = entry.getValue().get(); .findFirst().orElse(null);
command.parse(input); if (parser != null) {
return command; return parser.apply(input);
}
} }
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }

View File

@ -1,35 +1,35 @@
package edu.kit.informatik.ui.command; 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.CardGame; import edu.kit.informatik.model.CardGame;
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 class Draw extends Command { public final class Draw extends Command {
@Override private Draw() {
public void apply(CardGame game) throws InvalidInputException {
Card card = game.draw();
Terminal.printLine(card);
} }
@Override @Override
public void parse(String input) throws InvalidInputException { public void apply(CardGame game) throws InvalidInputException {
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 {
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();
} }
/*
Es existieren folgende Fallunterscheidungen:
Wenn sich keine Karte mehr auf dem Stapel befindet bzw. der Befehl in einer nicht für
den Befehl vorhergesehenen Spielphase durchgeführt wird, ist dieser Spielzug nicht zulässig.
Entsprechend wird eine Fehlermeldung ausgegeben.
Zieht der Spieler eine Karte, so wird der Name der Ressource bzw. der englischsprachige
Bezeichner aus den o.g. Kategorien (i) Ressourcen, (ii) Tiere, (iii) Katastrophe ausgegeben.
Entsprechend der gezogenen Karte kann sich die Spielphase ändern.
*/
} }

View File

@ -9,7 +9,11 @@ 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 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 InvalidInputException {
List<Item> items = game.getItems(); List<Item> items = game.getItems();
@ -22,13 +26,20 @@ public class ListBuildings extends Command {
} }
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
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

@ -9,7 +9,11 @@ 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 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 InvalidInputException {
Deque<Card> resources = game.getResources(); Deque<Card> resources = game.getResources();
@ -20,13 +24,20 @@ public class ListResources extends Command {
} }
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
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

@ -6,20 +6,31 @@ 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 class Reset extends Command { public final class Reset extends Command {
private Reset() {
}
@Override @Override
public void apply(CardGame game) { public void apply(CardGame game) {
game.reset(); game.reset();
Terminal.printLine("OK"); Terminal.printLine("OK");
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
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

@ -9,28 +9,40 @@ import java.util.regex.Pattern;
import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE; import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE;
public 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 int size; private final int size;
private int rolledNumber; private final 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 InvalidInputException {
Terminal.printLine(game.rollDice(size, rolledNumber)); Terminal.printLine(game.rollDice(size, rolledNumber));
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
if (input == null || !input.startsWith(ROLL_DICE)) { if (input == null || !input.startsWith(ROLL_DICE)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input); final Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input);
if (!matcher.matches()) { if (!matcher.matches()) {
throw new InvalidInputException("invalid roll dice syntax"); throw new InvalidInputException("invalid roll dice syntax");
} }
size = Integer.parseInt(matcher.group(1)); final int size = Integer.parseInt(matcher.group(1));
rolledNumber = Integer.parseInt(matcher.group(2)); final int rolledNumber = Integer.parseInt(matcher.group(2));
return new RollDice(size, rolledNumber);
} }
} }

View File

@ -10,10 +10,14 @@ import java.util.Deque;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public 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 Deque<Card> cards; private final 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 InvalidInputException {
@ -24,23 +28,30 @@ public class Start extends Command {
} }
} }
@Override /**
public void parse(String input) throws InvalidInputException { * 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 {
if (input == null || !input.startsWith(CommandFactory.START)) { if (input == null || !input.startsWith(CommandFactory.START)) {
throw new InvalidInputException("unknown command"); throw new InvalidInputException("unknown command");
} }
Matcher matcher = START_ARGUMENTS.matcher(input); final Matcher matcher = START_ARGUMENTS.matcher(input);
if (!matcher.matches()) { if (!matcher.matches()) {
throw new InvalidInputException("invalid start arguments"); throw new InvalidInputException("invalid start arguments");
} }
cards = new ArrayDeque<>(); final Deque<Card> cards = new ArrayDeque<>();
for (String s : matcher.group(1).split(",")) { for (final String s : matcher.group(1).split(",")) {
Card card = Card.parse(s); final Card card = Card.parse(s);
if (card == null) { if (card == null) {
throw new InvalidInputException("invalid start argument value(s)"); throw new InvalidInputException("invalid start argument value(s)");
} }
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