mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-08 18:00:37 +00:00
Implement some commands partially
This commit is contained in:
parent
343ae03421
commit
59a2f86a32
@ -8,7 +8,11 @@ public enum Card {
|
||||
SNAKE,
|
||||
TIGER,
|
||||
THUNDERSTORM;
|
||||
|
||||
|
||||
public boolean isResource() {
|
||||
return this.equals(WOOD) || this.equals(METAL) || this.equals(PLASTIC);
|
||||
}
|
||||
|
||||
public static Card parse(String input) {
|
||||
switch (input) {
|
||||
case "wood":
|
||||
@ -29,4 +33,26 @@ public enum Card {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,63 @@
|
||||
package edu.kit.informatik.model;
|
||||
|
||||
import edu.kit.informatik.ui.InvalidInputException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
public class CardGame {
|
||||
private Deque<Card> cardStack;
|
||||
private List<Card> resources = new ArrayList<>();
|
||||
private List<Item> buildings = new ArrayList<>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cardStack order: 1st element = 1st to take
|
||||
* @return
|
||||
*/
|
||||
public boolean start(Deque<Card> cardStack) {
|
||||
if (this.cardStack == null || this.cardStack.isEmpty()) {
|
||||
this.cardStack = cardStack;
|
||||
return true;
|
||||
} else {
|
||||
return false; // still in game
|
||||
}
|
||||
}
|
||||
|
||||
public Card draw() throws InvalidInputException {
|
||||
if (cardStack == null || cardStack.isEmpty()) {
|
||||
throw new InvalidInputException("no card to draw exists");
|
||||
}
|
||||
// TODO: awaiting dice roll
|
||||
Card card = cardStack.removeFirst();
|
||||
if (card.isResource()) {
|
||||
resources.add(card);
|
||||
}
|
||||
// TODO: make game await dice roll
|
||||
return card;
|
||||
}
|
||||
|
||||
public boolean build(Item item) throws InvalidInputException {
|
||||
Card[] resourcesNeeded = item.resourcesNeeded();
|
||||
// TODO
|
||||
buildings.add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Card> getResources() {
|
||||
// do not allow caller to modify internal list
|
||||
return new ArrayList<>(resources);
|
||||
}
|
||||
|
||||
public List<Item> getBuildings() {
|
||||
// do not allow caller to modify internal list
|
||||
return new ArrayList<>(buildings);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.cardStack = null;
|
||||
this.resources = new ArrayList<>();
|
||||
this.buildings = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
86
src/edu/kit/informatik/model/Item.java
Normal file
86
src/edu/kit/informatik/model/Item.java
Normal file
@ -0,0 +1,86 @@
|
||||
package edu.kit.informatik.model;
|
||||
|
||||
import static edu.kit.informatik.model.Card.METAL;
|
||||
import static edu.kit.informatik.model.Card.PLASTIC;
|
||||
import static edu.kit.informatik.model.Card.WOOD;
|
||||
|
||||
public enum Item {
|
||||
AXE,
|
||||
CLUB,
|
||||
SHACK,
|
||||
FIREPLACE,
|
||||
SAILING_RAFT,
|
||||
HANG_GLIDER,
|
||||
STEAMBOAT,
|
||||
BALLOON;
|
||||
|
||||
public Card[] resourcesNeeded() {
|
||||
switch (this) {
|
||||
case AXE:
|
||||
return new Card[] { METAL, METAL, METAL };
|
||||
case CLUB:
|
||||
return new Card[] { WOOD, WOOD, WOOD };
|
||||
case SHACK:
|
||||
return new Card[] { WOOD, WOOD, METAL, PLASTIC, PLASTIC };
|
||||
case FIREPLACE:
|
||||
return new Card[] { WOOD, WOOD, WOOD, METAL };
|
||||
case SAILING_RAFT:
|
||||
return new Card[] { WOOD, WOOD, WOOD, WOOD, METAL, METAL, PLASTIC, PLASTIC };
|
||||
case HANG_GLIDER:
|
||||
return new Card[] { WOOD, WOOD, METAL, METAL, PLASTIC, PLASTIC, PLASTIC, PLASTIC };
|
||||
case STEAMBOAT:
|
||||
return new Card[] { METAL, METAL, METAL, METAL, METAL, METAL, PLASTIC };
|
||||
case BALLOON:
|
||||
return new Card[] { WOOD, PLASTIC, PLASTIC, PLASTIC, PLASTIC, PLASTIC, PLASTIC };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Item parse(String input) {
|
||||
switch (input) {
|
||||
case "axe":
|
||||
return AXE;
|
||||
case "club":
|
||||
return CLUB;
|
||||
case "shack":
|
||||
return SHACK;
|
||||
case "fireplace":
|
||||
return FIREPLACE;
|
||||
case "sailingraft":
|
||||
return SAILING_RAFT;
|
||||
case "hangglider":
|
||||
return HANG_GLIDER;
|
||||
case "steamboat":
|
||||
return STEAMBOAT;
|
||||
case "ballon":
|
||||
return BALLOON;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
case AXE:
|
||||
return "axe";
|
||||
case CLUB:
|
||||
return "club";
|
||||
case SHACK:
|
||||
return "shack";
|
||||
case FIREPLACE:
|
||||
return "fireplace";
|
||||
case SAILING_RAFT:
|
||||
return "sailingraft";
|
||||
case HANG_GLIDER:
|
||||
return "hangglider";
|
||||
case STEAMBOAT:
|
||||
return "steamboat";
|
||||
case BALLOON:
|
||||
return "ballon"; // lol, a type in the specification!
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
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.ui.InvalidInputException;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
@ -11,11 +13,13 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILD;
|
||||
public class Build extends Command {
|
||||
private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)");
|
||||
|
||||
private String item;
|
||||
private Item item;
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) throws InvalidInputException {
|
||||
|
||||
if (game.build(item)) {
|
||||
Terminal.printLine("OK");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -27,6 +31,6 @@ public class Build extends Command {
|
||||
if (!matcher.matches()) {
|
||||
throw new InvalidInputException("invalid build command");
|
||||
}
|
||||
item = matcher.group(1);
|
||||
item = Item.parse(matcher.group(1));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,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.ui.InvalidInputException;
|
||||
|
||||
@ -8,7 +10,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.DRAW;
|
||||
public class Draw extends Command {
|
||||
@Override
|
||||
public void apply(CardGame game) throws InvalidInputException {
|
||||
|
||||
Card card = game.draw();
|
||||
Terminal.printLine(card);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,14 +1,23 @@
|
||||
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.ui.InvalidInputException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS;
|
||||
|
||||
public class ListBuildings extends Command {
|
||||
@Override
|
||||
public void apply(CardGame game) throws InvalidInputException {
|
||||
|
||||
List<Item> items = game.getBuildings();
|
||||
if (items.isEmpty()) {
|
||||
Terminal.printLine("EMPTY");
|
||||
} else {
|
||||
items.forEach(Terminal::printLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,14 +1,23 @@
|
||||
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.ui.InvalidInputException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES;
|
||||
|
||||
public class ListResources extends Command {
|
||||
@Override
|
||||
public void apply(CardGame game) {
|
||||
|
||||
public void apply(CardGame game) throws InvalidInputException {
|
||||
List<Card> resources = game.getResources();
|
||||
if (resources.isEmpty()) {
|
||||
Terminal.printLine("EMPTY");
|
||||
} else {
|
||||
resources.forEach(Terminal::printLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,6 @@
|
||||
package edu.kit.informatik.ui.command;
|
||||
|
||||
import edu.kit.informatik.Terminal;
|
||||
import edu.kit.informatik.model.CardGame;
|
||||
import edu.kit.informatik.ui.InvalidInputException;
|
||||
|
||||
@ -8,7 +9,8 @@ import static edu.kit.informatik.ui.command.CommandFactory.RESET;
|
||||
public class Reset extends Command {
|
||||
@Override
|
||||
public void apply(CardGame game) {
|
||||
|
||||
game.reset();
|
||||
Terminal.printLine("OK");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,10 +1,12 @@
|
||||
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.ui.InvalidInputException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -16,11 +18,15 @@ import static edu.kit.informatik.ui.command.CommandFactory.CARD;
|
||||
public class Start extends Command {
|
||||
private static final Pattern START_ARGUMENTS = Pattern.compile("start (?:(" + CARD + "),){63},(" + CARD + ")");
|
||||
|
||||
private List<Card> cards;
|
||||
private Deque<Card> cards;
|
||||
|
||||
@Override
|
||||
public void apply(CardGame game) {
|
||||
|
||||
public void apply(CardGame game) throws InvalidInputException {
|
||||
if (game.start(cards)) {
|
||||
Terminal.printLine("OK");
|
||||
} else {
|
||||
throw new InvalidInputException("could not start game");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -35,7 +41,7 @@ public class Start extends Command {
|
||||
cards = IntStream.rangeClosed(1, matcher.groupCount())
|
||||
.mapToObj(matcher::group)
|
||||
.map(Card::parse)
|
||||
.collect(Collectors.toList());
|
||||
.collect(Collectors.toCollection(ArrayDeque::new));
|
||||
if (cards.stream().anyMatch(Objects::isNull)) {
|
||||
cards = null;
|
||||
throw new InvalidInputException("invalid start argument value(s)");
|
||||
|
Loading…
Reference in New Issue
Block a user