improvements
This commit is contained in:
parent
808d8d496d
commit
5926655a54
@ -7,6 +7,8 @@ public class Game {
|
||||
|
||||
public static void main(String... args) {
|
||||
loop(
|
||||
new Player("Chiheb"),
|
||||
new Player("Artem"),
|
||||
new Player("Sam"),
|
||||
new Player("Daniel")
|
||||
);
|
||||
@ -18,35 +20,46 @@ public class Game {
|
||||
System.out.println("\n" +
|
||||
"===================================\n" +
|
||||
"Next player: " + player.getName());
|
||||
player.rollTheDices();
|
||||
System.out.println(player);
|
||||
|
||||
interactWith(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void interactWith(Player player) {
|
||||
System.out.println(player);
|
||||
if(player.misthrow()){
|
||||
player.finishRound();
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
System.out.println("" +
|
||||
"[r] roll the dices\n" +
|
||||
"[i] mark dice i\n" +
|
||||
"[f] finish round");
|
||||
String line = scanner.nextLine();
|
||||
switch (line) {
|
||||
case "r":
|
||||
player.rollTheDices();
|
||||
System.out.println(player);
|
||||
break;
|
||||
case "f":
|
||||
player.finishRound();
|
||||
System.out.println(player);
|
||||
return;
|
||||
default:
|
||||
int i = Integer.parseInt(line);
|
||||
player.toggleMark(i);
|
||||
System.out.println(player);
|
||||
break;
|
||||
try {
|
||||
System.out.println("" +
|
||||
"[r] roll the dices\n" +
|
||||
"[i] mark dice i\n" +
|
||||
"[f] finish round");
|
||||
String line = scanner.nextLine();
|
||||
switch (line) {
|
||||
case "r":
|
||||
player.rollTheDices();
|
||||
System.out.println(player);
|
||||
if(player.misthrow()){
|
||||
player.finishRound();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case "f":
|
||||
player.finishRound();
|
||||
System.out.println(player);
|
||||
return;
|
||||
default:
|
||||
int i = Integer.parseInt(line);
|
||||
player.toggleMark(i);
|
||||
System.out.println(player);
|
||||
break;
|
||||
}
|
||||
} catch (NumberFormatException | SelectionException | IllegalStateException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,27 +19,47 @@ public class Player implements DiceInteraction{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void rollTheDices() {
|
||||
// If all dices are fixed and score points,
|
||||
// (1) add selectionScore to roundScore,
|
||||
// (2) unFix all dices
|
||||
// (3) and roll the dices again
|
||||
public boolean misthrow(){
|
||||
// If there is no unFixed dice which scores points,
|
||||
// then this is a misthrow.
|
||||
return Arrays.stream(dices)
|
||||
.filter(d -> !d.isFixed())
|
||||
.noneMatch(d -> d.getDiceEyes().points() > 0);
|
||||
}
|
||||
|
||||
public void rollTheDices() throws IllegalStateException{
|
||||
if (misthrow()){
|
||||
throw new IllegalStateException("It is not allowed to roll the dices again after a misthrow");
|
||||
}
|
||||
// If no dices are marked
|
||||
boolean cond = Arrays.stream(dices)
|
||||
.filter(Dice::isFixed)
|
||||
.filter(d -> d.getDiceEyes() == ONE || d.getDiceEyes() == FIVE)
|
||||
.count() == 6;
|
||||
if (cond) {
|
||||
roundScore += selectionScore();
|
||||
Arrays.stream(dices).forEach(Dice::unFix);
|
||||
rollUnfixedDices();
|
||||
return;
|
||||
.noneMatch(Dice::isMarked);
|
||||
if(cond) {
|
||||
throw new IllegalStateException("It is not allowed to roll the dices again without marking at least one dice");
|
||||
}
|
||||
|
||||
cond = Arrays.stream(dices)
|
||||
.filter(Dice::isFixed)
|
||||
.filter(d -> d.getDiceEyes().points() > 0)
|
||||
.count() == 6;
|
||||
// If all dices are fixed and score points,
|
||||
if (cond) {
|
||||
// add selectionScore to roundScore,
|
||||
roundScore += selectionScore();
|
||||
// unFix all dices
|
||||
Arrays.stream(dices).forEach(Dice::unFix);
|
||||
} else {
|
||||
// Fix all currently marked dices.
|
||||
Arrays.stream(dices)
|
||||
.filter(Dice::isMarked)
|
||||
.forEach(Dice::fix);
|
||||
}
|
||||
|
||||
// Fix all currently marked dices.
|
||||
Arrays.stream(dices)
|
||||
.filter(Dice::isMarked)
|
||||
.forEach(Dice::fix);
|
||||
rollUnfixedDices();
|
||||
|
||||
if(misthrow()) {
|
||||
roundScore = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void rollUnfixedDices() {
|
||||
@ -48,13 +68,16 @@ public class Player implements DiceInteraction{
|
||||
.forEach(Dice::roll);
|
||||
}
|
||||
|
||||
public void toggleMark(int diceIdx) {
|
||||
public void toggleMark(int diceIdx) throws SelectionException {
|
||||
if (diceIdx < 0 || diceIdx >= dices.length) {
|
||||
throw new SelectionException("Invalid dice index: " + diceIdx);
|
||||
}
|
||||
if (dices[diceIdx].isFixed()) {
|
||||
throw new SelectionException("Dice " + diceIdx + " is already fixed");
|
||||
}
|
||||
if(dices[diceIdx].getDiceEyes().points() == 0){
|
||||
throw new SelectionException("Can't mark dice " + diceIdx + " which scores no points");
|
||||
}
|
||||
dices[diceIdx].toggleMarked();
|
||||
}
|
||||
|
||||
@ -62,14 +85,20 @@ public class Player implements DiceInteraction{
|
||||
totalScore += roundScore + selectionScore();
|
||||
roundScore = 0;
|
||||
|
||||
// Unfix and unmark all dices
|
||||
// UnFix and unMark all dices
|
||||
for (Dice dice : dices) {
|
||||
dice.unMark();
|
||||
dice.unFix();
|
||||
}
|
||||
|
||||
rollUnfixedDices();
|
||||
}
|
||||
|
||||
private int selectionScore() {
|
||||
if (misthrow()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ones = 0;
|
||||
int fives = 0;
|
||||
for (Dice dice : dices) {
|
||||
@ -108,6 +137,17 @@ public class Player implements DiceInteraction{
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s2 = toStringHelper();
|
||||
int tail = 6 * 3 + 5;
|
||||
int head = s2.length() - tail;
|
||||
|
||||
String s1 = " ".repeat(head);
|
||||
s1 += " 0 1 2 3 4 5 ";
|
||||
|
||||
return s1 + "\n" + s2;
|
||||
}
|
||||
|
||||
String toStringHelper() {
|
||||
return totalScore + " + " + (roundScore + selectionScore()) + ": " + dicesToString();
|
||||
}
|
||||
|
||||
|
@ -17,15 +17,15 @@ class PlayerTest {
|
||||
|
||||
player = new Player("Player1");
|
||||
expected = "0 + 0: 4 5 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
player = new Player("Player2");
|
||||
expected = "0 + 0: 1 2 5 4 2 3 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
player = new Player("Player3");
|
||||
expected = "0 + 0: 1 2 6 1 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -39,11 +39,11 @@ class PlayerTest {
|
||||
|
||||
player.toggleMark(diceIdx);
|
||||
expected = "0 + 50: 4 (5) 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
player.rollTheDices();
|
||||
expected = "0 + 50: 1 [5] 2 5 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> player.toggleMark(diceIdx));
|
||||
}
|
||||
@ -62,12 +62,12 @@ class PlayerTest {
|
||||
// Player marks dice 1 as fixed
|
||||
player.changeDiceState(diceIdx);
|
||||
expected = "0 + 50: 4 (5) 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
// Then rolls all other dices again.
|
||||
player.rollTheDices();
|
||||
expected = "0 + 50: 1 [5] 2 5 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
assertEquals(expected, player.toStringHelper());
|
||||
|
||||
// When they now try to unmark dice 1,
|
||||
// an exception will be thrown.
|
||||
|
Loading…
x
Reference in New Issue
Block a user