E. Final Program with Comments

Everything that is bold and italicized is a comment in the code.

clc
clear
%Intro of the Game
fprintf(‘Welcome to Over/Under 7!\n’)
fprintf(‘Menu Options:\n’)
fprintf(‘1.Play\n’)
fprintf(‘2.Instructions/Rules (If you read the instructions/rules, you will have to run the program again to play the game)\n’)
fprintf(‘3.Quit\n’)
userchoice = input(‘Enter the number of option you wish to choose: \n’);
%if the user chooses to play
if userchoice == 1
total = input(‘How much money do you have today? (Enter a non-negative integer) \n’);
playAgain = ‘y’;
bet = input(‘Enter how much money you wish to bet on the roll (Enter a positive bet): \n’);
%if the user has money and wants to play
while ((total > 0) && (strcmp(playAgain,’y’)==1))
estimatedDecision = input(‘Enter ”Under 7” if you wish to bet under 7.\nEnter ”Over 7” if you wish to bet over 7.\nEnter ”Exactly 7” if you wish to bet on exactly 7.\n’,’s’);
fprintf(‘The dice will now be rolled.\n’)
load Dice;
roll = randi([1 6],[1 2])
imshow([Dice{roll}])
dicetotal = roll(1)+roll(2);

if dicetotal < 7 %if the total of the value of two dices is under 7
actualResult = ‘Under 7’;
elseif dicetotal > 7 %if the total of the value of two dices is over 7
actualResult = ‘Over 7’;
else %if the total of the value of two dices is exactly 7
actualResult = ‘Exactly 7’;
end

%if the user guessed correctly and the guess was under 7
if (strcmp(estimatedDecision,actualResult)==1 && (strcmp(actualResult,’Under 7′)==1))
total = total + bet;
%if the user guessed correctly and the guess was over 7
elseif ((strcmp(estimatedDecision,actualResult)==1) && (strcmp(actualResult,’Over 7′)==1))
total = total + bet;
%if the user guessed correctly and the guess was equal to 7
elseif ((strcmp(estimatedDecision,actualResult)==1) && (strcmp(actualResult,’Exactly 7′)==1))
total = total + 4*bet;
%if the user was wrong
else
total = total – bet;
end
fprintf(‘You have $%d dollars left.\n’,total)
%if the user has money left, then the user is asked if he/she
%wants to play again
if (total>0)
playAgain = input(‘Do you wish to play another round? (Enter y/n)\n’,’s’);
end
end
%if the user decides to look at the instructions
elseif userchoice == 2
%Instructions/Rules are based on the ones found on Carmen
fprintf(‘Instructions/Rules: \n\n’)
fprintf(‘1.The game is played with two dice.\n’)
fprintf(‘2.The player specifies how much money he/she is betting on the roll.\n’)
fprintf(‘3.The player bets whether he/she will roll Under 7,Over 7,or Exactly 7.\n’)
fprintf(‘4.The dice is rolled.\n’)
fprintf(‘5.If the player bets incorrectly, he/she loses the amount of money in the bet.\n’)
fprintf(‘6.If the player bets correctly, the payoff is 1:1 for Under 7 and for Over 7(the player wins $1 if the bet was $1).\n The payoff is 4:1 for Exactly 7(if the bet was $1 then the player wins $4).\n’)
fprintf(‘7.Player can start game with any amount of money, and can bet any amount(<=total) for each game, until out of money or chooses to quit game.\nNo negative bets.’)
%if the user decides to quit
elseif userchoice == 3
fprintf(‘\nGoodbye.’)
end