Design a program that simulates a slot machine. When the program runs, it should
ID: 3546813 • Letter: D
Question
Design a program that simulates a slot machine. When the program runs, it should do one of the following:
1. Ask the user to enter the amount of money he or she want to insert into the slot machine.
2. Instead displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars.
The program will select and display a word from this list three times.
4. If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
5. The program will ask whether the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered in the slot machine and the total amount won.
Explanation / Answer
clc;
clear all;
close all;
m=1;
while(m~=0)
x=input('please enter the amount you want to invest');
for i=1:3
g(i)=ceil(6*rand);
end
for i=1:3
if g(i)==1
display('cherries');
elseif g(i)==2
display('oranges');
elseif g(i)==3
display('plums');
elseif g(i)==4
display('bells');
elseif g(i)==5
display('melons');
else
display('bars');
end
end
h=3*x;
k=2*x;
if g(1)==g(2)&&g(2)==g(3)
display('you have won three times your money');
display(h);
elseif g(1)~=g(2)&&g(2)~=g(3)&&g(3)~=g(1)
display ('you have won nothing');
display ('amount won zero');
else
display('you have won double your money');
display(k);
end
m=input(':if you want to play again please press 1"');
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.