Question

In: Computer Science

Many games involve the use of dice. The singular form of dice is die. A standard...

Many games involve the use of dice. The singular form of dice is die. A standard die is a cube, i.e. a polyhedron with six sides. Each side of a die has a number of dots (or pips) on it; each side of a die is unique and contains a number of pips from 1 through 6. A die is rolled or tossed such that it comes to rest with one side randomly facing up. The die is said to have rolled a number equal to the number of pips on the side facing up.

Armed with this knowledge:

Part 1 (20%)

Implement a Java class called Die with the following features:

  • an instance variable that can store a whole number value from 1 through 6.
  • a constructor that assigns a random valid value to a die object.
  • a method that will roll a die, assigning it a new valid value.
  • a method that returns a die's value.

Part 2 (30%)

Implement a dice game called Guess wherein the player chooses a number between 2 and 12 - this is the range of possible values that can be achieved by summing the result of rolling a pair of dice. This dice game will be in its own class called Guess; it will not be in the Die class.

The game then creates two die objects and rolls them together up to three times. If the player's number comes up when summing the result of rolling the pair of dice, the player wins and the game ends. If, after three rolls of the pair of dice, the player's number has not come up, the computer wins and the game ends.

Create output charting the progress of the game and the result.

Part 3 (50%)

Implement a dice game called Matador. The Matador game will be in its own class called Matador; it will not be in the Die class or in the Guess class.

Here are the rules of Matador:

  • The game is played between the player and the computer.
  • The player's score and the computer's score each begin at 0.
  • The object of the game is to be the first reach 121 points.
  • The player and the computer take turns. On each turn:
    1. The player / computer chooses a number from 2 through 6
      • prompt the player to enter their choice on the keyboard
      • the computer should choose randomly
    2. The player / computer rolls two dice. Determine the results for the player / computer (depending on who is rolling):
    • if a 1 appears on both dice, their score is reset to 0 and it becomes the other's turn.
    • if a 1 appears on only one die, nothing is added to their score and it becomes the other's turn.
    • if a 1 does not appear on either die, the sum of the two dice is added to the roller's score.
      • if their chosen number appears on only one die, they roll again.
      • if their chosen number appears on both dice, they win.
      • if their chosen number did not appear on either die, it becomes the other's turn.

The first to reach a total score of 121 wins. Create output charting the progress of the game and the result.

Please submit:

  1. your three Java classes (source code / text files / .java extension)
  2. screenshots showing your programs in action (image files)

Solutions

Expert Solution

ANSWER part 1 and 2

SOURCE CODE

    import java.util.Random;

    import java.util.Scanner;

    public class Die{

        private int value;

        

        // DEFINING THE CONSTRUCTOR

        public Die(){

            Random rand = new Random();

            value = rand.nextInt(6) + 1;

        }

        // thsi function assigns a radnom value to a dice

        // between 1 and 6

        public void roll(){

            Random rand = new Random();

            value = rand.nextInt(6) + 1;

        }

        // returns the value of the dice

        public int getValue(){

            return value;

        }

    }

    // Guess class

    class Guess{

        // decalare 2 die

        private Die dice1 = new Die();

        private Die dice2 = new Die();

        // it has only one method

        // this method controls the entire game

        public void playGame(){

            // decalring a new scanner

            Scanner sc = new Scanner(System.in);

            // taking user's guess as input

            System.out.print("Please Enter your guess(2-12): ");

            int guess = sc.nextInt();

            sc.close();

            int diceRoll = 0, attempts = 0, flag = 0;

            // loop to see who won

            while(attempts < 3){

                // roll the die

                dice1.roll();

                dice2.roll();

                // get the net value

                diceRoll = dice1.getValue() + dice2.getValue();

                // print the net value

                System.out.println("The result of rolling the die is " + diceRoll + "...");

                // see if user has won

                if(guess == diceRoll){

                    flag = 1;

                    break;

                }

                attempts++;

            }

            // if user has won

            if(flag == 1)

                System.out.println("Player has won!");

            // if user has lost

            else

                System.out.println("Computer has won!");

        }

    }

    // main class to test the above classes

    class main_class{

        public static void main(String[] args) {

            Guess g = new Guess();

            g.playGame();

        }

    }

OUTPUT

PART 3

SOURCE CODE

import java.util.Random;

import java.util.Scanner;

public class Die {

    private int value;

    // DEFINING THE CONSTRUCTOR

    public Die() {

        Random rand = new Random();

        value = rand.nextInt(6) + 1;

    }

    // thsi function assigns a radnom value to a dice

    // between 1 and 6

    public void roll() {

        Random rand = new Random();

        value = rand.nextInt(6) + 1;

    }

    // returns the value of the dice

    public int getValue() {

        return value;

    }

}

// The Matador class

class Matador{

    // private data members

    private Die dice1 = new Die();

    private Die dice2 = new Die();

    private int playerScore;

    private int computerScore;

    private int playerGuess;

    private int computerGuess;

    private boolean playersTurn;

    // constructor

    // sets the scores to 0

    public Matador(){

        playerScore = 0;

        computerScore = 0;

    }

    // function to show the current score to screen

    public void showScore(){

        System.out.println("Player Score = " + playerScore + "\t\tComputer Score = " + computerScore);

    }

    

    // function to roll the die

    private void rollDie(){

        dice1.roll();

        dice2.roll();

    }

    // function to set the score according to given conditions

    private void setScore(){

        int d1 = dice1.getValue(), d2 = dice2.getValue();

        int rollValue = d1+d2;

        

        // if one 1 has been generated

        if((d1 == 1 && d2 != 1) || (d1 != 1 && d2 == 1)){

            if(playersTurn){

                playerScore += rollValue;

                playersTurn = false;

            }

            else{

                computerScore += rollValue;

                playersTurn = true;

            }

        }

        // if both die yield 1

        else if(d1 == 1 && d2 == 1){

            if(playersTurn){

                playersTurn = false;

                playerScore = 0;

            }

            else{

                playersTurn = true;

                computerScore = 0;

            }

        }

        // if none of the die yield one

        else{

            // if the die and guess values are same for player

            if(playersTurn && d1 == playerGuess && d2 == playerGuess)

                playerScore = 121;

            // if the die and guess values are not same for player

            else if(playersTurn)

            playerScore += rollValue;

            // if the die and guess values are same for computer

            else if(!playersTurn && d1 == computerGuess && d2 == computerGuess)

            computerScore = 121;

            // if the die and guess values are not same for computer

            else

                computerScore += rollValue;

        }

    }

    // method to play the game

    public void playGame(){

        // the turn is set to the player

        playersTurn = true;

        Scanner sc = new Scanner(System.in);

        // loop to see who wins

        while(playerScore != 121 && computerScore != 121){

            if(playersTurn){

                System.out.println("Player's turn...");

                System.out.print("Enter your guess(2-6): ");

                

                // enter players guess

                playerGuess = sc.nextInt();

                System.out.println("Player has guessed " + playerGuess);

                // roll the die and set the score

                rollDie();

                setScore();

                

                // show what values have been yield by die

                System.out.println("Dice 1 = " + dice1.getValue() + "\t\tDice 2 = " + dice2.getValue());

            }

            else{

                // set the guess of computer

                Random rand = new Random();

                System.out.println("Computer's turn...");

                computerGuess = rand.nextInt(5) + 2;

                System.out.println("Computer has guessed " + computerGuess);

                

                // roll the die and set the score

                rollDie();

                setScore();

                

                // show what values have been yield by die

                System.out.println("Dice 1 = " + dice1.getValue() + "\t\tDice 2 = " + dice2.getValue());

            }

            // show score

            showScore();

        }

        

        sc.close();

        // see who's won

        if(playerScore == 121)

            System.out.println("Player has won!");

        else

            System.out.println("Computer has won!");

    }

}

// main class to test the above classes

class main_class {

    public static void main(String[] args) {

        Matador m = new Matador();

        m.playGame();

    }

}

OUTPUT


Related Solutions

Two 6-sided dice are rolled. One die is a standard die, and the other is a...
Two 6-sided dice are rolled. One die is a standard die, and the other is a Fibonacci die with sides 1, 1, 2, 3, 5, and 8. a. What is the probability distribution of this experiment? b. What is the shape of the probability distribution? c. What is the expected value when these 2 dice are rolled?
role playing games like dungeons & dragons use many different types of dice. suppose that a...
role playing games like dungeons & dragons use many different types of dice. suppose that a SIX sided die has faces marked 1,2,3,4,5,6. The intelligence of a character is determined by rolling this die twice and adding 1 to the sum of the spots. The faces are equally likely and the two rolls are independent. what is the average (mean) intelligence for such characters? how spread out are their intelligence , as measured by the standard deviation of the distribution?(round...
Part 1: Two 6-sided dice are rolled. One die is a standard die, and the other...
Part 1: Two 6-sided dice are rolled. One die is a standard die, and the other is a Fibonacci die with sides 1, 1, 2, 3, 5, and 8. Submit a file with the probability distribution for this experiment. Part 2: Two 6-sided dice are rolled. One die is a standard die, and the other is a Fibonacci die with sides 1, 1, 2, 3, 5, and 8. What is the shape of the probability distribution? Part 3: Two 6-sided...
Dice Suppose that a red die and a green die are rolled and the numbers on...
Dice Suppose that a red die and a green die are rolled and the numbers on the sides that face upward are observed. (See Example 7 of this section and Example 2 of the first section.) (a) What is the probability that the numbers add up to 9? (b) What is the probability that the sum of the numbers is less than S?
Two fair dice are tossed, and the face on each die is observed. Use a tree...
Two fair dice are tossed, and the face on each die is observed. Use a tree diagram to find the 36 sample points contained in the sample space. Assign probabilities to the sample points in part a. Find the probability of each of the following events: A = {3 showing on each die} B = {sum of two numbers showing is 7} C = {sum of two numbers showing is even} Also, for events A, B, and C, which one...
Four standard dice were rolled. Then each die that did not come up six (any if)...
Four standard dice were rolled. Then each die that did not come up six (any if) was rolled again. Following this, exactly one of the four dice showed six on its upper face. Find the probability that at least one of the four dice came up six on its initial roll.
1. Many games require rolling 2 dice and adding the rolls together. Fill in the table...
1. Many games require rolling 2 dice and adding the rolls together. Fill in the table below with the sum of the two die rolls. The first few cells have been completed as an example. Sum of Die Rolls First Die Roll 1 2 3 4 5 6 Second Die Roll 1 2 3 4 2 3 3 4 5 6 a. We assume die rolls are all equally likely. There are 36 possible outcomes (6x6) when we give them...
An experiment consists of rolling three fair dice --- a red die, a blue die, and...
An experiment consists of rolling three fair dice --- a red die, a blue die, and a white die --- and recording the number rolled on each die. Assume that the dice are fair, so that all outcomes are equally likely. (1) What probability should be assigned to each outcome? equation editorEquation Editor (2) What is the probability that the sum of the numbers rolled is 5? equation editorEquation Editor (3) What is the probability that the sum of the...
Consider a six-sided fair dice. Complete the following tables for this die: Number on the dice...
Consider a six-sided fair dice. Complete the following tables for this die: Number on the dice Number of times it appears on the dice Relative frequency of seeing that number 1 2 3 4 5 6 Total This is the entire question, does not say how many times the dice is thrown
If one dice is rolled (die 1, die 2), find the probability of getting sum less...
If one dice is rolled (die 1, die 2), find the probability of getting sum less than 11? a. What is the probability experiment? Rolling a dice (die 1, die 2) b. What is the event(s)? sum greater than 11 c. What technique can I use to solve this problem? Select an answer d. How do you know you can use that technique? Select an answer f. Find the probability of rolling a sum that is sum less than 11....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT