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