简体   繁体   中英

Implementing structs an functions in a C porgran

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 ;
 }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM