/* #### Twenty-One Simulator #### #### Terry Smith, CMSC 201 0301,0302 #### Created 09/20/98 #### #### This program plays a simplified version #### of Blackjack, with the following limitations: #### #### -Face cards are represented as {1,10,11} #### -No double down, no actual gambling #### #### (1+rand()%11) is used to select a card value from 1-11 #### */ #include #include /* Needed for rand() */ #define YES 1 #define NO 0 /* Constants for readability */ main() { int i, playerCard, dealerCard, /* Hold card values */ playerTotal, dealerTotal; /* Hold totals per hand */ int playerLost; /* Used as flag */ char reply; /* Holds user input */ do { /* Begin Hand */ playerTotal=0; dealerTotal=0; playerLost = NO; printf("\nTwenty One: Get Ready to Play\n\n"); for(i=0;i<2;++i) /* Deal first two cards */ { playerCard = (1+rand()%11); printf("Your card is %d\n",playerCard); playerTotal += playerCard; dealerCard = (1+rand()%11); dealerTotal += dealerCard; } printf("Your beginning hand is %d\n",playerTotal); do { /* Deal to Player */ printf("(h)it or (s)tand? "); scanf("%c",&reply); fflush(stdin); if (reply == 'h') { playerCard = (1+rand()%11); playerTotal += playerCard; printf("Next card is a %d\n",playerCard); printf("Your new total is %d\n",playerTotal); if (playerTotal > 21) { playerLost = YES; } } } while( (reply != 's') && (playerLost == NO) ); if(playerLost == NO) { /* Deal to Dealer */ printf("Dealer starts with %d\n",dealerTotal); while( (dealerTotal < playerTotal) && (dealerTotal < 21) ) { dealerCard = (1+rand()%11); dealerTotal += dealerCard; printf("Dealer gets a %d\n",dealerCard); printf("Dealer's total is %d\n",dealerTotal); } if(dealerTotal>21) { printf("Dealer BUSTED!\n"); } else { playerLost = YES; } } if(playerLost == YES) /* Find out who won */ { printf(" I'm Sorry, you LOSE!!!\n"); } else { printf(" You've WON!!!! \n"); } printf(" Do you want to play again (y/n)? "); scanf("%c",&reply); fflush(stdin); } while(reply == 'y'); }