473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Modifying my code...

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

Expand|Select|Wrap|Line Numbers
  1. cin.get(rankCh);
  2.     switch (toupper(rankCh))
  3.      {
  4.       case '-1':            exit(0);
  5.       case '?':           rank = 0; break;
  6.       case 'A':          rank = 1; break;
  7.       case '2':           rank = 2; break;
  8.       case '3':           rank = 3; break;
  9.       case '4':           rank = 4; break;
  10.       case '5':           rank = 5; break;
  11.       case '6':           rank = 6; break;
  12.       case '7':           rank = 7; break;
  13.       case '8':           rank = 8; break;
  14.       case '9':           rank = 9; break;
  15.       case 'T':           rank = 10; break;
  16.       case 'J':           rank = 11; break;
  17.       case 'Q':          rank = 12; break;
  18.       case 'K':          rank = 13; break;
  19. }
  20. bool isStraight(int numInRank[]){
  21.     int count = 0;
  22.     for(int i = 1; i  < 14; i++){
  23.         if(numInRank[i] != 0){
  24.             count++;
  25.             if(i == 13 && numInRank[1] != 0) count++;
  26.         }else{
  27.             count = 0;
  28.         }
  29.         if(count == 5) return true;
  30.     }
  31.     return false;
  32. }
  33.  
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...
Jul 30 '07 #1
8 1456
weaknessforcats
9,208 Expert Mod 8TB
I suggest you inmplement a flag that derternmines whether the ace is high or low. In your switch:

Expand|Select|Wrap|Line Numbers
  1. case 'A':          
  2.      if (flag == HIGH) 
  3.      {
  4.          rank = 13;
  5.      }
  6.      else
  7.      {
  8.           rank =1;
  9.      }
  10.      break;
  11.  
You would do this for each case in your switch.
Jul 30 '07 #2
JamC
8
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...

Expand|Select|Wrap|Line Numbers
  1. int numInRank[13];
  2.  
  3. bool flag = true;   //flag to determine if Ace is high or low
  4.  
  5. cin.get(rankCh);
  6.     switch (toupper(rankCh))
  7.      {         
  8.       case '-1':            exit(0);
  9.       case 'A':
  10.            if (flag == HIGH)
  11.              rank = 13; 
  12.            else
  13.              rank = 0;
  14.           break;
  15.       case '2':           rank = 1; break;
  16.       case '3':           rank = 2; break;
  17.       case '4':           rank = 3; break;
  18.       case '5':           rank = 4; break;
  19.       case '6':           rank = 5; break;
  20.       case '7':           rank = 6; break;
  21.       case '8':           rank = 7; break;
  22.       case '9':           rank = 8; break;
  23.       case 'T':           rank = 9; break;
  24.       case 'J':           rank = 10; break;
  25.       case 'Q':          rank = 11; break;
  26.       case 'K':          rank = 12; break;
  27. }
  28.  
  29.  
  30. bool isStraight(int numInRank[])
  31. {
  32.  
  33.    int count = 0;
  34.  
  35.    for (int i = 0; i < 13; i++)
  36.    {
  37.     if (numInRank[i] != 0)
  38.     {
  39.       count++;
  40.       if(i == 13 && numInRank[1] != 0)
  41.         count++;
  42.     }
  43.     else
  44.       count = 0;
  45.     if (count == 5)
  46.         return true;
  47.    }
  48.    return false;
  49. }
Aug 2 '07 #3
I suggest you to initialize the array to 14.. because you have 14 elements
Aug 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
case 'A':
if (flag == HIGH)
rank = 13;
else
rank = 0;
break;
Yes. Assuming HIGH is true. Personally, I would:
Expand|Select|Wrap|Line Numbers
  1. case 'A':
  2.            if (HighFlag == true)
  3.              rank = 13; 
  4.            else
  5.              rank = 0;
  6.           break;
  7.  
Here I get the high part in the flag name.
Aug 2 '07 #5
JamC
8
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...

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.   //const int SUITS = 4;
  9.   //const int RANKS = 13;
  10.   //const int N = SUITS * RANKS;
  11.  const int NUMCARDS = 5;
  12.  
  13.  // const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
  14.  // const string suit[] = {"C","D","H","S"};
  15.  
  16.   bool isStraight(int numInRank[]);
  17.  
  18. int main()
  19. {
  20. int numInRank[13];
  21.   int numInSuit[4];
  22.  
  23.   bool straight, flush, four, three, royalFlush;
  24.   int pairs;  
  25.  
  26.   bool cardExists[13][4];
  27.   char ch, rankCh, suitCh;
  28.   int rank, suit;
  29.   bool badCard;
  30.   int cardsRead = 0;
  31.  
  32.   int numConsec = 0;
  33.  
  34.   royalFlush = false;
  35.   straight = false;
  36.   flush = false;
  37.   four = false;
  38.   three = false;
  39.   pairs = 0;
  40.  
  41.  
  42.   for (rank = 0; rank < 13; rank++) 
  43.   {
  44.     numInRank[rank] = 0;
  45.     for (suit = 0; suit < 4; suit++) 
  46.       cardExists[rank][suit] = false;
  47.   }
  48.  
  49.   for (suit = 0; suit < 4; suit++) 
  50.     numInSuit[suit] = 0;
  51.  
  52.   while (cardsRead < NUMCARDS) 
  53.   {
  54.  
  55.     badCard = false;
  56.     bool highflag = false;  //flag to determine if Ace is high or low
  57.     cout << "Enter a card: ";
  58.  
  59.     //rankCh = getchar();
  60.     cin.get(rankCh);
  61.     switch (toupper(rankCh)) 
  62.      {
  63.       case '0':           exit(0);
  64.       case '2':           rank = 1; break;
  65.       case '3':           rank = 2; break;
  66.       case '4':           rank = 3; break;
  67.       case '5':           rank = 4; break;
  68.       case '6':           rank = 5; break;
  69.       case '7':           rank = 6; break;
  70.       case '8':           rank = 7; break;
  71.       case '9':           rank = 8; break;
  72.       case 'T': rank = 9; break;
  73.       case 'J': rank = 10; break;
  74.       case 'Q': rank = 11; break;
  75.       case 'K': rank = 12; break;
  76.       //case 'A': rank = 0; break;
  77.       case 'A':
  78.            if (highflag == true)
  79.              rank = 13; 
  80.            else
  81.              rank = 0;
  82.           break;
  83.       default:            badCard = true;
  84.     }
  85.     //suitCh = getchar();
  86.     cin.get(suitCh);
  87.     switch (toupper(suitCh)) 
  88.     {
  89.       case 'C': suit = 0; break;
  90.       case 'D': suit = 1; break;
  91.       case 'H': suit = 2; break;
  92.       case 'S': suit = 3; break;
  93.       default:     badCard = true;
  94.     }
  95.  
  96.     while ((ch = cin.get())!= '\n')
  97.     //while ((ch = getchar())!= '\n')
  98.       if (ch != ' ') 
  99.         badCard = true;
  100.     if (badCard)
  101.       cout << "Bad card; ignored." << endl;
  102.     else if (cardExists[rank][suit])
  103.       cout << "Duplicate card; ignored." << endl;
  104.     else 
  105.     {
  106.       numInRank[rank]++;
  107.       numInSuit[suit]++;
  108.       cardExists[rank][suit] = true;
  109.       cardsRead++;
  110.     }
  111.   }
  112.  
  113.   if (isStraight(numInRank))
  114.       cout << "is a straight" << endl;
  115.   else
  116.       cout << "is not a straight" << endl;
  117.  
  118.    return 0;
  119. }
  120.  
  121. bool isStraight(int numInRank[])
  122. {
  123.  
  124.    int count = 0;
  125.  
  126.    for (int i = 0; i < 13; i++)
  127.    {
  128.     if (numInRank[i] != 0)
  129.     {
  130.       count++;
  131.       if(i == 13 && numInRank[1] != 0)
  132.         count++;
  133.     }
  134.     else
  135.       count = 0;
  136.     if (count == 5)
  137.         return true;
  138.    }
  139.    return false;
  140. }
Aug 3 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 3 '07 #7
JamC
8
Well I think there is a miscommunication 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...

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.   //const int SUITS = 4;
  9.   //const int RANKS = 13;
  10.   //const int N = SUITS * RANKS;
  11.  const int NUMCARDS = 5;
  12.  
  13.  // const string rank[] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
  14.  // const string suit[] = {"C","D","H","S"};
  15.  
  16.   bool isStraight(int numInRank[]);
  17.   void initialize (int numInRank[], int numInSuit[], bool cardExists[][4]);
  18.   void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4]);
  19. int main()
  20. {
  21. int numInRank[13];
  22.   int numInSuit[4];
  23.  
  24.  // bool straight, flush, four, three, royalFlush;
  25.  // int pairs;  
  26.  
  27.   bool cardExists[13][4];
  28.  int cardsRead = 0;
  29.  
  30.   initialize (numInRank, numInSuit, cardExists);
  31.  checkCards(cardsRead, numInRank, numInSuit, cardExists);
  32.  
  33.  
  34.   if (isStraight(numInRank))
  35.       cout << "is a straight" << endl;
  36.   else
  37.       cout << "is not a straight" << endl;
  38.  
  39.    return 0;
  40. }
  41.  
  42. void initialize (int numInRank[], int numInSuit[], bool cardExists[][4])
  43. {
  44.   int rank, suit;
  45.   for (rank = 0; rank < 13; rank++) 
  46.   {
  47.     numInRank[rank] = 0;
  48.     for (suit = 0; suit < 4; suit++) 
  49.       cardExists[rank][suit] = false;
  50.   }
  51.  
  52.   for (suit = 0; suit < 4; suit++) 
  53.     numInSuit[suit] = 0;
  54. }
  55. void checkCards(int cardsRead, int numInRank[], int numInSuit[], bool cardExists[][4])
  56. {
  57.     bool badCard;
  58.  
  59.   int numConsec = 0;
  60.    int rank, suit;
  61.     char ch, rankCh, suitCh;
  62.  
  63.   while (cardsRead < NUMCARDS) 
  64.   {
  65.  
  66.     badCard = false;
  67.     bool highflag = false;  //flag to determine if Ace is high or low
  68.     cout << "Enter a card: ";
  69.  
  70.  
  71.     cin.get(rankCh);
  72.     switch (toupper(rankCh)) 
  73.      {
  74.       case '0':           exit(0);
  75.       case '2':           rank = 1; break;
  76.       case '3':           rank = 2; break;
  77.       case '4':           rank = 3; break;
  78.       case '5':           rank = 4; break;
  79.       case '6':           rank = 5; break;
  80.       case '7':           rank = 6; break;
  81.       case '8':           rank = 7; break;
  82.       case '9':           rank = 8; break;
  83.       case 'T': rank = 9; break;
  84.       case 'J': rank = 10; break;
  85.       case 'Q': rank = 11; break;
  86.       case 'K': rank = 12; break;
  87.       case 'A':
  88.            if (highflag == true)
  89.              rank = 13; 
  90.            else
  91.              rank = 0;
  92.           break;
  93.       default:            badCard = true;
  94.     }
  95.  
  96.     cin.get(suitCh);
  97.     switch (toupper(suitCh)) 
  98.     {
  99.       case 'C': suit = 0; break;
  100.       case 'D': suit = 1; break;
  101.       case 'H': suit = 2; break;
  102.       case 'S': suit = 3; break;
  103.       default:     badCard = true;
  104.     }
  105.  
  106.     while ((ch = cin.get())!= '\n')
  107.       if (ch != ' ') 
  108.         badCard = true;
  109.     if (badCard)
  110.       cout << "Bad card; ignored." << endl;
  111.     else if (cardExists[rank][suit])
  112.       cout << "Duplicate card; ignored." << endl;
  113.     else 
  114.     {
  115.       numInRank[rank]++;
  116.       numInSuit[suit]++;
  117.       cardExists[rank][suit] = true;
  118.       cardsRead++;
  119.     }
  120.   }
  121. }
  122.  
  123. bool isStraight(int numInRank[])
  124. {
  125.  
  126.    int count = 0;
  127.  
  128.    for (int i = 0; i < 13; i++)
  129.    {
  130.     if (numInRank[i] != 0)
  131.     {
  132.       count++;
  133.       if(i == 13 && numInRank[1] != 0)
  134.         count++;
  135.     }
  136.     else
  137.       count = 0;
  138.     if (count == 5)
  139.         return true;
  140.    }
  141.    return false;
  142. }
Aug 3 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
isStraight() will need to know about the flag.

Pass the flag in also.
Aug 4 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
2
by: Yavuz Bogazci | last post by:
Hi, i am reading the LastAccessTime, LastWriteTime and DateofCreation of a file. The second and third are working well, but each time i want to read the LastAccessTime, its set to "now". How can...
13
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
12
by: AES | last post by:
Can an HTML web page dynamically modify its own code? (without resort to Java or any other non-HTML complexities) That is, is there any provision in HTML such that a single "Next" button on a...
56
by: subramanian100in | last post by:
The standard allows that we can copy strings onto arg, arg, etc. Why is it allowed ? What can be the maximum length of such a string that is copied ?
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.