int is_straight (hand_t hand) { int is_straight = 0; int i; sort_hand(hand); /** Sorts the hand and checks whether the difference between successive terms is 1 **/ if (hand[4].value != 14) { if ( (hand[1].value - hand[0].value) == 1) { if ( (hand[2].value - hand[1].value) == 1) { if ( (hand[3].value - hand[2].value) == 1) { if ( (hand[4].value - hand[3].value) == 1) { is_straight = 1; } } } } } else { if ( (hand[1].value - hand[0].value) == 1) { if ( (hand[2].value - hand[1].value) == 1) { if ( (hand[3].value - hand[2].value) == 1) { is_straight = 1; } } } } return is_straight; }
int is_royalflush (hand_t hand) { sort_hand(hand); int is_royalflush = 0; if ( (hand[0].suit == hand[1].suit) && (hand[1].suit == hand[2].suit) && (hand[2].suit == hand[3].suit) && (hand[3].suit == hand[4].suit) ) { if ( (hand[0].value == 10) && (hand[1].value == 11) && (hand[2].value == 12) && (hand[3].value == 13) && (hand[4].value == 14) ) { is_royalflush = 1; } } return is_royalflush; }
int is_straightflush (hand_t hand) { sort_hand(hand); int is_straightflush = 0; if ( (hand[0].value == (hand[1].value - 1)) && (hand[1].value == (hand[2].value-1)) && (hand[2].value == (hand[3].value - 1)) && (hand[3].value == (hand[4].value -1))) { if ( (hand[0].suit == hand[1].suit) && (hand[1].suit == hand[2].suit) && (hand[2].suit == hand[3].suit) && (hand[3].suit == hand[4].suit) ) { is_straightflush = 1; } } return is_straightflush; }
int is_threeofakind (hand_t hand) { int i; int threeOfAKind = 0; sort_hand(hand); for (i = 0; i < 3; i++) { if ( (hand[0].value == hand[i + 1].value) && (hand[i + 1].value == hand[i + 2].value) ) { threeOfAKind++; break; } } return threeOfAKind; }
/* compares the hands based solely on their highcard values (ignoring rank). if * the highcards are a draw, compare the next set of highcards, and so forth. */ int compare_highcards (hand_t h1, hand_t h2) { sort_hand(h1); sort_hand(h2); if ( h1[4].value > h2[4].value ) { return 0; } else if ( h1[4].value < h2[4].value ) { return 1; } else if ( h1[3].value > h2[3].value ) { return 0; } else if ( h1[3].value < h2[3].value ) { return 1; } else if ( h1[2].value > h2[2].value ) { return 0; } else if ( h1[2].value < h2[2].value ) { return 1; } else if ( h1[1].value > h2[1].value ) { return 0; } else if ( h1[1].value < h2[1].value ) { return 1; } if ( h1[0].value > h2[0].value ) { return 0; } else if ( h1[0].value < h2[0].value ) { return 1; } }
// Ex. 7 // Evaluates hand int evaluate_hand(card* hand) { sort_hand(hand); int straight_flag = is_straight(hand); if(is_same_suit(hand)) { if((hand[0].value == 8) && (straight_flag)) return 9; // If the 1st card is Ten and the cards form a straight, return 9 (Royal flush) if(straight_flag) return 8; // If the cards form a straight, return 8 (Straight flush) else return 5; // Otherwise, return 5 (Flush) } else if(is_four_of_a_kind(hand)) return 7; else if(is_full_house(hand)) return 6; else if(straight_flag) return 4; else if(is_three_of_a_kind(hand)) return 3; else if(is_two_pairs(hand)) return 2; else if(has_pair(hand)) return 1; return 0; // If none of these evaluations holds, return 0 (Bust) }
int is_fullhouse (hand_t hand) { sort_hand(hand); int is_fullhouse = 0; if( (hand[2].value == hand[3].value) && (hand[3].value == hand[4].value) ) { if (hand[0].value == hand[1].value) { is_fullhouse = 1; } } else if ( (hand[0].value == hand[1].value) && (hand[1].value == hand[2].value)) { if (hand[3].value == hand[4].value) { is_fullhouse = 1; } } return is_fullhouse; }
/* compares the hands based on rank -- if the ranks (and rank values) are * identical, compares the hands based on their highcards. * returns 0 if h1 > h2, 1 if h2 > h1. */ int compare_hands (hand_t h1, hand_t h2) { int h2Rank = 0; int h1Rank = 0; /**Finds rank of first hand **/ if (is_onepair(h1) == 1) { h1Rank = 0; } if (is_twopairs(h1) == 1) { h1Rank = 1; } if (is_threeofakind(h1) == 1) { h1Rank = 2; } if (is_straight(h1) == 1) { h1Rank = 3; } if (is_flush(h1) == 1) { h1Rank = 4; } if (is_fullhouse(h1) == 1) { h1Rank = 5; } if (is_fourofakind(h1) == 1) { h1Rank = 6; } if (is_straightflush(h1) == 1) { h1Rank = 7; } if (is_royalflush(h1) == 1) { h1Rank = 8; } if (!is_onepair(h1) && !is_twopairs(h1) && !is_threeofakind(h1) && !is_straight(h1) && !is_flush(h1) && !is_fullhouse(h1) && !is_fourofakind(h1) && !is_straightflush(h1) && !is_royalflush(h1)) { h1Rank = -1; } /////////////////////////////////////////////// if (is_onepair(h2)) { if (h1Rank == 0) { int i; sort_hand(h1); sort_hand(h2); int card1 = h1[0].value, card2 = h2[0].value; for (i = 1; i<5; i++) { if (card1 == h1[i].value) break; else card1 = h1[i].value; } for (i = 1; i < 5; i++) { if (card2 == h2[i].value) break; else card2 = h2[i].value; } return (card2 > card1); } else h2Rank = 0; } if (is_twopairs(h2)) { if (h1Rank == 1) { sort_hand(h1); sort_hand(h2); int i; int k; int h1Pair1, h1Pair2, h2Pair1, h2Pair2; int highestH1, highestH2; for (i = 0; i < 4; i++) { if (h1[i].value == h1[i + 1].value) { h1Pair1 = h1[i].value; k = i + 2; break; } } for (i = k; i < 4; i++) { if (h1[i].value == h1[i + 1].value) { h1Pair2 = h1[i].value; break; } } for (i = 0; i < 4; i++) { if (h2[i].value == h2[i + 1].value) { h2Pair1 = h2[i].value; k = i + 2; break; } } for (i = k; i < 4; i++) { if (h2[i].value == h2[i + 1].value) { h2Pair2 = h2[i].value; break; } } if (h1Pair1 > h1Pair2) { highestH1 = h1Pair1; } else if (h1Pair2 > h1Pair1) { highestH1 = h1Pair2; } if (h2Pair1 > h2Pair2) { highestH2 = h1Pair1; } else if (h2Pair2 > h2Pair1) { highestH2 = h1Pair2; } if (highestH1 > highestH2) { return 0; } else if (highestH2 > highestH1) { return 1; } } else h2Rank = 1; } if (is_threeofakind(h2)) { if (h1Rank == 2) { int card1 = h1[0].value, card2 = h2[0].value; int i; for (i = 1; i < 5; i++) { if (card2 == h1[i].value) break; else card1 = h1[i].value; } for (i = 1; i < 5; i++) { if (card2 == h2[i].value) break; else card1 = h2[i].value; } return (card2 > card1); } else h2Rank = 2; } if (is_fullhouse(h2)) { if (h1Rank == 5) { int h2ThreeKindValue, h1ThreeKindValue; if ( (h1[0].value == h1[1].value) && (h1[1].value == h1[2].value) ) { h1ThreeKindValue = h1[0].value; } else h1ThreeKindValue = h1[1].value; if ( (h2[0].value == h2[1].value) && (h2[1].value == h2[2].value) ) { h2ThreeKindValue = h2[0].value; } else h2ThreeKindValue = h2[1].value; return (h2ThreeKindValue > h1ThreeKindValue); } else h2Rank = 5; } if (is_straight(h2) && !is_straightflush(h2)) { if (h1Rank == 3) { return compare_highcards(h1, h2); } else h2Rank = 3; } if (is_flush(h2)) { if (h1Rank == 4) { return compare_highcards(h1, h2); } else h2Rank = 4; } if (is_fourofakind(h2)) { if (h1Rank == 6) { int card1 = h1[0].value, card2 = h2[0].value; int i; for (i = 1; i<5; i++) { if (card1 == h1[i].value) break; else card1 = h1[i].value; } for (i = 1; i < 5; i++) { if (card2 == h2[i].value) break; else card2 = h2[i].value; } return (card2 > card1); } else h2Rank = 6; } if (is_straightflush(h2)) { if (h1Rank == 7) { return compare_highcards(h1, h2); } else h2Rank = 7; } if (is_royalflush(h2)) { h2Rank = 8; } if (!is_onepair(h2) && !is_twopairs(h2) && !is_threeofakind(h2) && !is_straight(h2) && !is_flush(h2) && !is_fullhouse(h2) && !is_fourofakind(h2) && !is_straightflush(h2) && !is_royalflush(h2)) { h2Rank = -1; } /**Compares hands **/ if (h1Rank > h2Rank) { return 0; } else if (h2Rank > h1Rank) { return 1; } else if (h2Rank == h1Rank) { return compare_highcards(h1, h2); } }
int main (void) { int game = 0, money = 1000; system("Color 24");//Color: Background: Green, Text: Red welcome_screen(); do{//while player1 has money /* initialize suit array */ const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; const char *suits[4] = {"\3", "\4", "\5", "\6"}; /* initialize face array */ const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; char faces[13] = {"A234567890JQK"}; char raise = '\0' , ten = ' '; Card hand1[5]; Card hand2[5]; /* initalize deck array */ int deck[4][13] = {0}; int is_pair = 0, is_two_pair = 0, is_three = 0, is_four = 0, is_full = 0, is_flush = 0, is_straight = 0, score1 = 0, score2 = 0, x = 0, y = 0, i = 0, j = 0, count = 10, count1 = 0, count2 = 0, bet = 0, bet2 = 0; srand ((unsigned) time (NULL)); /* see random-number generator */ hand1[0].is_active = 1; hand1[1].is_active = 1; hand1[2].is_active = 1; hand1[3].is_active = 1; hand1[4].is_active = 1; hand2[0].is_active = 1; hand2[1].is_active = 1; hand2[2].is_active = 1; hand2[3].is_active = 1; hand2[4].is_active = 1; //0 = Black 8 = Gray //1 = Blue 9 = Light Blue //2 = Green a = Light Green //3 = Aqua b = Light Aqua //4 = Red c = Light Red //5 = Purple d = Light Purple //6 = Yellow e = Light Yellow //7 = White f = Bright White do{//Places blind bet printf("Money: $%d\n", money); printf("Place your blind: \n"); scanf("%d", &bet); if(bet > money) { printf("You don't have that much money!\n"); system("Pause"); system("cls"); } }while(bet > money); shuffle (deck); deal(deck, face, faces, suit, suits, hand1, hand2, count, count1, count2); //Initial Deal printf("\nMoney: $%d", money - bet); bet = raise_bet(money, bet);//First round of betting - Raise before 'draw' count = select_cards(hand1, count);//how many cards to redeal count1 = count - 10; //deal(deck, face, suit, hand1, hand2, count); //Simulates Dealer, decides which cards the dealer keeps sort_hand(hand2); is_pair = pair(hand2, face, 0); is_two_pair = two_pair(hand2, face, 0); is_three = three_of_a_kind(hand2, face, 0); is_four = four_of_a_kind(hand2, face, 0); is_full = full_house(hand2, face, 0); is_flush = flush(hand2, face, 0); is_straight = straight(hand2, face, 0); for(i = 0; i < 5; i++)//Counts how many cards to redeal to dealer { if(hand2[i].is_active == 1) { count++; count2++; } } //printf("\n%d %d %d %d %d\n", hand1[0].is_active, hand1[1].is_active, hand1[2].is_active, hand1[3].is_active, hand1[4].is_active); //printf("\n%d %d %d %d %d\n", hand2[0].is_active, hand2[1].is_active, hand2[2].is_active, hand2[3].is_active, hand2[4].is_active); system("Pause"); system("cls"); printf("Money: $%d\n", money - bet); printf("Pot: $%d", 2*bet); deal(deck, face, faces, suit, suits, hand1, hand2, count, count1, count2); //Draw and Redeal sort_hand(hand1); sort_hand(hand2); printf("\nHand#1\n"); for(y = 0; y < 5; y++)//Prints hand1 { if(hand1[y].face == 9)//Prints '10' instead of just '0' { ten = '1'; } if(hand1[y].face != 9) { ten = ' '; } printf("Card#%d %5s of %-8s %c%c%s\n", y + 1, face[hand1[y].face], suit[hand1[y].suit], ten, faces[hand1[y].face], suits[hand1[y].suit]); } //money = money - bet; bet = raise_bet(money, bet);//Second round of betting - before 'showdown' money = money - bet; printf("Money: $%d\n", money); /*SHOWDOWN!!!*/ printf("Pot: $%d\n", 2*bet); system("Pause"); system("cls"); printf("\nHand#1\n"); for(y = 0; y < 5; y++) { if(hand1[y].face == 9) { ten = '1'; } if(hand1[y].face != 9) { ten = ' '; } printf("Card#%d %5s of %-8s %c%c%s\n", y + 1, face[hand1[y].face], suit[hand1[y].suit], ten, faces[hand1[y].face], suits[hand1[y].suit]); } printf("\nHand#2\n"); for(j = 0; j < 5; j++) { if(hand2[j].face == 9) { ten = '1'; } if(hand2[j].face != 9) { ten = ' '; } printf("Card#%d %5s of %-8s %c%c%s\n", j + 1, face[hand2[j].face], suit[hand2[j].suit], ten, faces[hand2[j].face], suits[hand2[j].suit]); } hand1[0].high_card = hand1[4].face;//Starts by assuming 'high card' is hand[4] hand2[0].high_card = hand2[4].face;//'high card' is changed if there is a pair, two pair, etc. printf("\nPlayer #1\t"); //Evaluates Player1's hand is_pair = pair(hand1, face, 1); is_two_pair = two_pair(hand1, face, 1); is_three = three_of_a_kind(hand1, face, 1); is_four = four_of_a_kind(hand1, face, 1); is_full = full_house(hand1, face, 1); is_flush = flush(hand1, face, 1); is_straight = straight(hand1, face, 1); //printf("\nHand #1:\nPair: %d\t\tTwo Pair: %d\tThree: %d\tFour: %d\t\tFlush: %d\tStraight: %d\n", is_pair, is_two_pair, is_three, is_four, is_flush, is_straight); score1 = calc_score(is_pair, is_two_pair, is_three, is_four, is_flush, is_straight, face, hand1); printf("\nPlayer#2\t");//Evaluates Dealer's hand is_pair = pair(hand2, face, 1); is_two_pair = two_pair(hand2, face, 1); is_three = three_of_a_kind(hand2, face, 1); is_four = four_of_a_kind(hand2, face, 1); is_full = full_house(hand2, face, 1); is_flush = flush(hand2, face, 1); is_straight = straight(hand2, face, 1); //printf("\nHand #2:\nPair: %d\t\tTwo Pair: %d\tThree: %d\tFour: %d\t\tFlush: %d\tStraight: %d\n", is_pair, is_two_pair, is_three, is_four, is_flush, is_straight); score2 = calc_score(is_pair, is_two_pair, is_three, is_four, is_flush, is_straight, face, hand2); if(score1 == score2)//If both hands have pair, two pair, etc. { score1 = hand1[0].high_card;//Note: If a hand has a pair, the highcard is the face value of the pair score2 = hand2[0].high_card; } x = 4; do{ if(score1 == score2)//If both hands have the same high card { score1 = hand1[x].face; score2 = hand2[x].face; if(score1 != score2) { printf("\nPlayer #1 New High Card: %5s\n", face[hand1[x].face]); printf("Player #2 New High Card: %5s\n", face[hand2[x].face]); } } x--; }while((x >= 0)&&(score1 == score2)); //printf("\n\nPlayer1 Score: %d\tPlayer2 Score: %d", score1, score2); if(score1 > score2) { printf("\n\nPlayer1 Wins: $%d\n", 2*bet); money = money + 2*bet; } if(score1 < score2) { printf("\nPlayer2 Wins: $%d\n", 2*bet); //money = money - 2*bet; } //printf("Money: $%d\n", money); system("Pause"); system("cls"); }while(money > 0); if(money <= 0) { printf("Game Over. . .\n"); } return 0; }