//Starts game / stays static void play_stay() { if(gameRunning == 0 && player_bet >= BET_INCREMENT) { deal_card(DEALER); //Display First Card To All Here deal_card(DEALER); deal_card(PLAYER); //Display First Card To All Here deal_card(PLAYER); gameRunning = 1; msg = NONE; change_turn(PLAYER); draw_window(); }else{ //Stay if(gameRunning == 1 && turn == PLAYER) { player_stay = 1; draw_window(); change_turn(DEALER); } } }
int test_queue(){ int i,j; Card card; Deck * deck = malloc(sizeof(Deck)); Deck * deck2 = malloc(sizeof(Deck)); Hand * hand = malloc(sizeof(Hand)); build_deck(deck); shuffle_deck(deck); build_deck(deck2); shuffle_deck(deck2); i = 0; for (;;){ printf("Initialized Deck and Hand\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); for (j = 0; j < HAND_SIZE; ++j){ card = deal_card(deck); hand->card[j] = card; } printf("\nAdded cards to hand\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); for (j = 0; j < HAND_SIZE; ++j){ add_card(deck, hand->card[j]); hand->card[j].suite = -1; hand->card[j].value = -1; } printf("\nPut cards back into deck\n"); print(deck->card, DECK_SIZE); print(hand->card, HAND_SIZE); printf("\n\n"); i++; if (i == 100) break; } sort_cards(deck->card, DECK_SIZE); sort_cards(deck2->card, DECK_SIZE); for (i = 0; i < DECK_SIZE; ++i) if (deck->card[i].value != deck2->card[i].value) printf("Bad Shuffle\n"); printf("Good Shuffle\n"); return 0; }
/* exchanging cards from the deck into the exchange struct */ Exchange * exchange_card(Deck * deck, Exchange * e, int count){ Card card; int i; for (i = 0; i < count; ++i){ card = deal_card(deck); add_card(deck, e->card[i]); e->card[i] = card; } return e; }
//Deal card to player static void hit_me() { if(gameRunning == 1 && turn == PLAYER) { deal_card(PLAYER); draw_window(); change_turn(PLAYER); } }
/* 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; }
//Dealers turn, keep hitting until 17+, once 17+ stay static void dealer_turn() { if(gameRunning == 1) { if(dealer_stay == 0) { if(dealer_hand >= 17) { dealer_stay = 1; msg = DEALER_STAY; }else{ msg = DEALER_HIT; deal_card(DEALER); } } draw_window(); usleep(1000000); change_turn(DEALER); } }
/* 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(int argc, char** argv){ printf("**********************************************************\n"); printf("* Runbai Ma *\n"); printf("* Final Project *\n"); printf("* Five draw Card Game *\n"); printf("* *\n"); printf("* *\n"); printf("**********************************************************\n"); printf("1.start game \n"); printf("2.quit\n"); char input = getchar(); char play_again = '1'; if(input == '1'){ while(play_again == '1'){ putchar('\n'); srand((unsigned int)time(NULL)); Deck* deck = (Deck*) malloc(sizeof(Deck)); Card* card_arr = malloc (sizeof(Card)*52); int* ret_value = malloc(sizeof(int)); card_arr = shuffle_card(); deck_init(deck,card_arr); /* initial the four player*/ char* name2 = malloc(sizeof(char)*15); name2 = "Computer 1"; Player* p2 = malloc(sizeof(Player)); player_init(p2,name2); deal_card(p2->hand,deck,5); printf("computer 1 hand: "); hand_print(p2->hand); char* name3 = malloc(sizeof(char)*15); name3 = "Computer 2"; Player* p3 = malloc(sizeof(Player)); player_init(p3,name3); deal_card(p3->hand,deck,5); printf("computer 2 hand: "); hand_print(p3->hand); char* name4 = malloc(sizeof(char)*15); name4 = "Computer 3"; Player* p4 = malloc(sizeof(Player)); player_init(p4,name4); deal_card(p4->hand,deck,5); printf("computer 3 hand: "); hand_print(p4->hand); char* name = malloc(sizeof(char)*15); name = "Poker King"; Player* p1 = malloc(sizeof(Player)); player_init(p1,name); deal_card(p1->hand,deck,5); printf("your hand: "); hand_print(p1->hand); printf("----------------------------------------------------------\n"); putchar('\n'); ret_value = monte_carlo_advisor(p2->hand); change_card_by_montecarlo(ret_value,p2->hand,deck); compute_hand_afterchange(p2->hand); ret_value = monte_carlo_advisor(p3->hand); change_card_by_montecarlo(ret_value,p3->hand,deck); compute_hand_afterchange(p3->hand); ret_value = monte_carlo_advisor(p4->hand); change_card_by_montecarlo(ret_value,p4->hand,deck); compute_hand_afterchange(p4->hand); printf("Monte Carlor suggestion for u: \n"); putchar('\n'); ret_value= monte_carlo_advisor(p1->hand); print_montecarlo_result(ret_value); printf("----------------------------------------------------------\n"); putchar('\n'); printf("Your hand: "); hand_print(p1->hand); putchar('\n'); char c; c = getchar(); printf("Enter the card index you want to change: \n"); while((c = getchar()) != '\n'){ //int i = atoi(&c); //printf("oh god: %d\n", i); if(c == '1') change_card(p1->hand, deck, 1); else if(c == '2') change_card(p1->hand, deck, 2); else if(c == '3') change_card(p1->hand, deck, 3); else if(c == '4') change_card(p1->hand, deck, 4); else if(c == '5') change_card(p1->hand, deck, 5); } compute_hand_afterchange(p1->hand); printf("----------------------------------------------------------\n"); putchar('\n'); printf("computer 1 after change is: "); hand_print(p2->hand); printf("computer 2 after change is: "); hand_print(p3->hand); printf("computer 3 after change is: "); hand_print(p4->hand); printf("your hand after change is : "); hand_print(p1->hand); printf("----------------------------------------------------------\n"); putchar('\n'); printf("the game result is: \n"); Player* p[4] = {p1,p2,p3,p4}; int winner = get_winner(p); if(winner == 0){ printf("You won the game!\n"); } else{ printf("%s won the game!\n", p[winner]->name); } putchar('\n'); putchar('\n'); putchar('\n'); printf("1.play again\n"); printf("2.quit\n"); play_again = getchar(); } } }
int main() { printf("Welcome\n\n"); printf("是否開始遊戲?\n 1.是 開始遊戲\n 2.否 離開遊戲\n\n"); int choice, i, j; printf("選擇(請輸入1或2): "); while(EOF!=scanf("%d", &choice)) { printf("\n\n"); if(choice==2) { printf("\nSee you next time\n"); break; } else if(choice==1) { srand(time(NULL)); int round; for(round=0; round<5; round++) { printf("#round %d\n\n", (round+1)); printf("玩家手牌:\n"); shuffling(2); for(i=0; i<5; i++) { for(j=0; j<13; j++) { hand_player1[i][j]=temp_card[i][j]; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { temp_card[i][j]=0; } } printf("對手手牌:\n"); shuffling(2); for(i=0; i<5; i++) { for(j=0; j<13; j++) { hand_player2[i][j]=temp_card[i][j]; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { temp_card[i][j]=0; } } printf("公牌:\n"); shuffling(5); for(i=0; i<5; i++) { for(j=0; j<13; j++) { shared_card[i][j]=temp_card[i][j]; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { temp_card[i][j]=0; } } for(i=0; i<5; i++) { for(j=2; j<13; j++) { hand_player1[i][j]=shared_card[i][j-2]; } } for(i=0; i<5; i++) { for(j=2; j<13; j++) { hand_player2[i][j]=shared_card[i][j-2]; } } int hand_player1_temp[14],hand_player2_temp[14]; int k=0; for(i=0; i<13; i++) { for(j=0; j<5; j++) { if(hand_player1[j][i]!=0) { hand_player1_temp[k]=hand_player1[j][i]; k++; } } } k=0; for(i=0; i<13; i++) { for(j=0; j<5; j++) { if(hand_player2[j][i]!=0) { hand_player2_temp[k]=hand_player2[j][i]; k++; } } } bubblesort(hand_player1_temp); bubblesort(hand_player2_temp); printf("\n\n"); printf("玩家手牌加公牌(已排序):\n"); for(i=0; i<14; i++) { printf("%c",hand_player1_temp[i]); if(i!=0&&i%2==1) printf(" "); } printf("\n\n"); printf("對手手牌加公牌(已排序):\n"); for(i=0; i<14; i++) { printf("%c",hand_player2_temp[i]); if(i!=0&&i%2==1) printf(" "); } printf("\n\n"); int a,b; printf("玩家:"); a=deal_card(hand_player1_temp); printf("對手:"); b=deal_card(hand_player2_temp); if(a<b) { printf("對手 win!!\n"); } else if(a>b) { printf("玩家 win!!\n"); } else if(a==b) { printf("平手\n"); } system("pause"); printf("\n\n"); printf("***************************************"); printf("\n\n"); for(i=0; i<5; i++) { for(j=0; j<13; j++) { hand_player1[i][j]=0; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { hand_player2[i][j]=0; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { shared_card[i][j]=0; } } for(i=0; i<5; i++) { for(j=0; j<13; j++) { temp_card[i][j]=0; } } } } printf("==SEE YOU NEXT TIME==\n\n"); break; } }