Need help writing a slot machine program in C?

July 29, 2010 :: Posted by - admin :: Category - Slot Machine

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(“nn 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(“nn 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(“nn 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!

Tags: , , , , ,

Comments Closed

One Response to “Need help writing a slot machine program in C?”

  1. Frecklefoot Says:

    Your code will look like this:

    if( (wheel_1 == wheel_2) && (wheel_2 == wheel_3) ) //you already have this
    {

    int winnings = 0;
    switch( wheel_1 )
    {
    case 1:
    winnings = 2 * wager;
    break;
    case 2:
    winnings = 3 * wager;
    break;
    case 3:
    winnings = 4 * wager;
    break;
    // you get the idea for the rest…
    }
    char szWinString[100];
    sprintf( szWinString, “You won $%d!n”, winnings );
    printf(“WINNERn”);
    printf( szWinString );

    }

    HTH


Powered by Yahoo! Answers

Long Awaited Report On Lottery...

Lottery Secrets - The 10 Winning Strategies To Massive Lottery Success

Silver Lotto System
In this report You will discover:
  • 10 Lotto Playing Mistakes You Should Avoid At Any Cost
  • 3 Essential Tips For Your Lottery Game
  • Should You Play 7 Ball Games?
  • The 5 Common Game & Ticket Mistakes You Should Never Make
  • What Secret Number Should You Always Include?
  • ...And Much More
Enter Your Email Below To Get This FREE Report
Long Awaited Report On Lottery...

Lottery Secrets - The 10 Winning Strategies To Massive Lottery Success

Silver Lotto System
In this report You will discover:
  • 10 Lotto Playing Mistakes You Should Avoid At Any Cost
  • 3 Essential Tips For Your Lottery Game
  • Should You Play 7 Ball Games?
  • The 5 Common Game & Ticket Mistakes You Should Never Make
  • What Secret Number Should You Always Include?
  • ...And Much More
Enter Your Email Below To Get This FREE Report
Contact Us | Copyrights | Customer Service Policy | Disclaimer | Privacy | Terms & Conditions