Question

In: Computer Science

Dice Game Rules: 2 - 4 players Each player has 5 Dice. The dice have 6...

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.

Solutions

Expert Solution

/*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
********************************************************

.....


Related Solutions

Two players A and B play a dice game with a 6-face fair dice. Player A...
Two players A and B play a dice game with a 6-face fair dice. Player A is only allowed to roll the dice once. Player B is allowed to roll the dice maximally twice (that is, Player B can decide whether or not she or he would roll the dice again after seeing the value of the first roll). If the player with the larger final value of the dice wins, what is the maximal probability for Player B to...
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to be the one that starts with the jar. Actions: - Each round at the same time both players deposit between 1 to 4 pennies into the jar. - Then both players are able to count the pennies in the jar. - If there are 21 or more pennies, the person with the jar is the winner. - If there are 20 or less pennies,...
This game is meant for two or more players. In the game, each player starts out...
This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generated by the dice is subtracted from the player’s points. The first player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the amount should be added to the player’s points. (As an...
Consider a game with two players, each of whom has two types. The types of player...
Consider a game with two players, each of whom has two types. The types of player 1 are T1 = (a,b). The types of player 2 are T2 = (c,d). Suppose the beliefs of the types are p1(c/a) = p2(a/c) = 0.25 and p1(c/b) = p2(a/d) = 0.75. Is there a common prior? If yes, construct one; if no, prove why not.
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
Each of 3 fair dice has six sides numbered 1, 2, 3, 4, 5, and 6....
Each of 3 fair dice has six sides numbered 1, 2, 3, 4, 5, and 6. If these 3 dice are all rolled at the same time, what is the probability that exactly 2 of these dice will show a 1 ? Answers in word version please
Consider the following game that has two players. Player A has three actions, and player B...
Consider the following game that has two players. Player A has three actions, and player B has three actions. Player A can either play Top, Middle or Bottom, whereas player B can play Left, Middle or Right. The payoffs are shown in the following matrix. Notice that a payoff to player A has been omitted (denoted by x). Player B    Left Middle Right Top (-1,1) (0,3) (1,10) Middle (2,0) (-2,-2) (-1,-1) Bottom (x,-1) (1,2) (3,2) (player A) Both players...
Two players (player A and player B) are playing a game against each other repeatedly until...
Two players (player A and player B) are playing a game against each other repeatedly until one is bankrupt. When a player wins a game they take $1 from the other player. All plays of the game are independent and identical. Suppose player A starts with $6 and player B starts with $6. If player A wins a game with probability 0.5, what is the probability the game ends (someone loses all their money) on exactly the 10th play of...
(10 marks) Two players (player A and player B) are playing a game against each other...
Two players (player A and player B) are playing a game against each other repeatedly until one is bankrupt. When a player wins a game they take $1 from the other player. All plays of the game are independent and identical. a) Suppose player A starts with $2 and player B starts with $1. If player A wins a game with probability p, what is the probability that player A wins all the money? b) Suppose player A starts with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT