Skip to content

Instantly share code, notes, and snippets.

@Roytangrb
Created November 21, 2020 16:57
Show Gist options
  • Select an option

  • Save Roytangrb/aa5f053a8dc879634fdd46e7f8a4dea7 to your computer and use it in GitHub Desktop.

Select an option

Save Roytangrb/aa5f053a8dc879634fdd46e7f8a4dea7 to your computer and use it in GitHub Desktop.

Revisions

  1. Roytangrb created this gist Nov 21, 2020.
    154 changes: 154 additions & 0 deletions poker.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,154 @@
    /*
    * Card Struct
    * By RT
    * 21 Nov, 2020
    */

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    #define DECK_SIZE 52
    #define TEST_SIZE 1000000

    typedef enum suit {
    DIAMONDS,
    CLUBS,
    HEARTS,
    SPADES,
    } suit;

    typedef struct card {
    short pip; // pip value
    suit s; // suit type
    } card;

    void init_deck(card deck[]);
    void shuffle_deck(card deck[]);
    void print_deck(card deck[]);
    int has_pairs(int n, card hand[], int pair);
    int has_x_of_a_kind(int n, card hand[], int x);
    int has_full_house(int n, card hand[]);

    int main(void){
    srand(time(NULL));

    int i = 0;
    int no_pair_case = 0,
    one_pair_case = 0,
    two_pairs_case = 0,
    has_3_of_a_kind_case = 0,
    has_4_of_a_kind_case = 0,
    has_full_house_case = 0;

    while (i++ < TEST_SIZE){
    card deck[DECK_SIZE];
    init_deck(deck);
    shuffle_deck(deck);

    // deck is uniformly randomized, draw the first 7
    no_pair_case += has_pairs(7, deck, 0);
    one_pair_case += has_pairs(7, deck, 1);
    two_pairs_case += has_pairs(7, deck, 2);
    has_3_of_a_kind_case += has_x_of_a_kind(7, deck, 3);
    has_4_of_a_kind_case += has_x_of_a_kind(7, deck, 4);
    has_full_house_case += has_full_house(7, deck);
    }

    printf("Probablity of no pair in a 7 cards hand: %lf %%: \n", (100.0 * no_pair_case) / TEST_SIZE);
    printf("Probablity of one pair in a 7 cards hand: %lf %%: \n", (100.0 * one_pair_case) / TEST_SIZE);
    printf("Probablity of two pairs in a 7 cards hand: %lf %%: \n", (100.0 * two_pairs_case) / TEST_SIZE);
    printf("Probablity of 3 of a kind in a 7 cards hand: %lf %%: \n", (100.0 * has_3_of_a_kind_case) / TEST_SIZE);
    printf("Probablity of full house in a 7 cards hand: %lf %%: \n", (100.0 * has_full_house_case) / TEST_SIZE);
    printf("Probablity of 4 of a kind in a 7 cards hand: %lf %%: \n", (100.0 * has_4_of_a_kind_case) / TEST_SIZE);

    return 0;
    }

    void init_deck(card deck[]){
    int k = 0;
    for (int i = 1; i <= 13; i ++){
    for (int j = DIAMONDS; j <= SPADES; j ++){
    card c = { i , j };
    deck[k++] = c;
    }
    }
    }

    void shuffle_deck(card deck[]){
    // uniformly randomize the card deck sequence
    for (int i = 0; i < DECK_SIZE; i ++){
    int rand_index = rand() % (DECK_SIZE - i) + i; // [i, 51]

    // swap
    card temp = { deck[i].pip, deck[i].s };
    deck[i].pip = deck[rand_index].pip;
    deck[i].s = deck[rand_index].s;
    deck[rand_index].pip = temp.pip;
    deck[rand_index].s = temp.s;
    }
    }

    void print_deck(card deck[]){
    for (int i = 0; i < DECK_SIZE; i ++){
    printf("%d-%d, ", deck[i].pip, deck[i].s);
    }
    }

    int has_pairs(int n, card hand[], int pair){
    // bucket count
    int dict[14] = {0};
    for (int i = 0; i < n; i++){
    int pip = hand[i].pip;
    dict[pip]++;
    }

    int pairs_count = 0;
    for (int i = 1; i <= 13; i++){
    if (dict[i] == 2) {
    pairs_count++;
    }
    }

    return pairs_count == pair;
    }

    int has_x_of_a_kind(int n, card hand[], int x) {
    // bucket count
    int dict[14] = {0};
    for (int i = 0; i < n; i++){
    int pip = hand[i].pip;
    dict[pip]++;
    }

    for (int i = 1; i <= 13; i++){
    if (dict[i] == x) {
    return 1;
    }
    }

    return 0;
    }

    int has_full_house(int n, card hand[]) {
    // bucket count
    int dict[14] = {0};
    for (int i = 0; i < n; i++){
    int pip = hand[i].pip;
    dict[pip]++;
    }

    int three_of_a_kind_pip = 0;
    int pair_pip = 0;
    for (int i = 1; i <= 13; i++){
    if (dict[i] >= 3) {
    three_of_a_kind_pip = i;
    }

    if (dict[i] >= 2 && i != three_of_a_kind_pip) {
    pair_pip = i;
    }
    }

    return pair_pip && three_of_a_kind_pip && pair_pip != three_of_a_kind_pip;
    }