mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-08 09:50:38 +00:00
Checkstyle
This commit is contained in:
parent
29a36ce1aa
commit
477b45c42b
@ -2,6 +2,12 @@ package edu.kit.informatik.cardgame;
|
||||
|
||||
import edu.kit.informatik.cardgame.ui.CommandLine;
|
||||
|
||||
/**
|
||||
* Main program class.
|
||||
*
|
||||
* @author Arne Keller
|
||||
* @version 1.0
|
||||
*/
|
||||
public final class Main {
|
||||
/**
|
||||
* Utility class -> private constructor.
|
||||
@ -13,7 +19,7 @@ public final class Main {
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command-line arguments
|
||||
* @param args command-line arguments (will be ignored)
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CommandLine.startInteractive();
|
||||
|
@ -39,6 +39,11 @@ public enum Card implements RequireDice {
|
||||
*/
|
||||
THUNDERSTORM;
|
||||
|
||||
/**
|
||||
* Get the category of this card.
|
||||
*
|
||||
* @return category of this card
|
||||
*/
|
||||
public CardCategory category() {
|
||||
switch (this) {
|
||||
case WOOD:
|
||||
@ -84,6 +89,11 @@ public enum Card implements RequireDice {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of cards of this type needed in a stack.
|
||||
*
|
||||
* @return amount of cards needed in a card deck
|
||||
*/
|
||||
public int requiredAmount() {
|
||||
switch (this) {
|
||||
case WOOD:
|
||||
@ -101,6 +111,13 @@ public enum Card implements RequireDice {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a single word and return the card represented by the input.
|
||||
* Fun fact: Card.parse(card.toString()) == card is true for all cards.
|
||||
*
|
||||
* @param input text
|
||||
* @return card object (null if input invalid)
|
||||
*/
|
||||
public static Card parse(String input) {
|
||||
switch (input) {
|
||||
case "wood":
|
||||
|
@ -33,11 +33,14 @@ public class CardGame {
|
||||
* Items built by the player.
|
||||
*/
|
||||
private final List<Item> items = new ArrayList<>();
|
||||
private RequireDice requireDice = null;
|
||||
/**
|
||||
* Current game phase.
|
||||
*/
|
||||
private Phase phase = null;
|
||||
private Phase phase;
|
||||
/**
|
||||
* Object currently requiring dice roll.
|
||||
*/
|
||||
private RequireDice requireDice;
|
||||
|
||||
/**
|
||||
* Start a new game with the specified stack of cards.
|
||||
|
@ -2,14 +2,25 @@ package edu.kit.informatik.cardgame.model;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Objects of classes implementing this optionally require a dice to be rolled.
|
||||
*
|
||||
* @author Arne Keller
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface RequireDice {
|
||||
/**
|
||||
* Get the size of the dice needed to activate this object.
|
||||
*
|
||||
* @return dice size needed to use this item (empty if dice not required)
|
||||
*/
|
||||
Optional<Integer> diceSizeNeeded();
|
||||
|
||||
|
||||
/**
|
||||
* Get the minimum dice roll needed to activate this object if it needs a dice roll.
|
||||
* Get the minimum dice roll needed to activate this object.
|
||||
*
|
||||
* @return minimum dice roll needed to use this item
|
||||
* @return minimum dice roll needed to use this item (empty if dice not required)
|
||||
*/
|
||||
Optional<Integer> minimumDiceRollNeeded();
|
||||
}
|
||||
|
@ -6,9 +6,6 @@ import edu.kit.informatik.cardgame.ui.command.Command;
|
||||
import edu.kit.informatik.cardgame.ui.command.CommandFactory;
|
||||
import edu.kit.informatik.Terminal;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Interactive game runner, gets user inputs and processes commands specified.
|
||||
*
|
||||
@ -35,10 +32,11 @@ public final class CommandLine {
|
||||
// create a new simulation
|
||||
CardGame simulation = new CardGame();
|
||||
boolean lost = false;
|
||||
|
||||
// accept user input indefinitely
|
||||
while (true) {
|
||||
String input = Terminal.readLine();
|
||||
|
||||
// handle quit command
|
||||
if (input.startsWith(QUIT)) {
|
||||
if (input.length() != QUIT.length()) {
|
||||
Terminal.printError("input after quit command");
|
||||
@ -49,19 +47,20 @@ public final class CommandLine {
|
||||
}
|
||||
|
||||
final Command command;
|
||||
|
||||
// attempt to parse user input
|
||||
try {
|
||||
command = CommandFactory.getCommand(input);
|
||||
} catch (NumberFormatException | InvalidInputException e) {
|
||||
Terminal.printError(e.getMessage());
|
||||
continue;
|
||||
}
|
||||
|
||||
// attempt to execute command
|
||||
try {
|
||||
command.apply(simulation);
|
||||
} catch (LogicException e) {
|
||||
Terminal.printError(e.getMessage());
|
||||
}
|
||||
// inform the player if he lost
|
||||
if (lost != simulation.gameLost() && simulation.gameLost()) {
|
||||
Terminal.printLine("lost");
|
||||
}
|
||||
|
@ -9,9 +9,8 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD;
|
||||
|
||||
public final class Build extends Command {
|
||||
public static final String NAME = "build";
|
||||
private static final Pattern BUILD_ARGUMENT = Pattern.compile(" (\\w+)");
|
||||
|
||||
private Item item;
|
||||
@ -28,10 +27,10 @@ public final class Build extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(BUILD)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(BUILD.length()));
|
||||
final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(NAME.length()));
|
||||
if (!matcher.matches()) {
|
||||
throw new InvalidInputException("invalid build command");
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Set;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILDABLE;
|
||||
|
||||
public final class Buildable extends Command {
|
||||
public static final String NAME = "build?";
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws LogicException {
|
||||
final Set<Item> buildable = game.getBuildableItems();
|
||||
@ -24,10 +24,10 @@ public final class Buildable extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(BUILDABLE)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
if (!input.equals(BUILDABLE)) {
|
||||
if (!input.equals(NAME)) {
|
||||
throw new InvalidInputException("invalid build? argument: none expected");
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package edu.kit.informatik.cardgame.ui.command;
|
||||
|
||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -10,30 +9,24 @@ import java.util.function.Supplier;
|
||||
* Factory used to parse user input into commands.
|
||||
*
|
||||
* @author Arne Keller
|
||||
* @version 1.1
|
||||
* @version 1.2
|
||||
*/
|
||||
public final class CommandFactory {
|
||||
public static final String START = "start";
|
||||
public static final String DRAW = "draw";
|
||||
public static final String LIST_RESOURCES = "list-resources";
|
||||
public static final String BUILD = "build";
|
||||
public static final String LIST_BUILDINGS = "list-buildings";
|
||||
public static final String BUILDABLE = "build?";
|
||||
public static final String ROLL_DICE = "rollD";
|
||||
public static final String RESET = "reset";
|
||||
|
||||
/**
|
||||
* Collection of command suppliers keyed by their respective command name.
|
||||
*/
|
||||
private static final TreeMap<String, Supplier<Command>> COMMANDS = new TreeMap<>();
|
||||
|
||||
static {
|
||||
// initialize registered commands
|
||||
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);
|
||||
COMMANDS.put(Start.NAME, Start::new);
|
||||
COMMANDS.put(Draw.NAME, Draw::new);
|
||||
COMMANDS.put(ListResources.NAME, ListResources::new);
|
||||
COMMANDS.put(Build.NAME, Build::new);
|
||||
COMMANDS.put(ListBuildings.NAME, ListBuildings::new);
|
||||
COMMANDS.put(Buildable.NAME, Buildable::new);
|
||||
COMMANDS.put(RollDice.NAME, RollDice::new);
|
||||
COMMANDS.put(Reset.NAME, Reset::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,13 +43,14 @@ public final class CommandFactory {
|
||||
* @throws InvalidInputException if user input is invalid
|
||||
*/
|
||||
public static Command getCommand(String input) throws InvalidInputException {
|
||||
Supplier<Command> commandSupplier = COMMANDS
|
||||
final Supplier<Command> commandSupplier = COMMANDS
|
||||
// match longest prefix
|
||||
.descendingKeySet().stream()
|
||||
.filter(input::startsWith)
|
||||
.map(COMMANDS::get)
|
||||
.findFirst().orElse(null);
|
||||
if (commandSupplier != null) {
|
||||
Command command = commandSupplier.get();
|
||||
final Command command = commandSupplier.get();
|
||||
command.parse(input);
|
||||
return command;
|
||||
} else {
|
||||
|
@ -5,9 +5,9 @@ import edu.kit.informatik.cardgame.model.CardGame;
|
||||
import edu.kit.informatik.cardgame.model.LogicException;
|
||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.DRAW;
|
||||
|
||||
public final class Draw extends Command {
|
||||
public static final String NAME = "draw";
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws LogicException {
|
||||
Terminal.printLine(game.draw());
|
||||
@ -15,10 +15,10 @@ public final class Draw extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(DRAW)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
if (!input.equals(DRAW)) {
|
||||
if (!input.equals(NAME)) {
|
||||
throw new InvalidInputException("invalid draw argument: none expected");
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.LIST_BUILDINGS;
|
||||
|
||||
public final class ListBuildings extends Command {
|
||||
public static final String NAME = "list-buildings";
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws LogicException {
|
||||
List<Item> items = game.getItems();
|
||||
@ -25,10 +25,10 @@ public final class ListBuildings extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(LIST_BUILDINGS)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
if (!input.equals(LIST_BUILDINGS)) {
|
||||
if (!input.equals(NAME)) {
|
||||
throw new InvalidInputException("invalid list-buildings argument: none expected");
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import java.util.Deque;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.LIST_RESOURCES;
|
||||
|
||||
public final class ListResources extends Command {
|
||||
public static final String NAME = "list-resources";
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws LogicException {
|
||||
Deque<Card> resources = game.getResources();
|
||||
@ -23,10 +23,10 @@ public final class ListResources extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(LIST_RESOURCES)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
if (!input.equals(LIST_RESOURCES)) {
|
||||
if (!input.equals(NAME)) {
|
||||
throw new InvalidInputException("invalid list-resources argument: none expected");
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,9 @@ import edu.kit.informatik.cardgame.model.CardGame;
|
||||
import edu.kit.informatik.cardgame.model.LogicException;
|
||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.RESET;
|
||||
|
||||
public final class Reset extends Command {
|
||||
public static final String NAME = "reset";
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws LogicException {
|
||||
game.reset();
|
||||
@ -16,10 +16,10 @@ public final class Reset extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(RESET)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
if (!input.equals(RESET)) {
|
||||
if (!input.equals(NAME)) {
|
||||
throw new InvalidInputException("invalid reset argument: none expected");
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.ROLL_DICE;
|
||||
|
||||
public final class RollDice extends Command {
|
||||
private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "\\+?(\\d+) \\+?(\\d+)");
|
||||
public static final String NAME = "rollD";
|
||||
private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(NAME + "\\+?(\\d+) \\+?(\\d+)");
|
||||
|
||||
private int size;
|
||||
|
||||
@ -24,7 +23,7 @@ public final class RollDice extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(ROLL_DICE)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
final Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input);
|
||||
|
@ -12,6 +12,7 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class Start extends Command {
|
||||
public static final String NAME = "start";
|
||||
private static final Pattern START_ARGUMENTS = Pattern.compile("start ((\\w+,){63}(\\w+))");
|
||||
|
||||
private Deque<Card> cards;
|
||||
@ -27,7 +28,7 @@ public final class Start extends Command {
|
||||
|
||||
@Override
|
||||
public void parse(String input) throws InvalidInputException {
|
||||
if (input == null || !input.startsWith(CommandFactory.START)) {
|
||||
if (input == null || !input.startsWith(NAME)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
final Matcher matcher = START_ARGUMENTS.matcher(input);
|
||||
|
Loading…
Reference in New Issue
Block a user