Create interface for objects that require dice roll

This commit is contained in:
Arne Keller 2020-03-17 11:41:54 +01:00
parent 9e75ca37a3
commit 33bf28485e
4 changed files with 52 additions and 58 deletions

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.cardgame.model;
import java.util.Optional;
/**
* Game card.
*
@ -7,7 +9,7 @@ package edu.kit.informatik.cardgame.model;
* @author Arne Keller
* @version 1.0
*/
public enum Card {
public enum Card implements RequireDice {
/**
* Wood. Basic resource.
*/
@ -54,29 +56,31 @@ public enum Card {
}
}
public int diceSizeNeeded() {
@Override
public Optional<Integer> diceSizeNeeded() {
switch (this) {
case SPIDER:
return 4;
return Optional.of(4);
case SNAKE:
return 6;
return Optional.of(6);
case TIGER:
return 8;
return Optional.of(8);
default:
return 0;
return Optional.empty();
}
}
public int minimumDiceRollNeeded() {
@Override
public Optional<Integer> minimumDiceRollNeeded() {
switch (this) {
case SPIDER:
return 3;
return Optional.of(3);
case SNAKE:
return 4;
return Optional.of(4);
case TIGER:
return 5;
return Optional.of(5);
default:
return 0;
return Optional.empty();
}
}

View File

@ -33,8 +33,7 @@ public class CardGame {
* Items built by the player.
*/
private final List<Item> items = new ArrayList<>();
private Integer awaitedDiceSize = null;
private Integer minimumDiceRoll = null;
private RequireDice requireDice = null;
/**
* Current game phase.
*/
@ -79,8 +78,7 @@ public class CardGame {
resources.add(card);
break;
case ANIMAL:
awaitedDiceSize = card.diceSizeNeeded();
minimumDiceRoll = card.minimumDiceRollNeeded();
requireDice = card;
phase = Phase.ENCOUNTER;
break;
case CATASTROPHE:
@ -103,16 +101,17 @@ public class CardGame {
* @throws LogicException if not expecting dice roll or dice roll is invalid
*/
public String rollDice(int size, int roll) throws LogicException {
if (phase != Phase.ENDEAVOR && phase != Phase.ENCOUNTER) {
if (requireDice == null) {
throw new LogicException("not expecting dice roll");
} else if (awaitedDiceSize != size) {
}
int sizeNeeded = requireDice.diceSizeNeeded().get();
int minimumNeeded = requireDice.minimumDiceRollNeeded().get();
if (sizeNeeded != size) {
throw new LogicException("unexpected dice size");
} else if (roll > awaitedDiceSize || roll < 1) {
} else if (roll > sizeNeeded || roll < 1) {
throw new LogicException("impossible roll");
}
awaitedDiceSize = null;
final int minimumNeeded = minimumDiceRoll;
minimumDiceRoll = null;
requireDice = null;
if (phase == Phase.ENCOUNTER) {
final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0);
phase = Phase.SCAVENGE;
@ -170,10 +169,9 @@ public class CardGame {
items.add(item);
switch (item.category()) {
case ESCAPE:
if (item.requiresDice()) {
if (item.diceSizeNeeded().isPresent()) {
// need at least a 4/d6
awaitedDiceSize = item.diceSizeNeeded();
minimumDiceRoll = item.minimumDiceRollNeeded();
requireDice = item;
phase = Phase.ENDEAVOR;
} else {
// player won
@ -321,8 +319,7 @@ public class CardGame {
}
this.resources.clear();
this.items.clear();
this.awaitedDiceSize = null;
this.minimumDiceRoll = null;
this.requireDice = null;
this.phase = Phase.SCAVENGE;
}

View File

@ -2,6 +2,7 @@ package edu.kit.informatik.cardgame.model;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import static edu.kit.informatik.cardgame.model.Card.METAL;
import static edu.kit.informatik.cardgame.model.Card.PLASTIC;
@ -14,7 +15,7 @@ import static edu.kit.informatik.cardgame.model.Card.WOOD;
* @author Arne Keller
* @version 1.0
*/
public enum Item {
public enum Item implements RequireDice {
/**
* Axe. Provides an attack bonus of two.
*/
@ -117,48 +118,25 @@ public enum Item {
return this == Item.SHACK ? 5 : 0;
}
/**
* Check whether this item requires rolling a dice to take effect.
*
* @return whether the player has to roll a dice after building this item
*/
public boolean requiresDice() {
@Override
public Optional<Integer> diceSizeNeeded() {
switch (this) {
case HANG_GLIDER:
case SAILING_RAFT:
return true;
return Optional.of(6);
default:
return false;
return Optional.empty();
}
}
/**
* Get the size of the dice to roll after building this item.
*
* @return size of the dice to roll, or 0 if not necessary
*/
public int diceSizeNeeded() {
@Override
public Optional<Integer> minimumDiceRollNeeded() {
switch (this) {
case HANG_GLIDER:
case SAILING_RAFT:
return 6;
return Optional.of(4);
default:
return 0;
}
}
/**
* Get the minimum dice roll needed to activate this item if it needs a dice roll.
*
* @return minimum dice roll needed to use this item
*/
public int minimumDiceRollNeeded() {
switch (this) {
case HANG_GLIDER:
case SAILING_RAFT:
return 4;
default:
return 0;
return Optional.empty();
}
}

View File

@ -0,0 +1,15 @@
package edu.kit.informatik.cardgame.model;
import java.util.Optional;
public interface RequireDice {
Optional<Integer> diceSizeNeeded();
/**
* Get the minimum dice roll needed to activate this object if it needs a dice roll.
*
* @return minimum dice roll needed to use this item
*/
Optional<Integer> minimumDiceRollNeeded();
}