Question

In: Computer Science

Each player throws both dice once per turn. The player only scores when the player throws...

Each player throws both dice once per turn. The player only scores when the player throws doubles. Double-6 scores 25 points. A double-3 cancels out a player’s score and puts the score back to zero. Any double other than a 3 or 6 scores 5 points. Scores are recorded and the first player to obtain a total score of fifty points wins the game.

Write a MATLAB program to simulate the FIFTY dice game that can:

1. Play the FIFTY dice game automatically for one player using two dice.

2. Add your name, purpose, and copyright your program.

3. Clear command window and clear all variables.

4. Randomize the pseudorandom number generator with the MATLAB built-in rng function and provide ‘shuffle’ as the function input.

5. Create a variable that will keep the game score. Set the value of this variable to 0.

6. Create another variable that will count the round number. Set the value of this variable to 1.

7. Welcome the player and briefly explain how to play the game when the program starts.

8. Print the current round number in the command window.

9. Print the current game score in the command window.

10. Generate two random integers between 1 and 6 to represent the face values of two dice.

11. Print the two dice values in the command window.

12. If the value of the 1 st die and the 2nd die are not equivalent with each other: a. No action required. We can optionally display a message that no point will be added to the game score.

13. Else a. If the value of the first die is equivalent with 3: i. Display a message about rolling a double 3 causes the game score to set back to 0. ii. Set the game score to 0. b. Elseif the value of the first die is equivalent with 6: i. Display a message about rolling a double 6 adds 25 points to the game score. ii. Set the game score to game score plus 25. c. Else: i. Print a message about rolling the double dice adds 5 points to the game score. ii. Set the game score to game score plus 5. d. End.

14. End.

15. Increment the round number by 1.

16. The game should keep playing while the player’s game score is less than 50 points. Insert a while loop to wrap around the code generated from step 8 through step 15. Make the existing code generated from steps 8 through 15 the code block of this new while loop.

17. Congratulate the player and show the player’s final game score in the command window.

Solutions

Expert Solution

% Matlab program to simulate the game of fifty
clear all;
clc;
rng('shuffle'); % Randomize the pseudorandom number generator
% set the score to 0 and round to 1
score = 0;
round = 1;
% print the welcome message and rules of the game
fprintf('Welcome to the game of FIFTY!!\nEach player throws two dice once per turn.');
fprintf('\nThe player only scores when the player throws doubles.\nDouble-6 scores 25 points.\nA double-3 cancels out a player’s score ');
fprintf('and puts the score back to zero.\nAny double other than a 3 or 6 scores 5 points.');
fprintf('\nScores are recorded and the first player to obtain a total score of fifty points wins the game.\n')
% loop that continues till the user reach a score of 50
while(score < 50)
% print the current score and round
fprintf('\nCurrent score : %d\nCurrent Round : %d',score,round);
dice = randi([1,6],1,2); % generate the rolls of 2 dice
fprintf('\nDice1 : %d Dice2 : %d',dice(1),dice(2));
% check if both dice are same or not
if(dice(1) ~= dice(2)) % not same
fprintf('\nNo point will be added to the game score');
else % same
if(dice(1) == 6) % if both are 6, add 25
fprintf('\nRolling a double 6 adds 25 points to the game score.');
score = score + 25;
elseif(dice(1) == 3) % if both are 3, set score to 0
fprintf('\nRolling a double 3 causes the game score to set back to 0');
score = 0;
else % if both are anything else, add 5 to score
fprintf('\nRolling the double dice adds 5 points to the game score');
score = score + 5;
end
end
  
round = round + 1;
end
% congratulate the user and print the number of rounds it took for the user
% to reach a score of 50
fprintf('\n\nCongratualtions!! You reached a score of %d in %d rounds\n',score,round);
%end of script

Output:


Related Solutions

A player throws an ordinary dice with faces numbered from 1 to 6, if he throws...
A player throws an ordinary dice with faces numbered from 1 to 6, if he throws 1 he has second throw. Let n denote his total score find p.m.f of random variables N and find E(n)
A game works as follows: the player rolls one dice once (let's call it ''n'') and...
A game works as follows: the player rolls one dice once (let's call it ''n'') and then he rolls it second time (let's call this second roll "m'') and receives nm points . A)What is the probability that n = 1 knowing that the player has received more than 20 points. b) What is the probability that m = 1 knowing that the player has received more than 20 points. c) (What is the probability that the player receives more...
A game works as follows: each player rolls one dice (let's call it ''n''). After both...
A game works as follows: each player rolls one dice (let's call it ''n''). After both players have made the first roll, they have the choice to leave or not. If a player exits, he automatically looses; if not, he rolls a second time (let's call this second roll "m'') and receives nm points . The player with the most points wins, in case of a tie no one wins. a) Give the fundamental space of the two throws. b)...
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:...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die has six faces, which 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. (i) If the sum is 2 or 10 on the first throw, the player wins. (ii) If the sum is 3, 7 or 12 on the first throw, the player loses. (iii)...
A baseball player is 98m away from home plate when he throws the ball at a...
A baseball player is 98m away from home plate when he throws the ball at a 30° angle above horizontal from a height of 1.45 m (the height of his hand). a. What is the initial speed of the throw for the ball to land exactly on home plate? In order to solve this problem, set up equations for x and y position vs time. Put in all known values. You will have two remaining unknowns: v0(the magnitude of the...
A basketball player shoots 3 free throws. On each throw, she has a 70% chance of...
A basketball player shoots 3 free throws. On each throw, she has a 70% chance of making the shot. Assume the outcomes of the throws are independent. What is the probability that she makes the first two free throws and then misses the last one?
You roll 48 dice all at once. If you add up only the even numbers rolled,...
You roll 48 dice all at once. If you add up only the even numbers rolled, what is the chance that the sum will be 104 or more??
Consider each situation in turn and for each say whether hashing, encryption or both would be...
Consider each situation in turn and for each say whether hashing, encryption or both would be applicable, and briefly outline how it would be used. A user logs in to an online shop. A user shares a document with another person over a network. The document is not confidential, but the parties involved want to be able to check the document has not been accidentally corrupted on route. A user shares a document with another person over a network and...
two fair dice are each rolled once. Let X denote the absolute value of the difference...
two fair dice are each rolled once. Let X denote the absolute value of the difference between the two numbers that appear. List all possible values of X Find the probability distribution of X. Find the probabilities P(2<X<5) and P(2£X<5). Find the expected value mand standard deviation of X.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT