mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-08 09:50:38 +00:00
Create interface for objects that require dice roll
This commit is contained in:
parent
9e75ca37a3
commit
33bf28485e
@ -1,5 +1,7 @@
|
|||||||
package edu.kit.informatik.cardgame.model;
|
package edu.kit.informatik.cardgame.model;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Game card.
|
* Game card.
|
||||||
*
|
*
|
||||||
@ -7,7 +9,7 @@ package edu.kit.informatik.cardgame.model;
|
|||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public enum Card {
|
public enum Card implements RequireDice {
|
||||||
/**
|
/**
|
||||||
* Wood. Basic resource.
|
* Wood. Basic resource.
|
||||||
*/
|
*/
|
||||||
@ -54,29 +56,31 @@ public enum Card {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int diceSizeNeeded() {
|
@Override
|
||||||
|
public Optional<Integer> diceSizeNeeded() {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case SPIDER:
|
case SPIDER:
|
||||||
return 4;
|
return Optional.of(4);
|
||||||
case SNAKE:
|
case SNAKE:
|
||||||
return 6;
|
return Optional.of(6);
|
||||||
case TIGER:
|
case TIGER:
|
||||||
return 8;
|
return Optional.of(8);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int minimumDiceRollNeeded() {
|
@Override
|
||||||
|
public Optional<Integer> minimumDiceRollNeeded() {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case SPIDER:
|
case SPIDER:
|
||||||
return 3;
|
return Optional.of(3);
|
||||||
case SNAKE:
|
case SNAKE:
|
||||||
return 4;
|
return Optional.of(4);
|
||||||
case TIGER:
|
case TIGER:
|
||||||
return 5;
|
return Optional.of(5);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,8 +33,7 @@ public class CardGame {
|
|||||||
* Items built by the player.
|
* Items built by the player.
|
||||||
*/
|
*/
|
||||||
private final List<Item> items = new ArrayList<>();
|
private final List<Item> items = new ArrayList<>();
|
||||||
private Integer awaitedDiceSize = null;
|
private RequireDice requireDice = null;
|
||||||
private Integer minimumDiceRoll = null;
|
|
||||||
/**
|
/**
|
||||||
* Current game phase.
|
* Current game phase.
|
||||||
*/
|
*/
|
||||||
@ -79,8 +78,7 @@ public class CardGame {
|
|||||||
resources.add(card);
|
resources.add(card);
|
||||||
break;
|
break;
|
||||||
case ANIMAL:
|
case ANIMAL:
|
||||||
awaitedDiceSize = card.diceSizeNeeded();
|
requireDice = card;
|
||||||
minimumDiceRoll = card.minimumDiceRollNeeded();
|
|
||||||
phase = Phase.ENCOUNTER;
|
phase = Phase.ENCOUNTER;
|
||||||
break;
|
break;
|
||||||
case CATASTROPHE:
|
case CATASTROPHE:
|
||||||
@ -103,16 +101,17 @@ public class CardGame {
|
|||||||
* @throws LogicException if not expecting dice roll or dice roll is invalid
|
* @throws LogicException if not expecting dice roll or dice roll is invalid
|
||||||
*/
|
*/
|
||||||
public String rollDice(int size, int roll) throws LogicException {
|
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");
|
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");
|
throw new LogicException("unexpected dice size");
|
||||||
} else if (roll > awaitedDiceSize || roll < 1) {
|
} else if (roll > sizeNeeded || roll < 1) {
|
||||||
throw new LogicException("impossible roll");
|
throw new LogicException("impossible roll");
|
||||||
}
|
}
|
||||||
awaitedDiceSize = null;
|
requireDice = null;
|
||||||
final int minimumNeeded = minimumDiceRoll;
|
|
||||||
minimumDiceRoll = null;
|
|
||||||
if (phase == Phase.ENCOUNTER) {
|
if (phase == Phase.ENCOUNTER) {
|
||||||
final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0);
|
final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0);
|
||||||
phase = Phase.SCAVENGE;
|
phase = Phase.SCAVENGE;
|
||||||
@ -170,10 +169,9 @@ public class CardGame {
|
|||||||
items.add(item);
|
items.add(item);
|
||||||
switch (item.category()) {
|
switch (item.category()) {
|
||||||
case ESCAPE:
|
case ESCAPE:
|
||||||
if (item.requiresDice()) {
|
if (item.diceSizeNeeded().isPresent()) {
|
||||||
// need at least a 4/d6
|
// need at least a 4/d6
|
||||||
awaitedDiceSize = item.diceSizeNeeded();
|
requireDice = item;
|
||||||
minimumDiceRoll = item.minimumDiceRollNeeded();
|
|
||||||
phase = Phase.ENDEAVOR;
|
phase = Phase.ENDEAVOR;
|
||||||
} else {
|
} else {
|
||||||
// player won
|
// player won
|
||||||
@ -321,8 +319,7 @@ public class CardGame {
|
|||||||
}
|
}
|
||||||
this.resources.clear();
|
this.resources.clear();
|
||||||
this.items.clear();
|
this.items.clear();
|
||||||
this.awaitedDiceSize = null;
|
this.requireDice = null;
|
||||||
this.minimumDiceRoll = null;
|
|
||||||
this.phase = Phase.SCAVENGE;
|
this.phase = Phase.SCAVENGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package edu.kit.informatik.cardgame.model;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
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.METAL;
|
||||||
import static edu.kit.informatik.cardgame.model.Card.PLASTIC;
|
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
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public enum Item {
|
public enum Item implements RequireDice {
|
||||||
/**
|
/**
|
||||||
* Axe. Provides an attack bonus of two.
|
* Axe. Provides an attack bonus of two.
|
||||||
*/
|
*/
|
||||||
@ -117,48 +118,25 @@ public enum Item {
|
|||||||
return this == Item.SHACK ? 5 : 0;
|
return this == Item.SHACK ? 5 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Check whether this item requires rolling a dice to take effect.
|
public Optional<Integer> diceSizeNeeded() {
|
||||||
*
|
|
||||||
* @return whether the player has to roll a dice after building this item
|
|
||||||
*/
|
|
||||||
public boolean requiresDice() {
|
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case HANG_GLIDER:
|
case HANG_GLIDER:
|
||||||
case SAILING_RAFT:
|
case SAILING_RAFT:
|
||||||
return true;
|
return Optional.of(6);
|
||||||
default:
|
default:
|
||||||
return false;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Get the size of the dice to roll after building this item.
|
public Optional<Integer> minimumDiceRollNeeded() {
|
||||||
*
|
|
||||||
* @return size of the dice to roll, or 0 if not necessary
|
|
||||||
*/
|
|
||||||
public int diceSizeNeeded() {
|
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case HANG_GLIDER:
|
case HANG_GLIDER:
|
||||||
case SAILING_RAFT:
|
case SAILING_RAFT:
|
||||||
return 6;
|
return Optional.of(4);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return Optional.empty();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/edu/kit/informatik/cardgame/model/RequireDice.java
Normal file
15
src/edu/kit/informatik/cardgame/model/RequireDice.java
Normal 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();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user