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