0

As an assignment for college I had to write a slot machine in C. I got it working however I have to change it to implement structs and functions. Anyone any ideas how to change my code to satisfy this? I have one function working but I can't figure out the rest. The structs need to be implemented for the column part seen midway through as:

 col1 = rand() % 3 + 1;
 col2 = rand() % 3 + 1;
 col3 = rand() % 3 +1; 
  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  #include <time.h>

  float Calculate( float credits, float bet, int col1, int col2, int col3) 
{

    if( (col1 == col2) && (col2 == col3) )
    {
        printf("\n \n WINNER!!");
        credits = credits + (bet*2) ;
    }

    else if( (col1==col2) || (col1==col3) || (col2==col3) )
        {
            printf ("\n\n Hard luck, you nearly got it. ") ;
            credits = credits- (bet*0.5) ;
        }

    else
    {
        printf("\n \n Sorry you lost.");
        credits = credits - bet ;
    }

    return credits ;    
} 




 int main ()
  {
 setbuf(stdin, NULL);
 setbuf(stdout, NULL);

   float credits=10, bet;
   int col1, col2, col3 ;
   char ans, dummy ;

printf ("\n\n *********** Welcome To My Slot Machine. ************\n\n") ;

    while (credits >= 2 )
 {
printf("\n You have %f credits", credits) ;

printf(" \n\n Please enter the amount you wish to bet: ");
scanf (" %f", &bet ) ; 

    if ( bet > credits)
    {
        printf ("\nYou can only bet what you have.") ;
        continue ;
    }

    if ( bet < 2)
    {
        printf("\nYou must bet at least 2 tokens!");
        continue ;
    }

    srand(time(NULL));

    col1 = rand() % 3 + 1;
    col2 = rand() % 3 + 1;
    col3 = rand() % 3 +1;

    switch (col1) {
        case 1:
            printf("\n\n |Apple| \t") ;
            break;

        case 2:
            printf ("\n\n |Orange| \t ");
            break;

        case 3 :
            printf ("\n\n |Banana| \t ") ;
            break ;

    }

    switch (col2) {
        case 1:
            printf(" |Apple| \t") ;
            break;

        case 2:
            printf (" |Orange| \t ");
            break;

        case 3 :
            printf (" |Banana| \t ") ;
            break ;

    }

    switch (col3) {
        case 1:
            printf(" |Apple| \n \t") ;
            break;

        case 2:
            printf (" |Orange| \n \t ");
            break;

        case 3 :
            printf (" |Banana| \n\t ") ;
            break ;

    }


credits = Calculate(credits, bet, col1, col2, col3 ) ;

  if (credits < 2 )
{
printf(" \n\n\n Sorry You Do not have enough to play. Thank you for playing! \n\n\n") ;
    return 0 ;
}

 else if ( credits>= 2 )
{
    printf ("\n\n\n Would you like to play again? y/n : ", credits) ;
    fflush(stdin) ;
    scanf("%c", &ans) ;


        if ( ans == 'y' )
            {
                continue ;
            }

        else if (ans == 'n' )
            {
printf ("\n\n\n Thank you for playing! You walk away  with %f credits", credits ) ;
                return 0 ;
            }
}
  }


    return 0 ;
 }
1
  • Here's a hint, if you have data that constantly must be passed together to functions, it deserves to be properly grouped in a structure or an array. Commented Feb 9, 2014 at 18:11

1 Answer 1

1

Like this:

struct SlotMachine {
    int col1;
    int col2;
    int col3;
    float credits;
    float bet;
};

void Calculate(struct SlotMachine* sm) 
{

    if( (sm->col1 == sm->col2) && (sm->col2 == sm->col3) )
    {
        printf("\n \n WINNER!!");
        sm->credits = sm->credits + (sm->bet*2) ;
    }

    else if( (sm->col1==sm->col2) || (sm->col1==sm->col3) || (sm->col2==sm->col3) )
        {
            printf ("\n\n Hard luck, you nearly got it. ") ;
            sm->credits = sm->credits- (sm->bet*0.5) ;
        }

    else
    {
        printf("\n \n Sorry you lost.");
        sm->credits = sm->credits - sm->bet ;
    }
} 

Which you'd call like this:

//initialize the members of the struct:
struct SlotMachine sm = { .col1 = 0, .col2 = 0, .col3 = 0, .credits = 0.0, .bet = 0.0};

//assign your values to the members of `sm`
//...

//perform the calculation:
Calculate(&sm);

//sm has been updated, now use the values in it as required:

//e.g.: look at the value of credits:
printf("Credits: %f\n", sm.credits);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.