Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
42
Task/Pig-the-dice-game/Java/pig-the-dice-game-1.java
Normal file
42
Task/Pig-the-dice-game/Java/pig-the-dice-game-1.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]);
|
||||
}
|
||||
}
|
||||
67
Task/Pig-the-dice-game/Java/pig-the-dice-game-2.java
Normal file
67
Task/Pig-the-dice-game/Java/pig-the-dice-game-2.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public interface PigDice {
|
||||
public static void main(String... arguments) {
|
||||
final int maxScore = 100;
|
||||
final int playerCount = 2;
|
||||
final String[] yesses = {"y", "Y", ""};
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
final Random random = new Random();
|
||||
|
||||
final int[] safeScore = new int[2];
|
||||
final int[] score = new int[2];
|
||||
|
||||
IntStream.iterate(0, player -> (player + 1) % playerCount)
|
||||
.map(player -> {
|
||||
boolean isRolling = true;
|
||||
while (isRolling) {
|
||||
System.out.printf(
|
||||
"Player %d: (%d, %d) Rolling? (y/n) ",
|
||||
player,
|
||||
safeScore[player],
|
||||
score[player]
|
||||
);
|
||||
isRolling =
|
||||
safeScore[player] + score[player] < maxScore
|
||||
&& Arrays.asList(yesses).contains(scanner.nextLine())
|
||||
;
|
||||
if (isRolling) {
|
||||
final int rolled = random.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[player],
|
||||
safeScore[player]
|
||||
);
|
||||
return -1;
|
||||
} else {
|
||||
score[player] += rolled;
|
||||
}
|
||||
} else {
|
||||
safeScore[player] += score[player];
|
||||
if (safeScore[player] >= maxScore) {
|
||||
return player;
|
||||
}
|
||||
System.out.printf("Sticking with %d\n\n", safeScore[player]);
|
||||
}
|
||||
}
|
||||
score[player] = 0;
|
||||
return -1;
|
||||
})
|
||||
.filter(player -> player > -1)
|
||||
.findFirst()
|
||||
.ifPresent(player ->
|
||||
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