void Hand::add_card(Card c) { if (c.get_rank() == 12) { if ((hand_value() + 11) > 21) value += 1; else { value += 11; soft = true; } } // check to see if ace is present and decide its value else if (c.get_rank() == 8 || c.get_rank() == 9 || c.get_rank() == 10 || c.get_rank() == 11) { value += 10; } // add cards of value of ten else { int rank = static_cast<int>(c.get_rank()); value += rank + 2; } // add numerical cards to value if (soft && value > 21) { value = value - 10; soft = false; } // if ace in hand and bust, revert ace back to value of 1 // set hand to hard count }
/* input: a player pointer, a shoe pointer * output: various player info * return: an int determining if the player busted, split, stood, etc * comments: makes the player play */ int player_play(player * foo, shoe * sho) { int rval = 0; int split = 0; int i = 0; for (i = 0; i < 2; ++i) hand_enq(&foo->one, deal_card(sho)); split = hand_play(&foo->one, sho, 0); if (split == 1) { int test; hand_enq(&foo->two, hand_deq(&foo->one)); test = hand_play(&foo->one, sho, 1); foo->value1 = hand_value(&foo->one); if (test == 0) { fdprintf(OUT, "Busted!!\n"); foo->value1 = 0; } test = hand_play(&foo->two, sho, 2); foo->value2 = hand_value(&foo->one); if (test == 0) { fdprintf(OUT, "Busted!!\n"); foo->value2 = 0; } rval = 2; } if (split == 2) { foo->value1 = hand_value(&foo->one); rval = 1; } if (split == 3) rval = 3; return rval; }
/* input: a dealer pointer, a shoe pointer * output: various dealer info * return: an int on if the dealer had a blackjack or not * comments: makes the dealer play */ int dealer_play(dealer * foo, shoe * sho) { int rval = 0; int x = 0; while (x != 1) { if ((dealer_value(foo) <= 16) && !x) { fdprintf(OUT, "Dealer has : "); hand_print(&foo->one); hand_enq(&foo->one, deal_card(sho)); fdprintf(OUT, " hit\n"); foo->value = hand_value(&foo->one); } if ((dealer_value(foo) >= 17) && (dealer_value(foo) <= 21) && !x) { foo->value = hand_value(&foo->one); fdprintf(OUT, "Dealer has : "); hand_print(&foo->one); fdprintf(OUT, " stand\n"); x = 1; } if ((dealer_value(foo) == 21) && (hand_length(&foo->one) <= 2)) rval = 1; if ((dealer_value(foo) > 21) && !x) { fdprintf(OUT, "Dealer has : "); hand_print(&foo->one); fdprintf(OUT, "\n"); fdprintf(OUT, "Dealer busts!\n"); foo->value = 0; x = 1; } }/* end while x != 1 */ return rval; }
bool Pokerhand::operator >(Pokerhand& o_hand) { std::pair<int, int> val1 = hand_value(); std::pair<int, int> val2 = o_hand.hand_value(); if (val1.first == val2.first) { if (val1.second == val2.second) { return next_highest() > o_hand.next_highest(); } return val1.second > val2.second; } return val1.first > val2.first; }
/* This function prompts the players including AI players for exchanging the cards */ int prompt_for_exchange(Player *players, Deck *d, int godmode, int iteration){ int i, bet, bet_sum, cur_bet = 0; char *choice; /* choice is represented with a string of maximal length 5 */ choice = malloc(6*sizeof(char)); bet_sum = 0; /* first deals with AI players */ for(i = 0; i < 4; ++i){ /* prompt for players if he/she is an alive AI player */ if((players+i)->isAI == 1 && (players+i)->alive == 1){ printf("%s is thinking...\n",(players+i)->name); if(godmode == 1){ printf("%s's cards before exchanging:",(players+i)->name); player_display(players+i); } /* use MC advisor to find out the best move */ choice = MC((players+i)->hand,godmode,iteration); printf("\n%s decides to exchange card %s \n",(players+i)->name,choice); parse_exchange(choice, (players+i)->hand, d); if(godmode == 1){ printf("%s's cards after exchanging:",(players+i)->name); player_display(players+i); } printf("\n"); /* generate the bet for AI players based on their hand value plus some randome variations. */ bet = hand_value((players+i)->hand) + rand() % 10 + i * 10; /* small chance of doubling */ if(rand() % 13 == 0) bet *= 2; /* if the bet is greater than the remaining chips for the player */ if(bet <= (players+i)->chip && bet >= cur_bet){ cur_bet = bet; (players+i)->chip -= bet; printf("[%s is betting %d on his/her hand.]\n\n", (players+i)->name, bet); } /* Bet all the chips */ else if(bet > (players+i)->chip && (players+i)->chip >= cur_bet){ bet = (players+i)->chip; cur_bet = bet; printf("[%s is betting %d on his/her hand.]\n\n", (players+i)->name, bet); } else{ printf("[%s folds.]\n\n",(players+i)->name); (players+i)->fold = 1; bet = 0; } bet_sum += bet; } } /* prompt for exchange for the live player */ printf("********************************************************************************\n"); printf(" Your current hand:\n "); /*player_display(players);*/ hand_value(players->hand); hand_display(players->hand); printf("********************************************************************************\n"); choice = MC(players->hand,godmode,iteration); printf("\nMC simulator suggest exchanging card %s\n\n",choice); printf("%s, please enter the card you want to exchange\n(e.g. To exchange the first card, enter 1; To exchange the second and the fifth card, enter 25;\nIf you do not want to exchange any card, enter 0):",(players)->name); scanf("%s",choice); /* if the player enters a string other than "0", parse the string and exchange the cards */ if(strcmp(choice,"0") != 0){ parse_exchange(choice, players->hand, d); printf("********************************************************************************\n"); printf("Your hand after exchanging:\n"); /*player_display(players);*/ hand_value(players->hand); hand_display(players->hand); printf("********************************************************************************\n"); printf("\n"); } /* no cards is exchanged if the player enters "0" */ else{ printf("No card exchanged for %s.\n",players->name); } /* evaluate the value */ hand_value((players)->hand); printf("Current highest bet: %d.\nPlease enter the amount you want to bet. (Enter 0 to fold)\n",cur_bet); scanf("%d",&bet); while( (bet > (players)->chip || bet < cur_bet) && bet != 0 ){ if(bet > (players)->chip) printf("You current chip is %d, you do not have enough chip, try again with a smaller amount.\n",players->chip); else printf("Please enter a number bigger than the current bet(%d).\n",cur_bet); scanf("%d",&bet); } if(bet == 0) players->fold = 1; players->chip -= bet; bet_sum += bet; cur_bet = bet; return bet_sum; }