mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-09 02:10:39 +00:00
Checkstyle
This commit is contained in:
parent
7e499c0c9a
commit
19712a7039
@ -90,7 +90,7 @@ public enum Card implements RequireDice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of cards of this type needed in a stack.
|
* Get the number of cards of this type needed to {@link CardGame#start start} a game.
|
||||||
*
|
*
|
||||||
* @return amount of cards needed in a card deck
|
* @return amount of cards needed in a card deck
|
||||||
*/
|
*/
|
||||||
|
@ -1,23 +1,32 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Card category. Game cards are categorized into three categories depending on their properties.
|
* Card category. {@link Card Game cards} are categorized into three categories depending on their properties.
|
||||||
*
|
*
|
||||||
* @see Card
|
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public enum CardCategory {
|
public enum CardCategory {
|
||||||
/**
|
/**
|
||||||
* Basic resource card, harmless.
|
* Basic resource card, harmless.
|
||||||
|
*
|
||||||
|
* @see Card#WOOD
|
||||||
|
* @see Card#METAL
|
||||||
|
* @see Card#PLASTIC
|
||||||
*/
|
*/
|
||||||
RESOURCE,
|
RESOURCE,
|
||||||
/**
|
/**
|
||||||
* Animal card, can attack player.
|
* Animal card, can attack player.
|
||||||
|
*
|
||||||
|
* @see Card#SNAKE
|
||||||
|
* @see Card#SPIDER
|
||||||
|
* @see Card#TIGER
|
||||||
*/
|
*/
|
||||||
ANIMAL,
|
ANIMAL,
|
||||||
/**
|
/**
|
||||||
* Catastrophic card, can not harm player.
|
* Catastrophic card, can not harm player.
|
||||||
|
*
|
||||||
|
* @see Card#THUNDERSTORM
|
||||||
*/
|
*/
|
||||||
CATASTROPHE
|
CATASTROPHE
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -8,7 +9,15 @@ import java.util.Set;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Card game.
|
* Card game. Features:
|
||||||
|
* <ul>
|
||||||
|
* <li>100% deterministic</li>
|
||||||
|
* <li>64! different card stacks</li>
|
||||||
|
* <li>8 buildable items</li>
|
||||||
|
* <li>7 exciting card types</li>
|
||||||
|
* <li>3 dice needed</li>
|
||||||
|
* <li>0 skill required</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@ -27,7 +36,7 @@ public class CardGame {
|
|||||||
*/
|
*/
|
||||||
private Phase phase;
|
private Phase phase;
|
||||||
/**
|
/**
|
||||||
* Object currently requiring dice roll.
|
* Object currently requiring dice roll, null if not awaiting dice roll.
|
||||||
*/
|
*/
|
||||||
private RequireDice requireDice;
|
private RequireDice requireDice;
|
||||||
|
|
||||||
@ -61,16 +70,16 @@ public class CardGame {
|
|||||||
*/
|
*/
|
||||||
public Card draw() throws LogicException {
|
public Card draw() throws LogicException {
|
||||||
if (cardStack == null) {
|
if (cardStack == null) {
|
||||||
throw new LogicException("no card to draw exists");
|
throw new LogicException("no card stack to draw from exists");
|
||||||
} else if (phase != Phase.SCAVENGE) {
|
} else if (phase != Phase.SCAVENGE) {
|
||||||
throw new LogicException("can only draw cards in scavenge phase");
|
throw new LogicException("can only draw cards in scavenge phase");
|
||||||
}
|
}
|
||||||
final Card card = cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
|
final Card card = cardStack.draw().orElseThrow(() -> new LogicException("no card to draw exists"));
|
||||||
switch (card.category()) {
|
switch (card.category()) {
|
||||||
case RESOURCE:
|
case RESOURCE: // simply add resource cards to the player's inventory
|
||||||
inventory.addResource(card);
|
inventory.addResource(card);
|
||||||
break;
|
break;
|
||||||
case ANIMAL:
|
case ANIMAL: // animal cards trigger an encounter
|
||||||
requireDice = card;
|
requireDice = card;
|
||||||
phase = Phase.ENCOUNTER;
|
phase = Phase.ENCOUNTER;
|
||||||
break;
|
break;
|
||||||
@ -97,8 +106,8 @@ public class CardGame {
|
|||||||
if (requireDice == null) {
|
if (requireDice == null) {
|
||||||
throw new LogicException("not expecting dice roll");
|
throw new LogicException("not expecting dice roll");
|
||||||
}
|
}
|
||||||
int sizeNeeded = requireDice.diceSizeNeeded().get();
|
final int sizeNeeded = requireDice.diceSizeNeeded().get();
|
||||||
int minimumNeeded = requireDice.minimumDiceRollNeeded().get();
|
final int minimumNeeded = requireDice.minimumDiceRollNeeded().get();
|
||||||
if (sizeNeeded != size) {
|
if (sizeNeeded != size) {
|
||||||
throw new LogicException("unexpected dice size");
|
throw new LogicException("unexpected dice size");
|
||||||
} else if (roll > sizeNeeded || roll < 1) {
|
} else if (roll > sizeNeeded || roll < 1) {
|
||||||
@ -106,6 +115,7 @@ public class CardGame {
|
|||||||
}
|
}
|
||||||
requireDice = null;
|
requireDice = null;
|
||||||
if (phase == Phase.ENCOUNTER) {
|
if (phase == Phase.ENCOUNTER) {
|
||||||
|
// calculate fighting bonus, selecting the most powerful item the player owns
|
||||||
final int bonus = inventory.itemStream().mapToInt(Item::fightingBonus).max().orElse(0);
|
final int bonus = inventory.itemStream().mapToInt(Item::fightingBonus).max().orElse(0);
|
||||||
phase = Phase.SCAVENGE;
|
phase = Phase.SCAVENGE;
|
||||||
checkLost();
|
checkLost();
|
||||||
@ -138,15 +148,14 @@ public class CardGame {
|
|||||||
*/
|
*/
|
||||||
public String build(Item item) throws LogicException {
|
public String build(Item item) throws LogicException {
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
throw new LogicException("can not build item");
|
throw new LogicException("can not build null item");
|
||||||
} else if (phase != Phase.SCAVENGE) {
|
} else if (phase != Phase.SCAVENGE) {
|
||||||
throw new LogicException("can only build in scavenge phase");
|
throw new LogicException("can only build in scavenge phase");
|
||||||
} else if (inventory.contains(item)) {
|
} else if (inventory.contains(item)) {
|
||||||
throw new LogicException("already built");
|
throw new LogicException("already built");
|
||||||
} else if (inventory.canBuild(item)) {
|
} else if (inventory.canBuild(item)) {
|
||||||
inventory.build(item);
|
inventory.build(item);
|
||||||
switch (item.category()) {
|
if (item.category() == ItemCategory.ESCAPE) {
|
||||||
case ESCAPE:
|
|
||||||
if (item.diceSizeNeeded().isPresent()) {
|
if (item.diceSizeNeeded().isPresent()) {
|
||||||
// player needs to roll dice to escape
|
// player needs to roll dice to escape
|
||||||
requireDice = item;
|
requireDice = item;
|
||||||
@ -157,12 +166,8 @@ public class CardGame {
|
|||||||
phase = Phase.WON;
|
phase = Phase.WON;
|
||||||
return "win";
|
return "win";
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
default:
|
|
||||||
checkLost();
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
checkLost();
|
||||||
return "OK";
|
return "OK";
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
@ -216,7 +221,7 @@ public class CardGame {
|
|||||||
* @return resources available
|
* @return resources available
|
||||||
* @throws LogicException if no game is active
|
* @throws LogicException if no game is active
|
||||||
*/
|
*/
|
||||||
public Deque<Card> getResources() throws LogicException {
|
public Collection<Card> getResources() throws LogicException {
|
||||||
if (!gameStarted()) {
|
if (!gameStarted()) {
|
||||||
throw new LogicException("can not get resources: game not started");
|
throw new LogicException("can not get resources: game not started");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,12 @@ import java.util.ArrayDeque;
|
|||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple card stack that remembers its initial state and can be {@link #reset} on demand.
|
||||||
|
*
|
||||||
|
* @author Arne Keller
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
public class CardStack {
|
public class CardStack {
|
||||||
/**
|
/**
|
||||||
* Copy of the card stack used to initialize the stack.
|
* Copy of the card stack used to initialize the stack.
|
||||||
@ -14,24 +20,44 @@ public class CardStack {
|
|||||||
*/
|
*/
|
||||||
private Deque<Card> activeStack;
|
private Deque<Card> activeStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new card stack with the specified card collection.
|
||||||
|
* The first element will be {@link #draw drawn} first.
|
||||||
|
*
|
||||||
|
* @param cards card stack
|
||||||
|
*/
|
||||||
public CardStack(Deque<Card> cards) {
|
public CardStack(Deque<Card> cards) {
|
||||||
// copy stack to avoid modifications
|
// copy stack to avoid modifications
|
||||||
this.stack = new ArrayDeque<>(cards);
|
this.stack = new ArrayDeque<>(cards);
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the next card.
|
||||||
|
*
|
||||||
|
* @return the card (empty if stack is empty)
|
||||||
|
*/
|
||||||
public Optional<Card> draw() {
|
public Optional<Card> draw() {
|
||||||
return Optional.ofNullable(activeStack.pollFirst());
|
return Optional.ofNullable(activeStack.pollFirst());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the currently active stack.
|
||||||
|
*/
|
||||||
public void clearActiveStack() {
|
public void clearActiveStack() {
|
||||||
activeStack.clear();
|
activeStack.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the currently active stack.
|
||||||
|
*/
|
||||||
public void reset() {
|
public void reset() {
|
||||||
activeStack = new ArrayDeque<>(stack);
|
activeStack = new ArrayDeque<>(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return whether the active card stack is empty
|
||||||
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return activeStack.isEmpty();
|
return activeStack.isEmpty();
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ public class Inventory {
|
|||||||
resources.add(resourceCard);
|
resources.add(resourceCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return resources owned by the player
|
||||||
|
*/
|
||||||
public Deque<Card> getResources() {
|
public Deque<Card> getResources() {
|
||||||
// have to copy here: caller does not expect an auto-updating collection
|
// have to copy here: caller does not expect an auto-updating collection
|
||||||
return new ArrayDeque<>(resources);
|
return new ArrayDeque<>(resources);
|
||||||
|
@ -9,27 +9,26 @@ import static edu.kit.informatik.cardgame.model.Card.PLASTIC;
|
|||||||
import static edu.kit.informatik.cardgame.model.Card.WOOD;
|
import static edu.kit.informatik.cardgame.model.Card.WOOD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Game item.
|
* Game item that can be built by the player. Categorized into several {@link ItemCategory categories}.
|
||||||
*
|
*
|
||||||
* @see ItemCategory
|
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public enum Item implements RequireDice {
|
public enum Item implements RequireDice {
|
||||||
/**
|
/**
|
||||||
* Axe. Provides an attack bonus of two.
|
* Axe. Provides an {@link #fightingBonus attack bonus} of two.
|
||||||
*/
|
*/
|
||||||
AXE,
|
AXE,
|
||||||
/**
|
/**
|
||||||
* Club. Provides an attack bonus of one.
|
* Club. Provides an {@link #fightingBonus attack bonus} of one.
|
||||||
*/
|
*/
|
||||||
CLUB,
|
CLUB,
|
||||||
/**
|
/**
|
||||||
* Shack. Can save the last five items in case of animal attacks or catastrophic events.
|
* Shack. Can {@link #itemsSecured save} the last five items in case of animal attacks or catastrophic events.
|
||||||
*/
|
*/
|
||||||
SHACK,
|
SHACK,
|
||||||
/**
|
/**
|
||||||
* Fireplace. Required for some advanced items.
|
* Fireplace. {@link #itemsNeededToBuild Required} for some advanced items.
|
||||||
*/
|
*/
|
||||||
FIREPLACE,
|
FIREPLACE,
|
||||||
/**
|
/**
|
||||||
@ -51,7 +50,7 @@ public enum Item implements RequireDice {
|
|||||||
BALLON;
|
BALLON;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resources needed to build this item, in no particular order.
|
* Get the resources needed to build this item, in no particular order.
|
||||||
*
|
*
|
||||||
* @return resources needed to build this item
|
* @return resources needed to build this item
|
||||||
*/
|
*/
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Item category. Game items are categorized into two categories: normal items and items used to escape.
|
* Item category. {@link Item Game items} are categorized into two categories: normal items and items used to escape.
|
||||||
*
|
*
|
||||||
* @see Item
|
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public enum ItemCategory {
|
public enum ItemCategory {
|
||||||
/**
|
/**
|
||||||
* Normal item, can not be used to escape.
|
* Normal item, can not be used to escape.
|
||||||
|
*
|
||||||
|
* @see Item#AXE
|
||||||
|
* @see Item#CLUB
|
||||||
|
* @see Item#SHACK
|
||||||
|
* @see Item#FIREPLACE
|
||||||
*/
|
*/
|
||||||
DEFAULT,
|
DEFAULT,
|
||||||
/**
|
/**
|
||||||
* Item that can be used to escape.
|
* Item that can be used to escape.
|
||||||
|
*
|
||||||
|
* @see Item#SAILING_RAFT
|
||||||
|
* @see Item#HANG_GLIDER
|
||||||
|
* @see Item#STEAMBOAT
|
||||||
|
* @see Item#BALLON
|
||||||
*/
|
*/
|
||||||
ESCAPE;
|
ESCAPE;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown on semantically invalid user input.
|
* Thrown on semantically invalid user input, usually in the {@link CardGame game}.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
|
@ -16,7 +16,6 @@ public interface RequireDice {
|
|||||||
*/
|
*/
|
||||||
Optional<Integer> diceSizeNeeded();
|
Optional<Integer> diceSizeNeeded();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the minimum dice roll needed to activate this object.
|
* Get the minimum dice roll needed to activate this object.
|
||||||
*
|
*
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package edu.kit.informatik.cardgame.ui;
|
package edu.kit.informatik.cardgame.ui;
|
||||||
|
|
||||||
|
import edu.kit.informatik.cardgame.ui.command.Command;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown on syntactically invalid user input.
|
* Thrown on syntactically invalid user input, usually in the {@link Command#parse command parsers}.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
|
@ -22,7 +22,7 @@ public final class ListBuildings extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws LogicException {
|
public void apply(CardGame game) throws LogicException {
|
||||||
List<Item> items = game.getItems();
|
final List<Item> items = game.getItems();
|
||||||
if (items.isEmpty()) {
|
if (items.isEmpty()) {
|
||||||
Terminal.printLine("EMPTY");
|
Terminal.printLine("EMPTY");
|
||||||
} else {
|
} else {
|
||||||
|
@ -6,7 +6,7 @@ import edu.kit.informatik.cardgame.model.CardGame;
|
|||||||
import edu.kit.informatik.cardgame.model.LogicException;
|
import edu.kit.informatik.cardgame.model.LogicException;
|
||||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||||
|
|
||||||
import java.util.Deque;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* list-resources command. Prints all of the {@link Card resources} collected by the player,
|
* list-resources command. Prints all of the {@link Card resources} collected by the player,
|
||||||
@ -23,7 +23,7 @@ public final class ListResources extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(CardGame game) throws LogicException {
|
public void apply(CardGame game) throws LogicException {
|
||||||
final Deque<Card> resources = game.getResources();
|
final Collection<Card> resources = game.getResources();
|
||||||
if (resources.isEmpty()) {
|
if (resources.isEmpty()) {
|
||||||
Terminal.printLine("EMPTY");
|
Terminal.printLine("EMPTY");
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user