mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-24 01:15:03 +00:00
Initial command parser
This commit is contained in:
parent
63d0af1f00
commit
343ae03421
32
src/edu/kit/informatik/model/Card.java
Normal file
32
src/edu/kit/informatik/model/Card.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package edu.kit.informatik.model;
|
||||||
|
|
||||||
|
public enum Card {
|
||||||
|
WOOD,
|
||||||
|
METAL,
|
||||||
|
PLASTIC,
|
||||||
|
SPIDER,
|
||||||
|
SNAKE,
|
||||||
|
TIGER,
|
||||||
|
THUNDERSTORM;
|
||||||
|
|
||||||
|
public static Card parse(String input) {
|
||||||
|
switch (input) {
|
||||||
|
case "wood":
|
||||||
|
return WOOD;
|
||||||
|
case "metal":
|
||||||
|
return METAL;
|
||||||
|
case "plastic":
|
||||||
|
return PLASTIC;
|
||||||
|
case "spider":
|
||||||
|
return SPIDER;
|
||||||
|
case "snake":
|
||||||
|
return SNAKE;
|
||||||
|
case "tiger":
|
||||||
|
return TIGER;
|
||||||
|
case "thunderstorm":
|
||||||
|
return THUNDERSTORM;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,16 @@ package edu.kit.informatik.ui.command;
|
|||||||
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 java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static edu.kit.informatik.ui.command.CommandFactory.BUILD;
|
||||||
|
|
||||||
public class Build extends Command {
|
public class Build extends Command {
|
||||||
|
private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)");
|
||||||
|
|
||||||
|
private String item;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) throws InvalidInputException {
|
||||||
|
|
||||||
@ -11,6 +20,13 @@ public class Build extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
public void parse(String input) throws InvalidInputException {
|
||||||
|
if (input == null || !input.startsWith(BUILD)) {
|
||||||
|
throw new InvalidInputException("unknown command");
|
||||||
|
}
|
||||||
|
Matcher matcher = BUILD_ARGUMENT.matcher(input);
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new InvalidInputException("invalid build command");
|
||||||
|
}
|
||||||
|
item = matcher.group(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,21 @@ package edu.kit.informatik.ui.command;
|
|||||||
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.BUILDABLE;
|
||||||
|
|
||||||
public class Buildable extends Command {
|
public class Buildable extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ import java.util.function.Supplier;
|
|||||||
* @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";
|
||||||
|
@ -3,6 +3,8 @@ package edu.kit.informatik.ui.command;
|
|||||||
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;
|
||||||
|
|
||||||
public class Draw extends Command {
|
public class Draw extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) throws InvalidInputException {
|
||||||
@ -11,7 +13,12 @@ public class Draw extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Es existieren folgende Fallunterscheidungen:
|
Es existieren folgende Fallunterscheidungen:
|
||||||
|
@ -3,6 +3,8 @@ package edu.kit.informatik.ui.command;
|
|||||||
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.LIST_BUILDINGS;
|
||||||
|
|
||||||
public class ListBuildings extends Command {
|
public class ListBuildings extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) throws InvalidInputException {
|
||||||
@ -11,6 +13,11 @@ public class ListBuildings extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,22 @@ package edu.kit.informatik.ui.command;
|
|||||||
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.LIST_RESOURCES;
|
||||||
|
|
||||||
public class ListResources extends Command {
|
public class ListResources extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package edu.kit.informatik.ui.command;
|
|
||||||
|
|
||||||
import edu.kit.informatik.model.CardGame;
|
|
||||||
import edu.kit.informatik.ui.InvalidInputException;
|
|
||||||
|
|
||||||
public class Quit extends Command {
|
|
||||||
@Override
|
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void parse(String input) throws InvalidInputException {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,14 +3,21 @@ package edu.kit.informatik.ui.command;
|
|||||||
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.RESET;
|
||||||
|
|
||||||
public class Reset extends Command {
|
public class Reset extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,18 @@ package edu.kit.informatik.ui.command;
|
|||||||
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 java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE;
|
||||||
|
|
||||||
public class RollDice extends Command {
|
public class RollDice extends Command {
|
||||||
|
private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)");
|
||||||
|
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
private int rolledNumber;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) throws InvalidInputException {
|
||||||
|
|
||||||
@ -11,6 +22,14 @@ public class RollDice extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
public void parse(String input) throws InvalidInputException {
|
||||||
|
if (input == null || !input.startsWith(ROLL_DICE)) {
|
||||||
|
throw new InvalidInputException("unknown command");
|
||||||
|
}
|
||||||
|
Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input);
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new InvalidInputException("invalid roll dice syntax");
|
||||||
|
}
|
||||||
|
size = Integer.parseInt(matcher.group(1));
|
||||||
|
rolledNumber = Integer.parseInt(matcher.group(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,45 @@
|
|||||||
package edu.kit.informatik.ui.command;
|
package edu.kit.informatik.ui.command;
|
||||||
|
|
||||||
|
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 java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import static edu.kit.informatik.ui.command.CommandFactory.CARD;
|
||||||
|
|
||||||
public class Start extends Command {
|
public class Start extends Command {
|
||||||
|
private static final Pattern START_ARGUMENTS = Pattern.compile("start (?:(" + CARD + "),){63},(" + CARD + ")");
|
||||||
|
|
||||||
|
private List<Card> cards;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws InvalidInputException {
|
public void apply(CardGame game) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse(String input) throws InvalidInputException {
|
public void parse(String input) throws InvalidInputException {
|
||||||
|
if (input == null || !input.startsWith(CommandFactory.START)) {
|
||||||
|
throw new InvalidInputException("unknown command");
|
||||||
|
}
|
||||||
|
Matcher matcher = START_ARGUMENTS.matcher(input);
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new InvalidInputException("invalid start arguments");
|
||||||
|
}
|
||||||
|
cards = IntStream.rangeClosed(1, matcher.groupCount())
|
||||||
|
.mapToObj(matcher::group)
|
||||||
|
.map(Card::parse)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (cards.stream().anyMatch(Objects::isNull)) {
|
||||||
|
cards = null;
|
||||||
|
throw new InvalidInputException("invalid start argument value(s)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user