Ace high and low in card game

This is a discussion on Ace high and low in card game within the C Programming forums, part of the General Programming Boards category; Program modified from C Programming book- into C++ Question: What would be the best way to handle ace high and ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    39

    Ace high and low in card game

    Program modified from C Programming book- into C++

    Question: What would be the best way to handle ace high and ace low straights...without brute force way of doing it ...

    I would like to just modify the code for the straight and royal flush right now- aces are low...

    I know I could make other changes but now I just trying to get this part to go...

    Any suggestions are appreciated...

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    const int NUMCARDS = 5;
    const int NUMSUITS = 4;
    const int NUMRANKS = 13;
    
    int main()
    {
      int numInRank[NUMRANKS];
      int numInSuit[NUMSUITS];
    
      bool straight, flush, four, three, royalFlush;
      int pairs;  
     
      bool cardExists[NUMRANKS][NUMSUITS];
      char ch, rankCh, suitCh;
      int rank, suit;
      bool badCard;
      int cardsRead = 0;
    
      int numConsec = 0;
     
      royalFlush = false;
      straight = false;
      flush = false;
      four = false;
      three = false;
      pairs = 0;
    
     
      for (rank = 0; rank < NUMRANKS; rank++)
      {
        numInRank[rank] = 0;
        for (suit = 0; suit < NUMSUITS; suit++)
          cardExists[rank][suit] = false;
      }
    
      for (suit = 0; suit < NUMSUITS; suit++)
        numInSuit[suit] = 0;
    
      while (cardsRead < NUMCARDS)
      {
    
        badCard = false;
    
        cout << "Enter a card: ";
    
        rankCh = getchar();
        //cin.get(rankCh);
        switch (rankCh)
         {
          case '0':           exit(0);
          case '2':           rank = 1; break;
          case '3':           rank = 2; break;
          case '4':           rank = 3; break;
          case '5':           rank = 4; break;
          case '6':           rank = 5; break;
          case '7':           rank = 6; break;
          case '8':           rank = 7; break;
          case '9':           rank = 8; break;
          case 't': case 'T': rank = 9; break;
          case 'j': case 'J': rank = 10; break;
          case 'q': case 'Q': rank = 11; break;
          case 'k': case 'K': rank = 12; break;
          case 'a': case 'A': rank = 0; break;
          default:            badCard = true;
        }
        suitCh = getchar();
        //cin.get(suitCh);
        switch (suitCh)
        {
          case 'c': case 'C': suit = 0; break;
          case 'd': case 'D': suit = 1; break;
          case 'h': case 'H': suit = 2; break;
          case 's': case 'S': suit = 3; break;
          default:            badCard = true;
        }
    
        //cin.get(ch);
        //while ((ch = cin.get())!= '\n')
          while ((ch = getchar())!= '\n')
          if (ch != ' ')
            badCard = true;
        //cin.ignore();
        if (badCard)
          cout << "Bad card; ignored." << endl;
        else if (cardExists[rank][suit])
          cout << "Duplicate card; ignored." << endl;
        else
        {
          numInRank[rank]++;
          numInSuit[suit]++;
          cardExists[rank][suit] = true;
          cardsRead++;
        }
      //cin.ignore();
      }
    
      for (suit = 0; suit < NUMSUITS; suit++)
        if (numInSuit[suit] == NUMCARDS)
          flush = true;
    
       for (rank = 0; rank < NUMRANKS; rank++)
         if (numInRank[rank] == 0)
           numConsec = 0;
         else
         {
           numConsec++;
            
           if (numConsec == NUMCARDS)
           {
              straight = true;
                if (rank == NUMRANKS && flush)
                royalFlush = true;
           }
         }
    
      for (rank = 0; rank < NUMRANKS; rank++)
      {
        if (numInRank[rank] == 4)
                four = true;
        if (numInRank[rank] == 3)
                three = true;
        if (numInRank[rank] == 2)
                pairs++;
      }
    
      if (royalFlush)    
         cout << "Royal flush\n\n";
      else if (straight && flush)      
         cout << "Straight flush\n\n";
      else if (four)        
         cout << "Four of a kind\n\n";
      else if (three && pairs == 1)  
         cout << "Full house\n\n";
      else if (flush)        
         cout << "Flush\n\n";
      else if (straight)    
        cout << "Straight\n\n";
      else if (three)        
        cout << "Three of a kind\n\n";
      else if (pairs == 2)  
        cout << "Two pairs\n\n";
      else if (pairs == 1)  
        cout << "Pair\n\n";
      else                  
        cout << "High card\n\n";
    
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just write a case for each hand.
    Code:
    if ACE && KING && QUEEN && JACK && 10
    That's fairly easy.


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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    39
    Thanks for the advice...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could also just make a table of hands, and run through it in a loop.
    Code:
    char *hands[5] =
    {
        { ACE, KING, QUEEN, JACK, TEN },
        { ACE, ACE, ACE, ACE, ANY },
        ...
    };
    But you really need a different sort of tokening system for finding hands. Because, a straight flush is any five cards in a row of the same suit. So you really need something like:
    Code:
        ...
        { S, S+1, S+2, S+3, S+4 },
        ...
    For "Card of one Suite", "Same suite, one higher, Same suit, two higher, etc..."

    And for four of a kind, you really need:
    Code:
        ...
        { F, F, F, F, ANY },
        ...
    for "any face value, the same, the same, the same, anything else..."

    I prefer to do cards as numbers zero to 51, dividing by 13 to get the suit, and mod by 13 to get the face of the card.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  2. C/C++, low or high level?
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2007, 10:43 PM
  3. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 10:46 AM
  4. high or low !
    By Beginner2002 in forum C Programming
    Replies: 3
    Last Post: 07-29-2002, 01:24 PM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21