/* Display the hand of a player */ void player_display(Player *player){ int i; for(i = 0; i < 5; ++i){ card_print((player->hand->cards)[i]->suit, (player->hand->cards)[i]->rank); } printf("\n"); }
/*Display a hand */ void hand_display(Hand *hand){ int i; char *class_s = malloc(20*sizeof(char)); switch(hand->class){ case 1: class_s = "High card"; break; case 2: class_s = "One pair"; break; case 3: class_s = "Two pair"; break; case 4: class_s = "Three of a kind"; break; case 5: class_s = "Straight"; break; case 6: class_s = "Flush"; break; case 7: class_s = "Full house"; break; case 8: class_s = "Four of a kind"; break; case 9: class_s = "Straight flush"; break; case 10: class_s = "Royal flush"; break; } for(i = 0; i < 5; ++i){ card_print((hand->cards)[i]->suit, (hand->cards)[i]->rank); } printf("[%s]",class_s); printf("\n"); }
/* This function displays the current deck of cards in order. I implemented using a temp queue that stores the cards as they are removed from the origin queue and enqueue them again after displaying all the cards. */ void deck_display(Deck *deck){ int padding = 0; Deck *tmp = malloc(sizeof(Deck)); Card *c = malloc(sizeof(Card)); queue_init(tmp,NULL); while(deck->size > 0){ if(padding % 13 == 0){ printf("\n"); } padding++; queue_dequeue(deck, (void**)&c); card_print(c->suit, c->rank); queue_enqueue(tmp, (const void*)c); } printf("\n"); /* Move the cards back from the temp queue */ while(tmp->size > 0){ queue_dequeue(tmp, (void**)&c); queue_enqueue(deck, (const void*)c); } free(tmp); }
/* input: a dealer pointer, a shoe pointer * output: various dealer info * return: an int with the first cards value * comments: starts the dealer and shows the first card */ int dealer_start(dealer * foo, shoe * bar) { int i; int rval = 0; card tmp; for (i = 0; i < 2; ++i) { card_init(&tmp); tmp = deal_card(bar); hand_enq(&foo->one, tmp); ++foo->cnt; foo->value += card_value(&tmp); if (i == 0) { fdprintf(OUT, "Dealer shows : "); card_print(&tmp); rval = card_value(&tmp); fdprintf(OUT, "\n"); } } return rval; }
int main() { struct deck my_deck; struct card my_card,card_result; int res,i; #define CHECK_DECK_OP() \ if ( res != DECK_OK ) {\ deck_get_op_result(res);\ deck_print_cards_cnt(&my_deck);\ } //10 cards at first res = deck_init(&my_deck,10); CHECK_DECK_OP(); //look in empty deck printf("look in empty deck test\n"); res =deck_look(&my_deck,&my_card); CHECK_DECK_OP(); card_print(&my_card); printf("look in empty deck test end\n"); printf("draw from empty deck test\n"); res = deck_draw(&my_deck,&my_card); CHECK_DECK_OP(); card_print(&my_card); printf("draw from empty deck test end\n"); printf("push in deck test\n"); card_init(&my_card,"test",1,1,1); res =deck_push(&my_deck,&my_card); CHECK_DECK_OP(); res =deck_look(&my_deck,&card_result); CHECK_DECK_OP(); card_print(&card_result); printf("push in deck test end\n"); printf("push five cards and draw 5 cards test\n"); for( i =0; i < 5; i ++ ){ card_init(&my_card,"test",i,i,i); res = deck_push(&my_deck,&my_card); CHECK_DECK_OP(); } printf("get last 10 cards from deck\n"); for ( i =0; i < 10; i++ ){ res = deck_draw(&my_deck,&card_result); CHECK_DECK_OP(); card_print(&card_result); } printf("push five cards and draw 10 cards test end\n"); printf("reallocation test\n"); printf("old size [%d]\n",deck_get_size(&my_deck)); for ( i =0; i < 15; i++ ){ card_init(&my_card,"test",i,i,i); res = deck_push(&my_deck,&my_card); CHECK_DECK_OP(); } printf("new size [%d]\n",deck_get_size(&my_deck)); printf("reallocation test end\n"); printf("Deck deinit\n"); deck_deinit(&my_deck); return 0; }
int mc_findbest(Hand *hand, bool player){ int i, j, k, n; double score = 0.0; int best=0; double best_ev=0; Hand *handcopy = (Hand *)malloc(sizeof(Hand)); Deck *deck = (Deck *)malloc(sizeof(Deck)); hand_init(handcopy); deck_init(deck); if(hand->numCards < NUM_CARDS){ printf("Error: Can only advise on a full hand.\n"); return 0; } hand_sort(hand); for(i=0; i<=31; i++){/*0-31 corresponds to 00000-11111 in binary, 1 means discard that card*/ unsigned int seed = time(NULL); score = 0; for(j=0; j<NUM_TRIALS; j++){ for(k=0; k<NUM_CARDS; k++){ hand_add_card(handcopy, deck_remove_card(deck, hand->cards[k])); } if(handcopy->numCards < NUM_CARDS){ hand_print(hand); hand_print(handcopy); printf("Error: Could not find all cards in deck"); return 0; } seed = (seed+5); deck_shuffle(deck, seed); n=i; for(k=NUM_CARDS-1; k>=0; k--){ if(n >= power(2, k)){ n -= power(2, k); hand_discard(handcopy, k, deck); } } hand_draw5(handcopy, deck); score += imp_score_hand(handcopy); for(k=0; k<NUM_CARDS; k++){ hand_discard(handcopy, k, deck); } } if(score/NUM_TRIALS > best_ev){ best = i; best_ev = score/NUM_TRIALS; } } if(player){ printf("Current hand score: %.2f\n", imp_score_hand(hand)); printf("Best expected hand score: %.2f\n", best_ev); printf("Advise discarding cards:\n"); for(k=NUM_CARDS-1; k>=0; k--){ if(best >= power(2, k)){ best -= power(2, k); printf("\t"); printf("%d: ", k); card_print(hand->cards[k]); printf("\n"); } } printf("\n"); } hand_destroy(handcopy, deck); free(handcopy); deck_destroy(deck); free(deck); return best; }