tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
42
Task/Pig-the-dice-game/Java/pig-the-dice-game.java
Normal file
42
Task/Pig-the-dice-game/Java/pig-the-dice-game.java
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import java.util.*;
|
||||
|
||||
public class PigDice {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int maxScore = 100;
|
||||
final int playerCount = 2;
|
||||
final String[] yesses = {"y", "Y", ""};
|
||||
|
||||
int[] safeScore = new int[2];
|
||||
int player = 0, score = 0;
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
Random rnd = new Random();
|
||||
|
||||
while (true) {
|
||||
System.out.printf(" Player %d: (%d, %d) Rolling? (y/n) ", player,
|
||||
safeScore[player], score);
|
||||
if (safeScore[player] + score < maxScore
|
||||
&& Arrays.asList(yesses).contains(sc.nextLine())) {
|
||||
final int rolled = rnd.nextInt(6) + 1;
|
||||
System.out.printf(" Rolled %d\n", rolled);
|
||||
if (rolled == 1) {
|
||||
System.out.printf(" Bust! You lose %d but keep %d\n\n",
|
||||
score, safeScore[player]);
|
||||
} else {
|
||||
score += rolled;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
safeScore[player] += score;
|
||||
if (safeScore[player] >= maxScore)
|
||||
break;
|
||||
System.out.printf(" Sticking with %d\n\n", safeScore[player]);
|
||||
}
|
||||
score = 0;
|
||||
player = (player + 1) % playerCount;
|
||||
}
|
||||
System.out.printf("\n\nPlayer %d wins with a score of %d",
|
||||
player, safeScore[player]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue