In: Computer Science
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.
% 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: