In: Computer Science
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.
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 ): "); } }