Question by J: Need help writing a slot machine program in C?
I need help writing a slot machine that takes three random numbers as wheels. I can’t seem to figure out how to set payoffs for each combination (ie 3 1′s = 2x wager, 3 2′s = 4x wager etc.) Listed below is the assignment and source code. Please help!
Write a C program that “approximates” a slot machine. Here’s the general idea (I give you license to be a little creative and make this idea much better): 1.
1. Greet the user and ask him/her to enter some whole-dollar figure that will be used for wagering (this is called the_bank).
2. Ask the user how much of the_bank he/she would like to wager (in whole-dollars).
3. Ask the user to press 1 then enter to pull the lever of the slot machine (any other input produces an error msg and re-asks).
4. Pulling the lever “rolls” 3 dice at once
* Develop a payoff system (different payoffs) for three 1s, three 2s, three 3s, three 4s, three 5s, and three 6s… every other combination is a loser.
5. Add or subtract from the_bank as appropriate (depending upon if the player wins or loses).
6. Ask the player if he/she wants to play again… if so, go to 2… if not, then the game ends and your can inform the player how much money he/she won or lost (hint: you’ll need to keep track of how much money was initially deposited in the_bank).
7.If the_bank goes to 0, the game ends (inform the player).
My source code is:
#include //include statements
#include
#include
int main(void) //main
{
int the_bank = 0; //Declare variables
int wager = 0;
int wheel_1 = 0;
int wheel_2 = 0;
int wheel_3 = 0;
printf(“\n Welcome to Joe’s Slot Machine”); //Greet User State purpose Step 1
printf(“\n Enter a whole dollar amount for which you wish to use as your purse for wagering: “);
scanf(“%d”, &the_bank); //Prompt and receive bank amount //step 2
printf(“\n\n Bank is %5d \n”, the_bank); //Let user know bank Step 3
do
{
printf(“\n Enter wager: “); //Ask for and receive wager Step 4
scanf(“%d”, &wager);
the_bank = (the_bank – wager); //Subtract wager from bank Step 5
printf(“\n\n Bank is %d \n “,the_bank);
printf(“Wager is %d \n”, wager); //State bank and wager Step 6
{
int wheel_1 = 0 , wheel_2 = 0 , wheel_3 = 0 , total = 0 ; //Spin wheel Step 7
srandom( (unsigned) time(NULL) ) ;
wheel_1 = random( ) % 6 + 1 ; // Assogn wheel random values Step 8
wheel_2 = random( ) % 6 + 1 ;
wheel_3 = random( ) % 6 +1 ;
printf(“\n\n Wheel 1 : %d \n”, wheel_1 ); // Display wheel amounts Step 9
printf(” Wheel 2 : %d \n”, wheel_2 );
printf(” Wheel 3 : %d \n”, wheel_3 );
{if( (wheel_1 == wheel_2) && (wheel_2 == wheel_3) )
printf(“WINNER”);
else
printf(“Loser”);
}
}
printf(“\n Bank is: %d \n “,the_bank);
}
while(the_bank > 0); //Close loop Step 11
printf(“\n You have run out of money \n”); //Make the user cry Step 11
}
Best answer:
Answer by AA
{if( (wheel_1 == wheel_2) && (wheel_2 == wheel_3) )
printf(“WINNER”);
switch wheel_1:
case 1:
Bank += wager * 2
case 2:
Bank += wager *3
case 3:
Bank += wager *4
caes 4:
bank += wager *5
case 5:
bank += wager *6
case 6:
bank += wager *7
else
printf(“Loser”);
switch wheel_1:
case 1:
Bank -= wager * 2
case 2:
Bank -= wager *3
case 3:
Bank -= wager *4
caes 4:
bank -= wager *5
case 5:
bank -= wager *6
case 6:
bank -= wager *7
}
}
Give your answer to this question below!