/* Discrete-event simulation of a simple casino poker game */ /* to demonstrate simulation approach and random numbers. */ /* */ /* Usage: cc -o poker poker.c */ /* ./poker */ /* */ /* Written by Carey Williamson March 12, 2013 */ #include #define NUM_ROUNDS 8 #define NUM_PLAYERS 4 #define NUM_CARDS 1 #define BANK_ROLL 10 #define MIN_WAGER 1 #define MAX_WAGER 5 #define DECK_SIZE 10 #define AVAILABLE -1 #define DEBUG 1 /***********************************************************************/ /* RANDOM NUMBER GENERATION STUFF */ /***********************************************************************/ /* Parameters for random number generation. */ #define MAX_INT 2147483647 /* Maximum positive integer 2^31 - 1 */ /* Generate a random floating point value uniformly distributed in [0,1] */ float Uniform01() { float randnum; /* get a random positive integer from random() */ randnum = (float) 1.0 * random(); /* divide by max int to get something in 0..1 */ randnum = (float) randnum / (1.0 * MAX_INT); return( randnum ); } int main() { int round; int money[NUM_PLAYERS]; int bet[NUM_PLAYERS]; int handvalue[NUM_PLAYERS]; int deckofcards[DECK_SIZE]; int i, j, card; float prob; int best, winner, pot; /* set random number seed based on time of day */ srandom(time(NULL)); /* Give each player some money to start with */ for( i = 0; i < NUM_PLAYERS; i++ ) { money[i] = BANK_ROLL; } /* Main loop: repeat once for each round of poker being simulated */ for( round = 1; round <= NUM_ROUNDS; round++ ) { printf("Starting round %d of poker simulation...\n", round); /* Report bank balance for each player */ for( i = 0; i < NUM_PLAYERS; i++ ) { printf(" Player %d has %d to spend\n", i, money[i]); } /* Shuffle the deck to make all cards available */ for( i = 0; i < DECK_SIZE; i++ ) { deckofcards[i] = AVAILABLE; } /* Deal cards to each player */ for( i = 0; i < NUM_PLAYERS; i++ ) { /* Don't let bankrupt players play any more rounds */ if( money[i] <= 0 ) { printf(" Player %d is BROKE, and gets no cards!\n", i); handvalue[i] = 0; continue; } handvalue[i] = 0; for( j = 0; j < NUM_CARDS; j++ ) { /* Generate a card (at random) for this player's hand */ card = Uniform01() * DECK_SIZE; while( deckofcards[card] != AVAILABLE ) { card = Uniform01() * DECK_SIZE; } #ifdef DEBUG printf(" Giving card %d to player %d\n", card, i); #endif deckofcards[card] = i; handvalue[i] += card; } } /* Collect wagers from each player */ pot = 0; for( i = 0; i < NUM_PLAYERS; i++ ) { bet[i] = MIN_WAGER + (Uniform01() * (MAX_WAGER - MIN_WAGER)); if( bet[i] > money[i] ) bet[i] = money[i]; #ifdef DEBUG printf(" Player %d is wagering %d\n", i, bet[i]); #endif pot += bet[i]; money[i] -= bet[i]; } #ifdef DEBUG printf(" Total pot for round %d is %d\n", round, pot); #endif /* Determine winner */ winner = -1; best = -1; for( i = 0; i < NUM_PLAYERS; i++ ) { if( handvalue[i] > best ) { best = handvalue[i]; winner = i; } } money[winner] += pot; printf("Player %d wins the round %d pot of %d with a hand value of %d!!\n\n", winner, round, pot, best); } }