clc
clear
load Dice; %load the dice
Balance = 0; %index variable
Balance = input(‘How much money do you have?\n’); %ask the user how much money they have
while Balance > 0 %allow user to continue while balance is greater than 0
fprintf(‘You have $%.2f\n’,Balance)
resume = input(‘Do you want to continue? (Any number larger than 0 to continue, 0 to end)\n’); %ask if the user if they want to keep playing
if ~resume;
break %if the user input 0, exit the while loop
end %end if statement
while true
Bet_Amount= input(‘How much money do you want to bet?\n’); %ask the user how much they want to bet
if Bet_Amount<=Balance && Bet_Amount>0 && floor(100*Bet_Amount)==100*Bet_Amount
break %if the bet is valid (greater than 0 and less than or equal to their balance) exit the inner while loop
end
fprintf(‘Invalid bet.\n’) %if the bet is 0 or greater than their balance, ask for another bet
end
fprintf(‘You bet $%.2f\n’,Bet_Amount) %display the bet
A = [8:12]; %index A
B = 7;%index B
C = [2:6]; %index C
while true
sumbet = input(‘What do you think the sum of the dice will be? [A>7 B=7 C<7]\n’,’s’); %ask what the user thinks the bet will be
if sumbet == ‘A’ || sumbet == ‘B’ || sumbet == ‘C’
break %if the bet is A, B, or C, exit the inner while loop
end
fprintf(‘Invalid bet. Enter A, B, or C.\n’) %if the bet i not A, B, or C, ask for a valid bet
end
roll = randi([1 6],[1 2]); %roll the dice
troll = (roll(1)+roll(2)); %sum the roll
imshow([Dice{roll}]); %show what values were rolled
if sumbet == ‘A’ && troll>7
fprintf(‘You win $%.2f\n’,Bet_Amount) %if the user bet A and the sum of the roll was greater than 7, they win
Balance = Balance+Bet_Amount; %add amount bet to original balance
fprintf(‘Your new balance is $%.2f\n’,Balance) %display new balance
elseif sumbet == ‘C’ && troll<7
fprintf(‘You win $%.2f\n’,Bet_Amount) %if the user bet C and the sum of the roll was less than 7, they win
Balance = Balance+Bet_Amount; %add amount bet to original balance
fprintf(‘Your new balance is $%.2f\n’,Balance)%display new balance
elseif sumbet == ‘B’ && troll==7
fprintf(‘You win $%.2f\n’,4*Bet_Amount) %if the user bet B and the sum of the roll was equal to 7, they win
Balance = Balance + 4*Bet_Amount; %add four times amount bet to original balance
fprintf(‘Your new balance is $%.2f\n’,Balance)%display new balance
else
Balance = Balance-Bet_Amount; %subtract amount bet from balance
fprintf(‘You lose $%.2f.\n’,Bet_Amount) %if the user did not satisfy conditions, they lose the amount they bet
fprintf(‘Your new balance is $%.2f\n’,Balance) %display new balance
end %end if statement
end %end while loop
fprintf(‘You left with $%.2f.\n’,Balance) %the user left the loop with a new balance