Ejemplo n.º 1
0
void split_pot() {
  if (ph == NULL) { printf("\nHand standing not determined yet!"); return; }
  winner_count = 1;
  int i = 0, j;
  winners = malloc(dealer->number_players*sizeof(int));

  while (!dealer->players[i].active) i++;
  winners[0] = i;

  for (i=winners[0]+1; i<dealer->number_players && dealer->players[i].active; i++) {
    int flag = 0;
    if (ph[i].type > ph[winners[0]].type) {
      winners[0] = i;
      winner_count = winner_count > 1 ? 1 : winner_count;
    }
    else if (ph[i].type == ph[winners[0]].type && comp_value(ph[i].value, ph[winners[0]].value) == 1) {
        winners[0] = i;
        winner_count = winner_count > 1 ? 1 : winner_count;
      }
    else if (ph[i].type == ph[winners[0]].type && ph[i].value == ph[winners[0]].value
      && comp_value(ph[i].value_secondary, ph[winners[0]].value_secondary) == 1) {

        winners[0] = i;
        winner_count = winner_count > 1 ? 1 : winner_count;
      }
    else if (ph[i].type == ph[winners[0]].type && ph[i].value == ph[winners[0]].value &&
      ph[i].value_secondary == ph[winners[0]].value_secondary) {

      for (j=0; j<5 && flag == 0; j++) {
        if (comp_value(ph[i].hand[j].value, ph[winners[0]].hand[j].value) == 1) {
          winners[0] = i; flag = 1;
          winner_count = winner_count > 1 ? 1 : winner_count;
        }
      }
      if (flag == 0 && ph[i].type == ph[winners[0]].type) {
          if (comp_value(ph[i].kicker.value, ph[winners[0]].kicker.value) == 1) {
            winners[0] = i; flag = 1;
            winner_count = winner_count > 1 ? 1 : winner_count;
          }
      }
    } else if (ph[i].type == ph[winners[0]].type && ph[i].value == ph[winners[0]].value &&
      ph[i].value_secondary == ph[winners[0]].value_secondary && flag == 0) {
      winners[winner_count] = i;
      winner_count++;
    }
  }

  printf("\nCongrats on the win ");
  for (j=0; j<winner_count; j++) {
    printf("Player %d, ", winners[j]);
    dealer->players[winners[j]].total_money += dealer->pot/winner_count;
  }
  printf("You win pot of size %d.\n", dealer->pot/winner_count);
  dealer->pot = 0;
}
Ejemplo n.º 2
0
/* Based on Gecko NS_LooseHexToRGB */
static int loose_hex_to_rgb(const WCHAR *hex)
{
    int len, dpc;

    len = strlenW(hex);
    if(*hex == '#') {
        hex++;
        len--;
    }
    if(len <= 3)
        return 0;

    dpc = min(len/3 + (len%3 ? 1 : 0), 4);
    return (comp_value(hex, dpc) << 16)
        | (comp_value(hex+dpc, dpc) << 8)
        | comp_value(hex+2*dpc, dpc);
}
Ejemplo n.º 3
0
void rank_poker_hands() {
  int i, j, k, m, l; ph = malloc(sizeof(PokerHand)*dealer->number_players);

  Card null_card;
  null_card.value = -1;
  null_card.suite = -1;

  printf("\n");
  for (i=0; i<dealer->number_players; i++) {
    printf("\nPlayer %d: %s and %s", i,
      card_name(dealer->players[i].hand[0]), card_name(dealer->players[i].hand[1]));
  }
  printf("\n\nCards on table %s, %s, %s, %s and %s\n",
    card_name(dealer->cards_on_table[0]), card_name(dealer->cards_on_table[1]),
    card_name(dealer->cards_on_table[2]), card_name(dealer->cards_on_table[3]),
    card_name(dealer->cards_on_table[4]) );

  for (i=0; i<dealer->number_players && dealer->players[i].active; i++) {
    ph[i].type = UNDETERMINED;
    ph[i].value = -1;
    ph[i].value_secondary = -1;
    for (j=0; j<5; j++) ph[i].hand[j] = null_card;
    ph[i].kicker = null_card;
    int num_filled = 0;


    /* Create hash maps */
    int suite_count[4]; Card by_suite[4][7];
    for (j=0; j<4; j++) suite_count[j]=0;
    int value_count[13]; Card by_value[13][4]; // For value count, 0-12 represents Two to Ace
    for (j=0; j<13; j++) value_count[j]=0;
    by_suite[dealer->players[i].hand[0].suite][suite_count[dealer->players[i].hand[0].suite]] = dealer->players[i].hand[0];
    suite_count[dealer->players[i].hand[0].suite]++;
    by_suite[dealer->players[i].hand[1].suite][suite_count[dealer->players[i].hand[1].suite]] = dealer->players[i].hand[1];
    suite_count[dealer->players[i].hand[1].suite]++;
    for (j=0; j<dealer->number_cards_on_table; j++) {
      by_suite[dealer->cards_on_table[j].suite][suite_count[dealer->cards_on_table[j].suite]] = dealer->cards_on_table[j];
      suite_count[dealer->cards_on_table[j].suite]++;
    }

    int val = dealer->players[i].hand[0].value == 0 ? 12 : dealer->players[i].hand[0].value - 1;
    by_value[val][value_count[val]] = dealer->players[i].hand[0];
    value_count[val]++;
    val = dealer->players[i].hand[1].value == 0 ? 12 : dealer->players[i].hand[1].value - 1;
    by_value[val][value_count[val]] = dealer->players[i].hand[1];
    value_count[val]++;
    for (j=0; j<dealer->number_cards_on_table; j++) {
      val = dealer->cards_on_table[j].value == 0 ? 12 : dealer->cards_on_table[j].value - 1;
      by_value[val][value_count[val]] = dealer->cards_on_table[j];
      value_count[val]++;
    }

    /* Check for best hand */
    for (j=0; j<4; j++) {
      if (suite_count[j] >= 5) {
        /* Flush */
        if (ph[i].type < FLUSH) {
          ph[i].type = FLUSH;
          ph[i].hand[0] = by_suite[j][0];
          ph[i].hand[1] = by_suite[j][1];
          ph[i].hand[2] = by_suite[j][2];
          ph[i].hand[3] = by_suite[j][3];
          ph[i].hand[4] = by_suite[j][4];

          ph[i].value = ph[i].hand[0].value;
          for (k=0; k<5; k++) {
            if (comp_value(ph[i].hand[k].value, ph[i].value) == 1) ph[i].value = ph[i].hand[k].value;
          }
          num_filled = 5;
        }
      }
    }

    /* Straight */
    for (j=12; j>=4; j--) {
      if (j == 4 && value_count[12]>=1 && value_count[j]>=1 && value_count[j-1]>=1 && value_count[j-2]>=1
        && value_count[j-3]>=1) {
          ph[i].type = STRAIGHT;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          ph[i].hand[1] = by_value[j-1][0];
          ph[i].hand[2] = by_value[j-2][0];
          ph[i].hand[3] = by_value[j-3][0];
          ph[i].hand[4] = by_value[12][0];
          num_filled = 5;
          break;
      } else if (value_count[j]>=1 && value_count[j-1]>=1 && value_count[j-2]>=1
        && value_count[j-3]>=1 && value_count[j-4]>=1) {
          ph[i].type = STRAIGHT;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          ph[i].hand[1] = by_value[j-1][0];
          ph[i].hand[2] = by_value[j-2][0];
          ph[i].hand[3] = by_value[j-3][0];
          ph[i].hand[4] = by_value[j-4][0];
          num_filled = 5;
          break;
      }
    }

    /* 4 of a kind */
    for (j=12; j>=0; j--) {
      if (value_count[j] == 4 && ph[i].type < FOUR_OF_A_KIND) {
          ph[i].type = FOUR_OF_A_KIND;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          ph[i].hand[1] = by_value[j][1];
          ph[i].hand[2] = by_value[j][2];
          ph[i].hand[3] = by_value[j][3];
          num_filled = 4;
          break;
      }
    }


    /* Full house and 3 of a kind */
    for (j=12; j>=0; j--) {
      if (value_count[j] == 3) {
        /* Check for 3 of a kind */
        for (k=12; k>=0 && k!=j; k--) {
          if (value_count[k] == 2
              && ph[i].type < FULL_HOUSE) {
            ph[i].type = FULL_HOUSE;
            ph[i].value = j;
            ph[i].value_secondary = k;
            ph[i].hand[0] = by_value[j][0];
            ph[i].hand[1] = by_value[j][1];
            ph[i].hand[2] = by_value[j][2];
            ph[i].hand[3] = by_value[k][0];
            ph[i].hand[4] = by_value[k][1];
            num_filled = 5;
            break;
          }
        }

        /* If not full house, is a 3 of a kind */
        if (ph[i].type < THREE_OF_A_KIND || (ph[i].type == THREE_OF_A_KIND && comp_value(ph[i].value, j) == -1)) {
          ph[i].type = THREE_OF_A_KIND;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          ph[i].hand[1] = by_value[j][1];
          ph[i].hand[2] = by_value[j][2];
          num_filled = 3;
        }
      }
    }

    for (j=12; j>=0; j--) {
      if (value_count[j] == 2) {
        /* Check for two pair */
        for (k=12; k>=0 && k!=j; k--) {
          if (value_count[k] == 2 && ph[i].type < TWO_PAIR) {
            ph[i].type = TWO_PAIR;
            ph[i].value = j;
            ph[i].value_secondary = k;
            ph[i].hand[0] = by_value[j][0];
            ph[i].hand[1] = by_value[j][1];
            ph[i].hand[2] = by_value[k][0];
            ph[i].hand[3] = by_value[k][1];
            num_filled = 4;
            break;
          }
        }

        /* If not 2 pair, is a single pair */
        if (ph[i].type < PAIR) {
          ph[i].type = PAIR;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          ph[i].hand[1] = by_value[j][1];
          num_filled = 2;
          break;
        }
      }
    }

    for (j=12; j>=0; j--) {
      if (value_count[j] >= 1 && ph[i].type < HIGH_CARD ) {
          ph[i].type = HIGH_CARD;
          ph[i].value = j;
          ph[i].hand[0] = by_value[j][0];
          num_filled = 1;
          break;
      }
    }

    /* Fill up remaining spots in best hand */
    for (j=12; j>=0 && num_filled<5; j--) {
      for (k=0; k<value_count[j] && num_filled<5; k++) {
        int flag = 0;
        for (l=0; l<num_filled; l++) {
          if (by_value[j][k].value == ph[i].hand[l].value && by_value[j][k].suite == ph[i].hand[l].suite) flag = 1;
        }
        if (flag == 0) {
          ph[i].hand[num_filled] = by_value[j][k];
          num_filled++;
        }
      }
    }

    /* Kicker */
    for (j=12; j>=0; j--) {
      for (k=0; k<value_count[j] && ph[i].kicker.value == -1; k++) {
        int flag = 0;
        for (l=0; l<num_filled; l++) {
          if (by_value[j][k].value == ph[i].hand[l].value && by_value[j][k].suite == ph[i].hand[l].suite) flag = 1;
        }
        if (flag == 0) {
          ph[i].kicker = by_value[j][k];
        }
      }
    }
  }


  for (i=0; i<dealer->number_players && dealer->players[i].active; i++) {
    char *s;
    switch (ph[i].type) {
        case ROYAL_FLUSH: s = "Royal Flush";
          break;
        case STRAIGHT_FLUSH: s = "Straight Flush";
          break;
        case FOUR_OF_A_KIND: s = "Four of a Kind";
          break;
        case FULL_HOUSE: s = "Full House";
          break;
        case FLUSH: s = "Flush";
          break;
        case STRAIGHT: s = "Straight";
          break;
        case THREE_OF_A_KIND: s = "Three of a Kind";
          break;
        case TWO_PAIR: s = "Two Pair";
          break;
        case PAIR: s = "Pair";
          break;
        case HIGH_CARD: s = "High Card";
          break;
        case UNDETERMINED: printf("\nError: Player hand should not be UNDETERMINED"); break;
    }
    printf("\nPlayer %d: %s with %s, %s, %s, %s, %s and kicker %s", i, s, card_name(ph[i].hand[0]), card_name(ph[i].hand[1]),
      card_name(ph[i].hand[2]), card_name(ph[i].hand[3]), card_name(ph[i].hand[4]), card_name(ph[i].kicker));
  }
  printf("\n");
}