III. Craps

money=-1;
%Loop allows player to keep playing until running out of money
while money~=0
if money<0
money=input(‘How much money do you have?\n’);
else
fprintf(‘You now have %.2f dollars.\n’,money)
end
resume = input(‘Do you want to continue? (Enter for yes, 1 for no)\n’);
if resume
break
end
b=-.005;
%The following conditions keep the bet from being an invalid value
while b<=0|b>money|floor(100*b)~=100*b
b=input(‘How much do you want to bet?\n’);
if floor(100*b)~=100*b
fprintf(‘Bet must be in dollars and full cents.\n’)
end
if b<=0
fprintf(‘Your bet must be greater than zero.\n’)
end
if b>money
fprintf(‘You cannot bet more than you have.\n’)
end
end
load Dice;
%Using randi function to create dice roll
roll = randi([1 6],[1 2]);
S = roll (1) + roll (2);
fprintf(‘You rolled %i.\n’,S)
%Conditional Statements to decide what should happen after first roll
if S==7|S==11
imshow([Dice{roll}]);
fprintf(‘\nYou won %i dollars!\n’,b)
elseif S==2|S==3|S==12
imshow([Dice{roll}]);
fprintf(‘\nYou lost %i dollars!\n’,b)
b=b*(-1);
%Using imshow to show dice after each roll
else
imshow([Dice{roll}]);
v=input(‘Press enter to roll again.\n’);
roll2 = randi([1 6],[1 2]);
k = roll2(1) + roll2(2);
while k~=7&k~=S

roll2 = randi([1 6],[1 2]);
imshow([Dice{roll2}]);
fprintf(‘You rolled %i.\n’, k);
v=input(‘Press enter to roll again.\n’);
k = roll2(1) + roll2(2);
end
if k==7
imshow([Dice{roll2}]);
fprintf(‘You rolled 7.\n\nYou lost %i dollars!\n’, b)
b=b*(-1);
else
imshow([Dice{roll2}]);
fprintf(‘You rolled your first roll.\n\nYou won %i dollars!\n’, b)
end
end
%Adjusts balance with won or lost bets
money=money+b;
end
fprintf(‘You left with $%.2f!\n’, money)