In: Computer Science
Dice Game
Rules:
2 - 4 players
Each player has 5 Dice. The dice have 6 sides.
Each player rolls their dice, and the dice statistics are reported:
Sum, number of pairs (and of what), and "straights" - (all dice in order - e.g. 1,2,3,4,5 or 2,3,4,5,6)
Player 1 might roll 2,2,3,4,4 so the results would
be:
Sum: 15, 1 pair (2), 1 pair (4)
Player 2 might roll 1, 1, 4, 6, 6 so the results would be:
Sum: 18, 1 pair (1), 1 pair (6)
Player 3 might roll 3, 3, 3, 5, 6 so the results would be:
Sum: 20, 1 triple (3)
Player 4 might roll 1, 2, 3, 5, 6
Sum: 17
Only one player wins per turn. Points are awarded as follows (only the highest possible point, not a sum of possibles):
All 5 same (quint) |
8 |
straight |
7 |
4 same (quad) |
6 |
triple + pair |
5 |
triple |
4 |
two pairs |
3 |
one pair |
2 |
high score |
1 |
The higher pairs beat the lower pairs. (If no other winner, then player 2 beats player 1 because a pair of 6 beats a pair of 4).
Ties re-roll between themselves.
First player to 50 points wins.
If you have built your program properly, you should be able to change the number of players, the number of dice sides, the number of dice, and the win point condition (50 points to something higher) and no changes should be needed to any of the rest of your code.
PS: NetBeans/Java Pls.
/*Dear Student, I have spent a lot of time on it, so if you got something from it, then give it an Upvote. Thank you.*/
-----------------------------------------Die Class ---------------------------------------
import java.util.Random;
public class Die {
private int numSides;
Random rand = new Random();
public Die(int numSides) {
this.numSides = numSides;
}
public int roll(){
return rand.nextInt(this.numSides) + 1;
}
@Override
public String toString() {
return "Number of sides: " +this.numSides+ "\n"
+ "Roll: " + roll();
}
}
---------------------------------------Dice Class -----------------------------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Dice {
private Random rand = new Random();
private ArrayList<Die> die;
private int numDice;
private int maxDice;
private int numSides;
private int[] rolls;
public Dice(int MaxDice, int numSides) {
this.maxDice = MaxDice;
this.numSides = numSides;
int numDice = 0;
die = new ArrayList<>(MaxDice);
rolls = new int[maxDice];
for(int i = 0; i < this.maxDice; i++){
die.add(new Die(numSides));
this.numDice++;
}
}
public int[] roll(){
for(int i = 0; i < this.maxDice; i++){
rolls[i] = die.get(i).roll();
}
return rolls;
}
public int[] getRolls() {
return rolls;
}
public int sum(){
int sum = 0;
for(int i = 0; i < this.maxDice; i++){
sum += rolls[i];
}
return sum;
}
public int nSpecial(int num){
int count = 0;
for(int i = 0; i < this.maxDice; i++){
if(num == rolls[i]){
count++;
}
}
return count;
}
@Override
public String toString() {
String str = "Rolls: " + Arrays.toString(this.rolls);
return str;
}
}
public class TestDie {
public static void main(String[] args) {
Die d1 = new Die(12);
Die d2 = new Die(6);
System.out.println(d1.toString());
System.out.println(d2.toString());
}
}
public class TestDice {
public static void main(String[] args) {
Dice dice = new Dice(5, 6);
dice.roll();
System.out.println(dice.toString());
System.out.println("Sum: " +dice.sum());
System.out.println("Count of 5's: " +dice.nSpecial(5));
}
}
----------------------------------PairSixSideDice extends Dice-----------------------------
import java.util.Arrays;
public class PairSixSidedDice extends Dice {
public PairSixSidedDice(int numSides) {
super(2, numSides);
}
public boolean isSnakeEyes() {
if (getRolls()[0] == 1 && getRolls()[1] == 1) {
return true;
}
return false;
}
public boolean isDoubles() {
if (getRolls()[0] == getRolls()[1]) {
return true;
}
return false;
}
public boolean isSixes() {
if (getRolls()[0] == 6 && getRolls()[1] == 6) {
return true;
}
return false;
}
@Override
public String toString() {
return "Rolls: " + Arrays.toString(getRolls());
}
}
public class TestPairSixSidedDice {
public static void main(String[] args) {
PairSixSidedDice dice = new PairSixSidedDice(6);
dice.roll();
System.out.println(dice.toString());
System.out.println("Is Doubles: " + dice.isDoubles());
System.out.println("Is Snake Eyes: " + dice.isSnakeEyes());
System.out.println("Is Sixes: " + dice.isSixes());
}
}
------------------------------------Dice Game(Main)----------------------------------------
import java.util.Arrays;
import java.util.Scanner;
public class DiceGame {
private static int[] getCounts(int[] dice) {
int oneCount = 0;
int[] count = new int[6];
for (int i = 0; i < count.length; i++) {
count[i] = 0;
}
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 1) {
oneCount++;
}
}
count[0] = oneCount;
int twoCount = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 2) {
twoCount++;
}
}
count[1] = twoCount;
int threeCount = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 3) {
threeCount++;
}
}
count[2] = threeCount;
int fourCount = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 4) {
fourCount++;
}
}
count[3] = fourCount;
int fiveCount = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 5) {
fiveCount++;
}
}
count[4] = fiveCount;
int sixCount = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 6) {
sixCount++;
}
}
count[5] = sixCount;
return count;
}
private static String getResult(int[] dice) {
String hand = "";
int[] count = getCounts(dice);
int twoCounter = 0;
Arrays.sort(dice);
int index = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] == 2) {
twoCounter++;
}
if (twoCounter == 1) {
if (index == 0) {
hand = "one," + (i + 1);
index = 1;
}
}
if (twoCounter == 2) {
hand = "two," + (i + 1);
break;
}
}
int threeCounter = 0;
index = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] == 3) {
threeCounter++;
index = i + 1;
}
}
if (threeCounter == 1) {
hand = "three," + index;
}
if (threeCounter == 1 && twoCounter == 1) {
hand = "full,0";
}
int fourCounter = 0;
index = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] == 4) {
fourCounter++;
}
if (fourCounter == 1) {
hand = "four," + i;
}
}
int fiveCounter = 0;
index = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] == 5) {
fiveCounter++;
}
if (fiveCounter == 1) {
hand = "five," + i;
}
}
int prev = -1;
int flag = 0;
for (int i = 0; i < dice.length; i++) {
if (prev == -1 || (prev + 1) == dice[i]) {
prev = dice[i];
} else {
flag = 1;
}
}
if (flag == 0) {
hand = "straight,0";
}
int highest = 0;
if (hand.equals("")) {
for (int i = 0; i < dice.length; i++) {
if (dice[i] > highest) {
highest = dice[i];
}
}
hand = "highest," + highest;
}
return hand;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int numPlayers = 0;
int numSides = 0;
int numPoints = 0;
do {
System.out.print("Enter number of players (2 to 4): ");
numPlayers = Integer.parseInt(reader.nextLine());
if (numPlayers >= 2 && numPlayers <= 4) {
break;
} else {
System.out.println("Invalid number.");
}
} while (true);
System.out.print("Enter number of sides: ");
numSides = Integer.parseInt(reader.nextLine());
Dice[] playerDice = new Dice[numPlayers];
String[][] playerHand = new String[numPlayers][2];
int[] highHand = new int[numPlayers];
int[] playerScore = new int[numPlayers];
for (int i = 0; i < numPlayers; i++) {
playerDice[i] = new Dice(5, numSides);
}
do{
System.out.print("Ente number of points: ");
numPoints = Integer.parseInt(reader.nextLine());
if(numPoints>=50){
break;
}else{
System.out.println("Invalid number of points.");
}
}while(true);
while(true) {
System.out.println("======================================================");
for (int i = 0; i < numPlayers; i++) {
playerHand[i] = getResult(playerDice[i].roll()).split(",");
System.out.println("Player #" + (i + 1) + " rolls: " + playerDice[i].toString());
switch (playerHand[i][0]) {
case "one":
highHand[i] = 2;
System.out.println("One Pair!");
break;
case "two":
highHand[i] = 3;
System.out.println("Two Pairs!");
break;
case "three":
highHand[i] = 4;
System.out.println("Three of a Kind!");
break;
case "full":
highHand[i] = 5;
System.out.println("Full House!");
break;
case "four":
highHand[i] = 6;
System.out.println("Four of a Kind!");
break;
case "five":
highHand[i] = 8;
System.out.println("Quint!");
break;
case "straight":
highHand[i] = 9;
System.out.println("Straight!");
break;
case "highest":
highHand[i] = 1;
System.out.println("highest: " + playerHand[i][1]);
}
System.out.println("------------------------------------------------------");
}
int index = 0;
int max = highHand[0];
int higherDie = Integer.parseInt(playerHand[0][1]);
for (int i = 1; i < numPlayers; i++) {
if (max < highHand[i]) {
index = i;
max = highHand[i];
higherDie = Integer.parseInt(playerHand[i][1]);
} else if (max == highHand[i]) {
if (higherDie < Integer.parseInt(playerHand[i][1])) {
index = i;
max = highHand[i];
higherDie = Integer.parseInt(playerHand[i][1]);
}
}
}
playerScore[index] += highHand[index];
System.out.println("player #" + (index + 1) + " wins.");
System.out.println("======================================================");
System.out.println("Scores: ");
for (int i = 0; i < numPlayers; i++) {
System.out.println("Player #" + (i + 1) + ": " + playerScore[i]);
}
System.out.println("*******************************************************");
if(playerScore[index]>=numPoints){
break;
}
}
}
}
Output:
Number of sides: 12
Roll: 4
Number of sides: 6
Roll: 5
BUILD SUCCESSFUL (total time: 0
seconds)
run:
Number of Dice: 5
Rolls:
6
5
4
6
2
Sum: 23
Count of 5's: 1
BUILD SUCCESSFUL (total time: 0
seconds)
run:
Rolls: [5, 5]
Is Doubles: true
Is Snake Eyes: false
Is Sixes: false
BUILD SUCCESSFUL (total time: 0 seconds)
run:
Rolls: [6, 6]
Is Doubles: true
Is Snake Eyes: false
Is Sixes: true
BUILD SUCCESSFUL (total time: 0
seconds)
run:
Rolls: [1, 1]
Is Doubles: true
Is Snake Eyes: true
Is Sixes: false
BUILD SUCCESSFUL (total time: 0
seconds)
run:
Enter number of players (2 to 4): -1
Invalid number.
Enter number of players (2 to 4): 5
Invalid number.
Enter number of players (2 to 4): 4
Enter number of sides: 6
Ente number of points: 65
========================================================
Player #1 rolls: Rolls: [2, 3, 6, 6, 6]
Three of a Kind!
--------------------------------------------------------
Player #2 rolls: Rolls: [1, 1, 2, 4, 4]
Two Pairs!
--------------------------------------------------------
Player #3 rolls: Rolls: [1, 2, 4, 6, 6]
One Pair!
--------------------------------------------------------
Player #4 rolls: Rolls: [2, 2, 3, 4, 5]
One Pair!
--------------------------------------------------------
player #1 wins.
========================================================
Scores:
Player #1: 4
Player #2: 0
Player #3: 0
Player #4: 0
********************************************************
.....