Thread: C Poker games

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    27

    C Poker games

    has anyone come accross any good poker games created in c which come with the code

    mainly looking for texas holdem but any kind is okay im not fussy i just want to play one and see how it is coded.
    Last edited by satory; 11-18-2004 at 08:37 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you having problems with? It shouldn't be that hard to make one. Are you trying to create the AI for one? Or do you just want one that deals out some cards to some players and they each take a turn, or what?
    Code:
    create a deck of 52 different cards
    shuffle deck
    
    for number of cards per hand
        for each player
            deal one card
    
    for each round
        for each player
            switch round
                betting round:
                    display amount to bet to stay in, if any
                    ask amount to bet
                    get bet
                card exchanging
                    ask which cards to get rid of
                    get rid of those cards
                    deal that many new cards
    
    calculate winner
    give the bets to the winner
    That looks like a fairly good start.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    i know most of that but im not too sure of the ai and them giving up the bad cards having been not very specific in the earlier post.

    also the other big bit would be is there any easy way to calculate the winner coz first you got to tell what cards the computer has then work out their best hand every time to see if its worth a big or small bet or just to fold

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    I guess a good start would be to hard code what the winning hands are. Hands that are better than others have a different value
    (im sure that doesnt make sense to anybody but me)

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about something like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    char *cardname(int num)
    {
      static char buf[10];
      char suits[] = "HCDS";
      char *cards[] = { " 2", " 3", " 4", " 5", " 6", " 7", " 8",
                        " 9", "10", " J", " Q", " K", " A"};
    
      sprintf(buf, "%s%c", cards[num%13], suits[num/13]);
      return buf;
    }
    
    char *cardval(int num)
    {
      static char *cards[] = { "2", "3", "4", "5", "6", "7", "8",
                               "9", "10", "J", "Q", "K", "A"};
    
      return cards[num%13];
    }
    
    int is_x_of_a_kind(int *vals, int x, int *val, int except)
    {
      int i, ctr;
    
      for(ctr = 0, *val = vals[0], i = 1;i < 5;++i)
      {
        if(vals[i] == except)
          continue;
    
        if(vals[i] == *val)
          ctr++;
        else
        {
          if(ctr == x-1)
            return 1;
          ctr = 0;
          *val = vals[i];
        }
      }
    
      return ctr == x-1;
    }
    
    int is_four_of_a_kind(int *vals, int *val)
    {
      return is_x_of_a_kind(vals, 4, val, -1);
    }
    
    int is_three_of_a_kind(int *vals, int *val)
    {
      return is_x_of_a_kind(vals, 3, val, -1);
    }
    
    int is_pair(int *vals, int *val)
    {
      return is_x_of_a_kind(vals, 2, val, -1);
    }
    
    int is_two_pair(int *vals, int *val1, int *val2)
    {
      if(!is_x_of_a_kind(vals, 2, val1, -1))
        return 0;
      if(!is_x_of_a_kind(vals, 2, val2, *val1))
        return 0;
    
      return 1;
    }
    
    int is_full_house(int *vals, int *val1, int *val2)
    {
      return is_three_of_a_kind(vals, val1) && is_pair(vals, val2);
    }
    
    int is_flush(int *vals, int *suits, int *val)
    {
      int i;
    
      for(i = 0;i < 4;++i)
        if(suits[i] != suits[i+1])
          return 0;
    
      *val = vals[4];
    
      return 1;
    }
    
    int is_straight(int *vals, int *val)
    {
      int i;
    
      for(i = 0;i < 4;++i)
        if(vals[i]+1 != vals[i+1])
          return 0;
    
      *val = vals[4];
    
      return 1;
    }
    
    int is_straight_flush(int *vals, int *suits, int *val)
    {
      return is_flush(vals, suits, val) && is_straight(vals, val);
    }
    
    void print_hand_rank(int *hand)
    {
      int i, j, temp;
      int vals[5], suits[5];
      int highcard1, highcard2;
    
      for(i = 0;i < 5;++i)
      {
        vals[i] = hand[i]%13;
        suits[i] = hand[i]/13;
      }
    
      for(i = 0;i < 5;++i)
        for(j = i+1;j < 5;++j)
          if(vals[i] > vals[j])
          {
            temp = vals[i];
            vals[i] = vals[j];
            vals[j] = temp;
            temp = hand[i];
            hand[i] = hand[j];
            hand[j] = temp;
            temp = suits[i];
            suits[i] = suits[j];
            suits[j] = temp;
          }
    
      if(is_straight_flush(vals, suits, &highcard1))
        printf("Straight flush - %s high\n", cardval(highcard1));
      else if(is_four_of_a_kind(vals, &highcard1))
        printf("4 of a kind - %s high\n", cardval(highcard1));
      else if(is_full_house(vals, &highcard1, &highcard2))
      {
        char buf[5];
    
        strcpy(buf, cardval(highcard1));
        printf("Full house - %s's over %s's\n", buf, cardval(highcard2));
      }
      else if(is_flush(vals, suits, &highcard1))
        printf("Flush - %s high\n", cardval(highcard1));
      else if(is_straight(vals, &highcard1))
        printf("Straight - %s high\n", cardval(highcard1));
      else if(is_three_of_a_kind(vals, &highcard1))
        printf("3 of a kind - %s high\n", cardval(highcard1));
      else if(is_two_pair(vals, &highcard1, &highcard2))
      {
        char buf[5];
    
        strcpy(buf, cardval(highcard1));
        printf("2 pair - %s's and %s's\n", buf, cardval(highcard2));
      }
      else if(is_pair(vals, &highcard1))
        printf("Pair of %s's\n", cardval(highcard1));
      else
        printf("%s high\n", cardval(vals[4]));
    }
    
    int main(void)
    {
      int deck[52], hand[5];
      int i, j, card, temp;
    
      srand(time(NULL));
    
      for(i = 0;i < 52;++i)
        deck[i] = i;
    
      for(i = 0;i < 10;++i)
        for(j = 0;j < 52;++j)
        {
          card = rand()%52;
          temp = deck[j];
          deck[j] = deck[card];
          deck[card] = temp;
        }
    
      for(i = 0;i < 5;++i)
        hand[i] = deck[i];
    
      for(i = 0;i < 5;++i)
        printf("%s ", cardname(hand[i]));
      putchar('\n');
    
      print_hand_rank(hand);
    
      return 0;
    Seems to work anyway
    Code:
    itsme@itsme:~/C$ ./poker
     5S  4D  8S  AS  5H
    Pair of 5's
    itsme@itsme:~/C$ ./poker
     6C  7S  9D  JD 10H
    J high
    itsme@itsme:~/C$ ./poker
     JH  6S  5H  6D  JC
    2 pair - 6's and J's
    If you understand what you're doing, you're not learning anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It just goes to show if you slack off long enough, someone will do everything for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by quzah
    It just goes to show if you slack off long enough, someone will do everything for you.

    Quzah.
    Just the hand calculation part. And it's not quite good enough for comparing 2 hands absolutely, but it's improvable. No decision making or anything in this code.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 05:32 PM
  2. Violent video games?
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 04-26-2006, 01:43 PM
  3. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM