In: Computer Science
With C language
A slot machine has three windows. A random fruit is picked for each window from cherry, apple, lemon, and orange. If all three windows match, the user wins 8 times the bet amount. If the first two windows only are cherries, the user wins 3 times the bet amount. If the first window is a cherry and the second window is not a cherry, the user wins the bet amount. Otherwise, the player loses their bet. Write a C program that will first allow the user to enter a bet amount. Next, the program should pick 3 random fruits for the 3 windows and print the 3 fruits picked. Lastly, the amount of money won or lost by the user should be displayed.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main(){
//seeding random number generator with time
srand(time(NULL));
int bet;
//asking, reading bet amount
printf("Enter the bet amount (integer): ");
scanf("%d",&bet);
//creating an array of 4 fruits
const char* choices[]={"cherry","apple","lemon","orange"};
//choosing random fruits for three windows
//(generating an index between 0 and 3, taking that value from choices array, storing in each
//window)
const char* window1=choices[rand()%4];
const char* window2=choices[rand()%4];
const char* window3=choices[rand()%4];
//displaying picked fruits for each window
printf("\nFruits picked: %s - %s - %s\n\n",window1,window2,window3);
//checking if all three windows have same fruit
if(strcmp(window1,window2)==0 && strcmp(window2,window3)==0){
printf("all three windows match, you won %d!\n",bet*8); //8x
}
//otherwise checking if first two windows are cherries
else if(strcmp(window1,"cherry")==0 && strcmp(window2,"cherry")==0){
printf("first two windows are cherries, you won %d!\n",bet*3); //3x
}
//otherwise checking if first window has cherry
else if(strcmp(window1,"cherry")==0 && strcmp(window2,"cherry")!=0){
printf("first window is a cherry and the second window is not a cherry, you won %d!\n",bet); //1x
}
//otherwise lost.
else{
printf("Sorry! You lose!\n");
}
return 0;
}
/*OUTPUT (different runs)*/
Enter the bet amount (integer): 10
Fruits picked: cherry - cherry - orange
first two windows are cherries, you won 30!
Enter the bet amount (integer): 5
Fruits picked: cherry - apple - apple
first window is a cherry and the second window is not a cherry, you won 5!
Enter the bet amount (integer): 10
Fruits picked: cherry - cherry - cherry
all three windows match, you won 80!
Enter the bet amount (integer): 20
Fruits picked: apple - cherry - lemon
Sorry! You lose!