Question

In: Computer Science

Stuck in the mud is a popular dice game in UK. The game uses five (5)...

Stuck in the mud is a popular dice game in UK. The game uses five (5) 6-sided dice to play. The players play in turns.
Choose one player to start the game. The player will roll all five (5) dice. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2 or a 5 becomes stuck in the mud. If this throw does not contain any 2s or 5s, the score is incremented by the sum of the dice values.
The player needs to set aside any 2s and 5s and throw the remaining dice. Again, if any 2s or 5s are rolled, the score will not be incremented for this throw. Throws without 2s and 5s are added to the previous total score.
Continue in this way until all the dice are stuck. Save the score and pass the dice to the next player.
Players can agree a total number of rounds to play in advance. Total up the score. The player with the highest score wins the game. The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud
Write a MATLAB program to simulate the Stuck in the Mud game with additional features that can:
• Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player.
• Greet the player when the game starts.
• Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs like 0, 1.2, -1, 999, and so on...
• The program should not play if the user enters a 0 or any negative value.
• The program should accurately play the number of rounds specified by the user. The player and the computer play in turns for each round.
• The program can always pick one side to start the game first, either the player side or the computer side. Randomly pick a side to start the rotation is optional.
• Print the current round number clearly in the command window.
• If the player side starts first, the program will automatically roll all five (5) dice for the player. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2 or a 5 becomes stuck in the mud. If this throw does not contain any 2s or 5s, the score is incremented by the sum of the dice values. The player needs to set aside any 2s and 5s and throw the remaining dice. Again, if any 2s or 5s are rolled, the score will not be incremented for this throw. Throws without 2s and 5s are added to the previous total score. Continue in this way until all the dice are stuck.
• The dice rolled for the player, the stuck dice, and the scores during the process should clearly be printed in the command window.
• The program then automatically roll all five (5) dice for the computer. Follow the game rules until all five (5) dice are stuck.
• The dice rolled for the computer, the stuck dice, and the scores during the process should also be clearly printed in the command window.
• Accurately track the total scores for the player and the computer.
• After all the rounds have been played, select a winner based on the highest total score. It is also possible that the game ends in a tie.

Solutions

Expert Solution

import java.util.Scanner;

public class DiceGame
{
        public static void main( String [] args )
        {
                //Rules of the Game
                System.out.println("______________________________________");
                System.out.println("/          Rules of the Game         /");
                System.out.println("/          -----------------         /");
                System.out.println("/  1)It's you vs computer.           /");
                System.out.println("/  2)You play by rolling the dice.   /");
                System.out.println("/  3)The first player to reach 100   /");
                System.out.println("/     points wins.                   /");
                System.out.println("/  4)When a player rolls a 1         /");
                System.out.println("/     the turn is over.              /");
                System.out.println("/  5)The computer's turn is over     /");
                System.out.println("/    when turn total reach 20 points /");
                System.out.println("/    in a single turn.               /");
                System.out.println("______________________________________");
                
                PairOfDice d1 = new PairOfDice(); //Creating PairOfDice object
                
                int turnTotal = 0;
                int computerTotal = 0; //your total
                int playerTotal = 0; //computer's total
                int turnOver = 1; //when to give up die
                int winner = 100; // amount to be reached before winning
                
                Scanner in = new Scanner( System.in );
                String answer; // named of what will take answer from user
                
                // first do-while loop is for repeating the change between user and computer
                do{
                        if (playerTotal <= winner && computerTotal <= winner)
                        {
                                System.out.println("Your turn.");
                                
                                // do-while loop for the player's turn.
                                do
                                {
                                System.out.println("Type 'y' if ready and 'n' to end turn.");
                                answer = in.next();
                                
                                if (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner)
                                {
                                        d1.roll();
                                        d1.getDie1();
                                        d1.getDie2();
                                        d1.toString();
                                        System.out.println(d1);                                 
                                        
                                        // if and else statement to figure out whether user's turn is over or not.
                                        if (d1.getDie1() == turnOver || d1.getDie2() == turnOver){
                                                System.out.println("You rolled a 1. Your turn is over.");
                                                System.out.println("Please type 'done' when you are ready to turn the dice over to the Computer.");
                                                answer = in.next();
                                        }
                                        else
                                        {
                                                turnTotal = turnTotal + d1.getDiceSum();
                                                playerTotal = playerTotal + d1.getDiceSum();
                                                System.out.println("Your Turn Total: " + turnTotal);
                                                System.out.println("Your Grand Total: " + playerTotal);
                                        }
                                }
                                }
                                
                                while (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner);
                                turnTotal = 0; // turntotal assigned to 0 again.
                                System.out.println();
                                System.out.println("Your Grand Total is: " + playerTotal);
                                System.out.println("The Computer's Grand Total is: " + computerTotal);
                                System.out.println();
                        
                                //Begin the Computer's turn
                                int endComputerTurn = 20;//when to end computer's turn
                                turnOver = 1; //what die equals for turn to be over
                                int answercomp = 1; 
                                
                                do
                                {
                                        if (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner)
                                        {
                                                d1.roll();
                                                d1.getDie1();
                                                d1.getDie2();
                                                d1.toString();
                                                System.out.println(d1);
                                                                if (d1.getDie1() == turnOver || d1.getDie2() == turnOver)
                                                                {
                                                                        System.out.println("The Computer rolled a 1. Their turn is over.");
                                                                        answercomp = 0;
                                                                }
                                                                
                                                                else
                                                                {
                                                                        turnTotal = turnTotal + d1.getDiceSum();
                                                                        computerTotal = computerTotal + d1.getDiceSum();
                                                                        System.out.println("The Computer's Turn Total is: " + turnTotal);
                                                                        System.out.println("The Computer's Grand Total is: " + computerTotal);
                                                                }
                                        }
                                }
                                
                                while (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner);
                                turnTotal = 0; //turntotal assigned to 0 again.
                        
                                if (playerTotal <= winner || computerTotal <= winner)
                                {
                                        System.out.println();
                                        System.out.println("The Computer's Grand Total is: " + computerTotal);
                                        System.out.println("Your Grand Total is: " + playerTotal);
                                        System.out.println();
                                }
                        
                                else
                                {
                                System.out.println();
                                System.out.println();
                                }
                        }
                }
                
                while(playerTotal <= winner && computerTotal <= winner);
                
                // if-else statements to check if there is a winner
                if (playerTotal >= winner)
                        System.out.println("You win!");
                else
                        System.out.println("You lose ): ");
        
        }
}

Related Solutions

Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud , with additional features that can: • Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player. • Greet the player when the game starts. • Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs...
A baby elephant is stuck in a mud hole. To help pull it out, game keepers use a rope to apply a...
A baby elephant is stuck in a mud hole. To help pull it out, game keepers use a rope to apply a force FA, as part a of the drawing shows. By itself, however, force FAis insufficient. Therefore, two additional forces FB and FC are applied, as in part b of the drawing. Each of these additional forces has the same magnitude F. Themagnitude of the resultant force acting on the elephant in part b of the drawing is k...
Game of dice - fair dice: If I throw a number >= 5 I win. If...
Game of dice - fair dice: If I throw a number >= 5 I win. If he throws a number =< 4 he wins. I throw the first dice. Given that he loses(throws a number >4) what is the probability of me winning ? What is the expected number of throws before either of us wins?
Java programming One of the most popular games of chance is a dice game known as...
Java programming One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2,3,4,5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on...
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:...
Question: You have five dice, like in a game of Yahtzee! Suppose you roll the five...
Question: You have five dice, like in a game of Yahtzee! Suppose you roll the five dice once and sum the numbers the five dice show. (a) What is the mean and the standard deviation of the sum of five dice? (b) Suppose you average 60 of such rolls with the five dice. What is the distribution of this average? (c) What is the chance the average of 60 such rolls is larger than 18?
1. A round in the game Yahtzee begins by rolling five fair dice. Find the probability...
1. A round in the game Yahtzee begins by rolling five fair dice. Find the probability of rolling a: a. one pair (ex 33421 but not 33441), two pair (ex 33441 but not 33444), and three of a kind (ex 24252 but not 24242) 2. Consider a 10x10 matrix that consists of all zeros. ten elements of the matrix are selected at random and their value is changed from a zero to a one. find the probability that the ones...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
Chicago is a group dice game that requires no skill. The objective of the game is...
Chicago is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations (GamezBuff, 2017). How do you play Chicago? There are eleven rounds in the game, one for each combination that can be made by adding two dice, namely the numbers two through 12. Each round has a target combination starting with two and going up all the way to 12. Going clockwise, the players take turns to roll...
Chicago is a group dice game that requires no skill. The objective of the game is...
Chicago is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations (GamezBuff, 2017). How do you play Chicago? There are eleven rounds in the game, one for each combination that can be made by adding two dice, namely the numbers two through 12. Each round has a target combination starting with two and going up all the way to 12. Going clockwise, the players take turns to roll...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT