diff --git a/src/edu/kit/informatik/model/Card.java b/src/edu/kit/informatik/model/Card.java index 9a42964..fc27309 100644 --- a/src/edu/kit/informatik/model/Card.java +++ b/src/edu/kit/informatik/model/Card.java @@ -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; + } + } } diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index 5eed4d0..b7a248f 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -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 cardStack; + private List resources = new ArrayList<>(); + private List buildings = new ArrayList<>(); + + /** + * + * @param cardStack order: 1st element = 1st to take + * @return + */ + public boolean start(Deque 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 getResources() { + // do not allow caller to modify internal list + return new ArrayList<>(resources); + } + + public List 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<>(); + } } diff --git a/src/edu/kit/informatik/model/Item.java b/src/edu/kit/informatik/model/Item.java new file mode 100644 index 0000000..3b673d4 --- /dev/null +++ b/src/edu/kit/informatik/model/Item.java @@ -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; + } + } +} diff --git a/src/edu/kit/informatik/ui/command/Build.java b/src/edu/kit/informatik/ui/command/Build.java index 7d8cfbb..0b5cff3 100644 --- a/src/edu/kit/informatik/ui/command/Build.java +++ b/src/edu/kit/informatik/ui/command/Build.java @@ -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)); } } diff --git a/src/edu/kit/informatik/ui/command/Draw.java b/src/edu/kit/informatik/ui/command/Draw.java index a798e37..2f03521 100644 --- a/src/edu/kit/informatik/ui/command/Draw.java +++ b/src/edu/kit/informatik/ui/command/Draw.java @@ -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 diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java index 1b390fe..f8479c4 100644 --- a/src/edu/kit/informatik/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -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 items = game.getBuildings(); + if (items.isEmpty()) { + Terminal.printLine("EMPTY"); + } else { + items.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java index c9e9417..63e6743 100644 --- a/src/edu/kit/informatik/ui/command/ListResources.java +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -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 resources = game.getResources(); + if (resources.isEmpty()) { + Terminal.printLine("EMPTY"); + } else { + resources.forEach(Terminal::printLine); + } } @Override diff --git a/src/edu/kit/informatik/ui/command/Reset.java b/src/edu/kit/informatik/ui/command/Reset.java index 3053c57..9c25bb6 100644 --- a/src/edu/kit/informatik/ui/command/Reset.java +++ b/src/edu/kit/informatik/ui/command/Reset.java @@ -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 diff --git a/src/edu/kit/informatik/ui/command/Start.java b/src/edu/kit/informatik/ui/command/Start.java index 9ddc2c4..24c4d88 100644 --- a/src/edu/kit/informatik/ui/command/Start.java +++ b/src/edu/kit/informatik/ui/command/Start.java @@ -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 cards; + private Deque 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)");