Modifying my code...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamC
    New Member
    • Apr 2007
    • 8

    Modifying my code...

    I have a program evaluating a straight hand in poker: Here is my code:

    [code=cpp]
    cin.get(rankCh) ;
    switch (toupper(rankCh ))
    {
    case '-1': exit(0);
    case '?': rank = 0; break;
    case 'A': rank = 1; break;
    case '2': rank = 2; break;
    case '3': rank = 3; break;
    case '4': rank = 4; break;
    case '5': rank = 5; break;
    case '6': rank = 6; break;
    case '7': rank = 7; break;
    case '8': rank = 8; break;
    case '9': rank = 9; break;
    case 'T': rank = 10; break;
    case 'J': rank = 11; break;
    case 'Q': rank = 12; break;
    case 'K': rank = 13; break;
    }
    bool isStraight(int numInRank[]){
    int count = 0;
    for(int i = 1; i < 14; i++){
    if(numInRank[i] != 0){
    count++;
    if(i == 13 && numInRank[1] != 0) count++;
    }else{
    count = 0;
    }
    if(count == 5) return true;
    }
    return false;
    }
    [/code]
    I would like to modify the above code to handle A high and A low straights- I sized my array to 14 instead of 13 to hold both...
    Any suggestions...
    Last edited by sicarie; Jul 30 '07, 12:55 PM. Reason: Code tags are to the right when you start or reply to a post. Please use them.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I suggest you inmplement a flag that derternmines whether the ace is high or low. In your switch:

    [code= c]
    case 'A':
    if (flag == HIGH)
    {
    rank = 13;
    }
    else
    {
    rank =1;
    }
    break;
    [/code]

    You would do this for each case in your switch.

    Comment

    • JamC
      New Member
      • Apr 2007
      • 8

      #3
      OK

      When I think flag I think boolean...
      Not sure what to set HIGH too...then I would have to modify function...

      Here is what I fixed...

      Code:
      int numInRank[13];
      
      bool flag = true;   //flag to determine if Ace is high or low
      
      cin.get(rankCh);
          switch (toupper(rankCh))
           {         
            case '-1':            exit(0);
            case 'A':
                 if (flag == HIGH)
                   rank = 13; 
                 else
                   rank = 0;
                break;
            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':           rank = 9; break;
            case 'J':           rank = 10; break;
            case 'Q':          rank = 11; break;
            case 'K':          rank = 12; break;
      }
      
      
      bool isStraight(int numInRank[])
      {
      
         int count = 0;
      
         for (int i = 0; i < 13; i++)
         {
          if (numInRank[i] != 0)
          {
            count++;
            if(i == 13 && numInRank[1] != 0)
              count++;
          }
          else
            count = 0;
          if (count == 5)
              return true;
         }
         return false;
      }

      Comment

      • leoafro
        New Member
        • Aug 2007
        • 3

        #4
        I suggest you to initialize the array to 14.. because you have 14 elements

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by JamC
          case 'A':
          if (flag == HIGH)
          rank = 13;
          else
          rank = 0;
          break;
          Yes. Assuming HIGH is true. Personally, I would:
          [code=cpp]
          case 'A':
          if (HighFlag == true)
          rank = 13;
          else
          rank = 0;
          break;
          [/code]

          Here I get the high part in the flag name.

          Comment

          • JamC
            New Member
            • Apr 2007
            • 8

            #6
            Well almost- I created a test program- works for all straights except ace high..

            I have to use the flag modification- just not sure how to modify my- I have to set the flag now somewhere in function not sure where...

            Code:
            #include <iostream>
            #include <ctime>
            #include <cstdlib>
            #include <string>
            
            using namespace std;
            
              //const int SUITS = 4;
              //const int RANKS = 13;
              //const int N = SUITS * RANKS;
             const int NUMCARDS = 5;
            
             // const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
             // const string suit[] = {"C","D","H","S"};
            
              bool isStraight(int numInRank[]);
            
            int main()
            {
            int numInRank[13];
              int numInSuit[4];
            
              bool straight, flush, four, three, royalFlush;
              int pairs;  
              
              bool cardExists[13][4];
              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 < 13; rank++) 
              {
                numInRank[rank] = 0;
                for (suit = 0; suit < 4; suit++) 
                  cardExists[rank][suit] = false;
              }
            
              for (suit = 0; suit < 4; suit++) 
                numInSuit[suit] = 0;
            
              while (cardsRead < NUMCARDS) 
              {
            
                badCard = false;
                bool highflag = false;  //flag to determine if Ace is high or low
                cout << "Enter a card: ";
            
                //rankCh = getchar();
                cin.get(rankCh);
                switch (toupper(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': rank = 9; break;
                  case 'J': rank = 10; break;
                  case 'Q': rank = 11; break;
                  case 'K': rank = 12; break;
                  //case 'A': rank = 0; break;
            	  case 'A':
                       if (highflag == true)
                         rank = 13; 
                       else
                         rank = 0;
                      break;
                  default:            badCard = true;
                }
                //suitCh = getchar();
                cin.get(suitCh);
                switch (toupper(suitCh)) 
                {
                  case 'C': suit = 0; break;
                  case 'D': suit = 1; break;
                  case 'H': suit = 2; break;
                  case 'S': suit = 3; break;
                  default:     badCard = true;
                }
            
                while ((ch = cin.get())!= '\n')
            	//while ((ch = getchar())!= '\n')
                  if (ch != ' ') 
                    badCard = true;
                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++;
                }
              }
            
              if (isStraight(numInRank))
            	  cout << "is a straight" << endl;
              else
            	  cout << "is not a straight" << endl;
            
               return 0;
            }
            
            bool isStraight(int numInRank[])
            {
            
               int count = 0;
            
               for (int i = 0; i < 13; i++)
               {
                if (numInRank[i] != 0)
                {
                  count++;
                  if(i == 13 && numInRank[1] != 0)
                    count++;
                }
                else
                  count = 0;
                if (count == 5)
                    return true;
               }
               return false;
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              You haven't written functions yet. Your logic is all in main().

              Consider main() to have a menu.

              Make a menu choice and switch on that choice to a function. When the function returns ask for another menu choice.

              Try to get your logic partitioned into functional blocks.

              Comment

              • JamC
                New Member
                • Apr 2007
                • 8

                #8
                Well I think there is a miscommunicatio n here- I added functions but I don't want the user to choose Ace high or low- I want the function isStraight to handle it when I type in the values to test...
                I will add more functions/clean code up once I get the isStraight function to work correctly...

                Code:
                #include <iostream>
                #include <ctime>
                #include <cstdlib>
                #include <string>
                
                using namespace std;
                
                  //const int SUITS = 4;
                  //const int RANKS = 13;
                  //const int N = SUITS * RANKS;
                 const int NUMCARDS = 5;
                
                 // const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
                 // const string suit[] = {"C","D","H","S"};
                
                  bool isStraight(int numInRank[]);
                  void initialize (int numInRank[], int numInSuit[], bool cardExists[][4]);
                  void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4]);
                int main()
                {
                int numInRank[13];
                  int numInSuit[4];
                
                 // bool straight, flush, four, three, royalFlush;
                 // int pairs;  
                  
                  bool cardExists[13][4];
                 int cardsRead = 0;
                 
                  initialize (numInRank, numInSuit, cardExists);
                 checkCards(cardsRead, numInRank, numInSuit, cardExists);
                
                
                  if (isStraight(numInRank))
                	  cout << "is a straight" << endl;
                  else
                	  cout << "is not a straight" << endl;
                
                   return 0;
                }
                
                void initialize (int numInRank[], int numInSuit[], bool cardExists[][4])
                {
                  int rank, suit;
                  for (rank = 0; rank < 13; rank++) 
                  {
                    numInRank[rank] = 0;
                    for (suit = 0; suit < 4; suit++) 
                      cardExists[rank][suit] = false;
                  }
                
                  for (suit = 0; suit < 4; suit++) 
                    numInSuit[suit] = 0;
                }
                void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4])
                {
                	bool badCard;
                  
                  int numConsec = 0;
                   int rank, suit;
                    char ch, rankCh, suitCh;
                
                  while (cardsRead < NUMCARDS) 
                  {
                
                    badCard = false;
                    bool highflag = false;  //flag to determine if Ace is high or low
                    cout << "Enter a card: ";
                
                  
                    cin.get(rankCh);
                    switch (toupper(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': rank = 9; break;
                      case 'J': rank = 10; break;
                      case 'Q': rank = 11; break;
                      case 'K': rank = 12; break;
                	  case 'A':
                           if (highflag == true)
                             rank = 13; 
                           else
                             rank = 0;
                          break;
                      default:            badCard = true;
                    }
                  
                    cin.get(suitCh);
                    switch (toupper(suitCh)) 
                    {
                      case 'C': suit = 0; break;
                      case 'D': suit = 1; break;
                      case 'H': suit = 2; break;
                      case 'S': suit = 3; break;
                      default:     badCard = true;
                    }
                
                    while ((ch = cin.get())!= '\n')
                      if (ch != ' ') 
                        badCard = true;
                    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++;
                    }
                  }
                }
                
                bool isStraight(int numInRank[])
                {
                
                   int count = 0;
                
                   for (int i = 0; i < 13; i++)
                   {
                    if (numInRank[i] != 0)
                    {
                      count++;
                      if(i == 13 && numInRank[1] != 0)
                        count++;
                    }
                    else
                      count = 0;
                    if (count == 5)
                        return true;
                   }
                   return false;
                }

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  isStraight() will need to know about the flag.

                  Pass the flag in also.

                  Comment

                  Working...