/******************************************************************* 
* 
*	slot.c 
* 
*	Simulates a slot machine. 
* 
*	INPUT:	number of games to play and RNG seed 
* 
*	OUTPUT:	results of games 1-5 
*				number of games 
*				number won 
*				percent won
* 
*	S. Dykes 
*	3-20-2001 
* 
*******************************************************************/ 
#include <stdio.h>  
#include <stdlib.h> 
 
#define	NBR_POSSIBLE_FRUITS  6  
#define	WILD	1  
#define	BANANA	2 
#define	CHERRY	3 
#define	ORANGE	4 
#define	LEMON	5 
#define	GRAPE	6 
 
/*-----------------------------
*	Function prototypes	
*------------------------------*/
int	slot		(int); 
int	won_game	(int,int,int); 
int	rand_int	(int,int); 
char*	fruit_name	(int);
 

int main (int argc, void **argv) 
{  
	int	seed, fruit1, fruit2, fruit3; 
	double	i, games, won;
  
	/*--------------------------------------*/
	/*   	Input number of games, RNG seed	*/ 
	/*--------------------------------------*/ 
	printf ("Enter RNG seed:  "); 
	scanf  ("%d", &seed); 
	srand  (seed); 
 
	printf ("Enter number of games to play:  "); 
	scanf  ("%lf", &games); 

	/*------------------------------*/  
	/*	Loop over games		*/   
	/*------------------------------*/	  
	for (i=0, won=0; i<games; i++) 
	{ 
		fruit1= slot(NBR_POSSIBLE_FRUITS); 
		fruit2= slot(NBR_POSSIBLE_FRUITS); 
		fruit3= slot(NBR_POSSIBLE_FRUITS); 
		if (won_game(fruit1,fruit2,fruit3)) won++; 
		if (i<10) 
		{ 
		     printf ("\nGame %g:\t%10s %10s %10s", i, fruit_name(fruit1), 
			fruit_name(fruit2), fruit_name(fruit3)); 
		     if (won_game(fruit1,fruit2,fruit3)) printf (" * WON"); 
		} 
		 
	} 
	printf ("\n\nGames played:  %.0f\n", games); 
	printf ("Games won:     %.0f\n",   won); 
	printf ("Percent won:   %.2f\n", (100.*won)/games); 
} 
 
/*------------------------------------------------ 
*	slot() 
* 
*	Returns fruit shown in slot window.  
*-------------------------------------------------*/ 
int slot (int nbr_fruits) 
{ 
	return ((int)rand_int(1,nbr_fruits)); 
} 
 
/*------------------------------------------------ 
*	won_game() 
* 
*	Returns 1 if slot game is won, 0 if lost.  
*-------------------------------------------------*/ 
int won_game (int f1, int f2, int f3) 
{ 
	if  (f1==f2   && f2==f3) return 1; 
	if  (f1==WILD && f2==f3) return 1; 
	if  (f2==WILD && f1==f3) return 1; 
	if ((f1==WILD && f2==WILD) || (f1==WILD && f3==WILD) || 
		(f2==WILD && f3==WILD)) return 1; 
	 
	return 0; 
} 

/*------------------------------------------------ 
*	fruit_name() 
* 
*	Returns string containing name of the fruit. 
*-------------------------------------------------*/ 
char* fruit_name(int f) 
{ 
	switch (f) 
	{ 
	case BANANA:	return("BANANA"); 
	case CHERRY:	return("CHERRY"); 
	case ORANGE:	return("ORANGE"); 
	case GRAPE:		return("GRAPE"); 
	case LEMON:		return("LEMON"); 
	case WILD:		return("WILD"); 
	default:		return("ERROR"); 
	}
}  

/*------------------------------------------------ 
*	rand_int() 
* 
*	Returns random integer in the range [a,b]  
*-------------------------------------------------*/ 
int rand_int (int a, int b) 
{ 
	return (a + rand()%(b-a+1)); 
} 
