Objective – creating a simple slot machine. 1. The machine should ask the user h
ID: 3804433 • Letter: O
Question
Objective – creating a simple slot machine.
1. The machine should ask the user how much money he/she is willing to Deposit.
2. The machine should ask the user how much money he/she is willing to Bet per each round.
3. The slot machine will display 3 numbers 0, 1, and 2.
4. The choice of these numbers should be decided by rand() function in c.
[How to use rand() function in c a. Num=rand()%3; //This line will generate a number between 0, 1, and 2 and assign the number to Num variable b. rand() requires #include <stdlib.h>
5. The choice of these 3 numbers should be computed outside of main() function – in other words, use the USER defined function.
6. Each round, the Deposit will be deducted by the amount of the Bet
7. When first two numbers match, the user will receive x1 of the Bet
8. When all three numbers match, the user will receive x2 of the Bet. *So, conditions 6, 7, and 8 express the rule of gambling. No matches, you will lose the Bet money (Deposit - Bet) First two numbers match, you will break even (Deposit – Bet + Bet) When 3 numbers match you make some (Deposit – Bet + 2 x Bet)
9. After each round, the user should be prompted whether he/she would like to play more.
10. If the Deposit money runs out, the game should end automatically.
Code in C
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int choice();
void main()
{
int deposit,bet,match=0,rep;
//Reading the deposit amount from user
printf("how much money you are willing to Deposit: ");
scanf("%d",&deposit);
//Reading the bet amount from user
printf("how much money you are willing to Bet per each round: ");
scanf("%d",&bet);
//Repeating the game until deposit exists and user wants to continue
while(deposit>0)
{
match=choice();
//if all the three numbers are same the user will get back 2*bet money
if(match==3)
{
deposit=deposit-bet+(2*bet);
}
//if atleasr two number matches the user will get back the 1*bet money
else if(match==2)
{
deposit=deposit-bet + bet;
}
//if all the three numbers are different, user will lose bet amount.
else
{
deposit=deposit-bet;
}
printf("Your balance after this round is %d ",deposit);
printf("Do you want to repeat? Press 1 to repeat and 0 to stop");
scanf("%d",&rep);
if(rep==0)
break;
}
getch();
}
//User defined function to select the numbers using random function
int choice()
{
int num1,num2,num3,match=0;
//Selecting numbers using randoom function
num1=rand()%3;
num2=rand()%3;
num3=rand()%3;
printf("You got the %d %d %d selected as choice ",num1,num2,num3);
//Verifying if 3 numbers are equal
if(num1==num2&&num1==num3)
match=3;
//Verifying if all the 3 numbers different
else if(num1!=num2&&num1!=num3&&num2!=num3)
match=0;
//Verifying if any two numbers are same.
else
match=2;
return match;
}
Sample output :
how much money you are willing to Deposit:
10
how much money you are willing to Bet per each round:
4
You got the 2 2 1 selected as choice
Your balance after this round is 10
Do you want to repeat? Press 1 to repeat and 0 to stop1
You got the 1 2 1 selected as choice
Your balance after this round is 10
Do you want to repeat? Press 1 to repeat and 0 to stop1
You got the 0 0 1 selected as choice
Your balance after this round is 10
Do you want to repeat? Press 1 to repeat and 0 to stop1
You got the 2 2 2 selected as choice
Your balance after this round is 14
Do you want to repeat? Press 1 to repeat and 0 to stop1
You got the 1 0 1 selected as choice
Your balance after this round is 14
Do you want to repeat? Press 1 to repeat and 0 to stop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.