Hey guys,
I've been working on a poker hand recognition assignment and ive been playing around with it for a week getting one problem after the next. Below is my code I have so far.

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#define iteration_max 10000000

using std::cin;
using std::cout;
using std::string;
using std::endl;

int cards[4][14];
int player_cards[2][2];   
int shuffled_deck [51];
int board_cards[4];
int player_hand[5];

int card;
int i;int x;
int showing;
   
void shuffle_deck(){
   int dealt_cards[51];int equal;
   srand((unsigned)time(0));
   
   for (i=0;i<52;i++)dealt_cards[i] = -1;

   for (i=0;i<52;i++)
   {  equal = 0;
      while(equal!=1)
      {  shuffled_deck[i] = rand()%52;
         if (dealt_cards[shuffled_deck[i]]!=0)
         {  dealt_cards[shuffled_deck[i]]++;equal=1;}}}
}  

void update(int start){
   for (i=start;i<showing;i++)
   {  if(board_cards[i]<13)cards[1][board_cards[i]+1]++; 
      else if((board_cards[i])<26)cards[2][board_cards[i]%13+1]++;
      else if(board_cards[i]<39)cards[3][board_cards[i]%26+1]++;
      else cards[4][board_cards[i]%39+1]++;
         
      if((board_cards[i]%39)==0)cards[4][14]++;
      else if((board_cards[i]%26)==0)cards[3][14]++;
      else if((board_cards[i]%13)==0)cards[2][14]++; 
      else if(board_cards[i]==0)cards[1][14]++;}
} 

void deal_cards(){
   card = 0;   
   for (i=1;i<3;i++)
   {  for (x=1;x<3;x++){player_cards[x][i] = shuffled_deck[card];card++;}} 
}

void flop(){
   for (i=0;i<3;i++){board_cards[i]=shuffled_deck[card];card++;}
   showing = 3;update(0);  
}

void turn(){
   board_cards[3]=shuffled_deck[card];
   showing++;update(3);card++; 
}

void river(){
   board_cards[4]=shuffled_deck[card];
   showing++;update(4);card++; 
}

void add_subtract(int player,int NUM){
   for(i=1;i<3;i++){  
      if(player_cards[player][i]<13)cards[1][player_cards[player][i]+1]+=NUM; 
      else if((player_cards[player][i])<26)cards[2][player_cards[player][i]%13+1]+=NUM;
      else if(board_cards[i]<39)cards[3][player_cards[player][i]%26+1]+=NUM;
      else cards[4][player_cards[player][i]%39+1]+=NUM;
      
      if((player_cards[player][i]%39)==0)cards[4][14]+=NUM;
      else if((player_cards[player][i]%26)==0)cards[3][14]+=NUM;
      else if((player_cards[player][i]%13)==0)cards[2][14]+=NUM; 
      else if(player_cards[player][i]==0)cards[1][14]+=NUM;}
}

bool check_pair(int player_hand[]){
   int pair=0;int temp=0;int cardpos=5;int seccardpos=3; 
   for(i=14;i>1;i--)
   {  temp=0;
      for(x=1;x<5;x++)
      {  temp+=cards[x][i];}
      if(temp!=2)
         temp=0;
      else if(temp!=2&&player_hand[cardpos]%13<cards[x][i]%13&&seccardpos>0)
         {  for(x=1;x<5;x++)
            if(cards[x][i]!=0)player_hand[seccardpos]=cards[x][i];
          seccardpos--;}
      else if(pair=0)
      {  for(x=1;x<5;x++)
            if(cards[x][i]!=0)player_hand[cardpos]=cards[x][i];cardpos--;}
         pair=1;}
   if(pair=1)return true;
}

void find_poker_hand(int player,int player_hand[]){   
   add_subtract(player,1);
   check_pair(player_hand);
   add_subtract(player,-1);
}

void initialize_hands(){
   for (i=1;i<3;i++)
   {  for (x=1;x<3;x++)
        player_cards[x][i] = -1;}

   for (i=0;i<5;i++) board_cards[i] = -1; 
     
   showing=0; 
   
   for (i=1;i<15;i++)
   {  for (x=1;x<5;x++)
         cards[x][i]=0;}   
}

int main(){
   int poker_hand[5];
   for(i=0;i<6;i++)poker_hand[i]=-1;

   string cardletter[52] = {"AH","2H","3H","4H","5H","6H","7H","8H","9H","TH","JH",
                 "QH","KH","AD","2D","3D","4D","5D","6D","7D","8D","9D","TD",
                 "JD","QD","KD","AC","2C","3C","4C","5C","6C","7C","8C","9C",
                 "TC","JC","QC","KC","AS","2S","3S","4S","5S","6S","7S","8S",
                 "9S","TS","JS","QS","KS",};
   
   initialize_hands();

   shuffle_deck();
   deal_cards();
   flop();
   turn();
   river();
   
   find_poker_hand(1,poker_hand);
   
   cout<<cardletter[player_cards[1][1]]<<" "<<cardletter[player_cards[1][2]]<<endl;
   cout<<cardletter[player_cards[2][1]]<<" "<<cardletter[player_cards[2][2]]<<endl;
   
   for(i=0;i<5;i++)cout<<cardletter[board_cards[i]]<<endl;

   for(i=0;i<52;i++)
      cout<<cardletter[shuffled_deck[i]]<<endl;
   cin>>i;
  
   return 0;
}

I've posted before so you may have seen my code. Its different now as I am changing the way I search for my poker hand. Basically my problem is this. I run the program and the strangest thing happens. These are my results below. The first 4 cards are player hands. The next 5 is what comes on the board. and the bottom 9 is the order of the deck. As you can see it works fine right now however after runnning a couple more times....

JH 4C
9S AD
5S
9D
2H
TC
9S

JH
9S
4C
AD
5S
9D
2H
TC
9S

I begin to see this:

7D <--- does not correspond to 6D
QC TH
8S
JC
TC
2C
TD

8D <--- this.
QC
6D
TH
8S
JC
TC
2C
TD

I ran it a number of times but from the looks of it their appears to be no noticable pattern. I've got no clue as to whats going on so i was wonderin if someone could help me out here.

To add numbers to your lines, simply do this:

[code=C++] // paste your code here

[/code]

The results:

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#define iteration_max 10000000

using std::cin;
using std::cout;
using std::string;
using std::endl;

int cards[4][14];
int player_cards[2][2];   
int shuffled_deck [51];
int board_cards[4];
int player_hand[5];

int card;
int i;int x;
int showing;
   
void shuffle_deck(){
   int dealt_cards[51];int equal;
   srand((unsigned)time(0));
   
   for (i=0;i<52;i++)dealt_cards[i] = -1;

   for (i=0;i<52;i++)
   {  equal = 0;
      while(equal!=1)
      {  shuffled_deck[i] = rand()%52;
         if (dealt_cards[shuffled_deck[i]]!=0)
         {  dealt_cards[shuffled_deck[i]]++;equal=1;}}}
}  

void update(int start){
   for (i=start;i<showing;i++)
   {  if(board_cards[i]<13)cards[1][board_cards[i]+1]++; 
      else if((board_cards[i])<26)cards[2][board_cards[i]%13+1]++;
      else if(board_cards[i]<39)cards[3][board_cards[i]%26+1]++;
      else cards[4][board_cards[i]%39+1]++;
         
      if((board_cards[i]%39)==0)cards[4][14]++;
      else if((board_cards[i]%26)==0)cards[3][14]++;
      else if((board_cards[i]%13)==0)cards[2][14]++; 
      else if(board_cards[i]==0)cards[1][14]++;}
} 

void deal_cards(){
   card = 0;   
   for (i=1;i<3;i++)
   {  for (x=1;x<3;x++){player_cards[x][i] = shuffled_deck[card];card++;}} 
}

void flop(){
   for (i=0;i<3;i++){board_cards[i]=shuffled_deck[card];card++;}
   showing = 3;update(0);  
}

void turn(){
   board_cards[3]=shuffled_deck[card];
   showing++;update(3);card++; 
}

void river(){
   board_cards[4]=shuffled_deck[card];
   showing++;update(4);card++; 
}

void add_subtract(int player,int NUM){
   for(i=1;i<3;i++){  
      if(player_cards[player][i]<13)cards[1][player_cards[player][i]+1]+=NUM; 
      else if((player_cards[player][i])<26)cards[2][player_cards[player][i]%13+1]+=NUM;
      else if(board_cards[i]<39)cards[3][player_cards[player][i]%26+1]+=NUM;
      else cards[4][player_cards[player][i]%39+1]+=NUM;
      
      if((player_cards[player][i]%39)==0)cards[4][14]+=NUM;
      else if((player_cards[player][i]%26)==0)cards[3][14]+=NUM;
      else if((player_cards[player][i]%13)==0)cards[2][14]+=NUM; 
      else if(player_cards[player][i]==0)cards[1][14]+=NUM;}
}

bool check_pair(int player_hand[]){
   int pair=0;int temp=0;int cardpos=5;int seccardpos=3; 
   for(i=14;i>1;i--)
   {  temp=0;
      for(x=1;x<5;x++)
      {  temp+=cards[x][i];}
      if(temp!=2)
         temp=0;
      else if(temp!=2&&player_hand[cardpos]%13<cards[x][i]%13&&seccardpos>0)
         {  for(x=1;x<5;x++)
            if(cards[x][i]!=0)player_hand[seccardpos]=cards[x][i];
          seccardpos--;}
      else if(pair=0)
      {  for(x=1;x<5;x++)
            if(cards[x][i]!=0)player_hand[cardpos]=cards[x][i];cardpos--;}
         pair=1;}
   if(pair=1)return true;
}

void find_poker_hand(int player,int player_hand[]){   
   add_subtract(player,1);
   check_pair(player_hand);
   add_subtract(player,-1);
}

void initialize_hands(){
   for (i=1;i<3;i++)
   {  for (x=1;x<3;x++)
        player_cards[x][i] = -1;}

   for (i=0;i<5;i++) board_cards[i] = -1; 
     
   showing=0; 
   
   for (i=1;i<15;i++)
   {  for (x=1;x<5;x++)
         cards[x][i]=0;}   
}

int main(){
   int poker_hand[5];
   for(i=0;i<6;i++)poker_hand[i]=-1;

   string cardletter[52] = {"AH","2H","3H","4H","5H","6H","7H","8H","9H","TH","JH",
                 "QH","KH","AD","2D","3D","4D","5D","6D","7D","8D","9D","TD",
                 "JD","QD","KD","AC","2C","3C","4C","5C","6C","7C","8C","9C",
                 "TC","JC","QC","KC","AS","2S","3S","4S","5S","6S","7S","8S",
                 "9S","TS","JS","QS","KS",};
   
   initialize_hands();

   shuffle_deck();
   deal_cards();
   flop();
   turn();
   river();
   
   find_poker_hand(1,poker_hand);
   
   cout<<cardletter[player_cards[1][1]]<<" "<<cardletter[player_cards[1][2]]<<endl;
   cout<<cardletter[player_cards[2][1]]<<" "<<cardletter[player_cards[2][2]]<<endl;
   
   for(i=0;i<5;i++)cout<<cardletter[board_cards[i]]<<endl;

   for(i=0;i<52;i++)
      cout<<cardletter[shuffled_deck[i]]<<endl;
   cin>>i;
  
   return 0;
}

I looked over your code and figured it out a little, but it was a little hard to follow for me due to the lack of comments. However, and I have no idea whether this has anything to do with this particular problem or not, you are courting disaster with lines like 24 and 27 above:

int dealt_cards[51];int equal;                // line 24
   for (i=0;i<52;i++)dealt_cards[i] = -1;   // line 27

You are assigning a value to dealt_cards[51], which is only defined for indexes 0 through 50. I am actually surprised that the program didn't crash on me. Whether this has anything to do with the problem, again I don't know, but it couldn't hurt to go through your code and add one to a few of the array declarations, as in:

int dealt_cards[52];

instead of

int dealt_cards[51];

It's a little hard to wrap my hands around some of the code like this (line 13):

int cards[4][14];

We're used to seeing 4 suits, 13 of each suit, so it's confusing when you add 1 to one of them and not the other and don't leave a comment as to why. Again here (line 76):

else cards[4][player_cards[player][i]%39+1]+=NUM;

you are again overflowing your declared array. It's dangerous, plus makes it harder to follow for the reader. Does the array start at index 0 or index 1? It's confusing if it starts at 0 for some arrays and 1 for others. Also, and this is just personal preference, others may disagree, but I find code like this:

for (i=0;i<5;i++) 
     board_cards[i] = -1;

more readable than code like this because the different parts of the statement stand out more easily to my eye:

for (i=0;i<5;i++) board_cards[i] = -1;

Again just personal preference on that one. I would fix up the arrays and establish some index/declaration consistency, get rid of the potential segmentation faults, and try it out again. May do absolutely nothing, but it couldn't hurt.

Thanks for replying. All my double arrays are suppose to start at index 1... and i have 14 columns in my cards[4][14] because i have a position for an A at the beginning and the end so I can search for straights etc. i dunno ill have to polish up my code a bit lol. My shuffled_deck and all 1 dimensional arrays start at 0. How can I change the index or starting point so to speak to 1? Thanks a lot for your help/input.

Thanks for replying. All my double arrays are suppose to start at index 1... and i have 14 columns in my cards[4][14] because i have a position for an A at the beginning and the end so I can search for straights etc. i dunno ill have to polish up my code a bit lol. My shuffled_deck and all 1 dimensional arrays start at 0. How can I change the index or starting point so to speak to 1? Thanks a lot for your help/input.

Well, if you want to change the starting indexes of all your arrays to start at index 1, you can do that, just define your arrays to be 1 larger than they need to be. For example, if you want dealt_cards to have indexes 1 through 52, define it as:

int dealt_cards[53];

That will define it to C++ as indexes 0 through 52, and you simply won't use index 0. For the cards array, if you want those indexes to be 1 through 4 and 1 through 13, define it as:

int cards[5][14];

Again, that defines indexes 0 through 4 and indexes 0 through 13 and you simply won't use index 0. But your potential segmentation faults will be resolved and your code will be more consistent. If you want it to be indexes 1 through 14 (10 - J - Q - K - A), where the A here represents 14, the value of Ace in an Ace High Straight, you'd do this:

int cards[5][15];

but if you do that, I think you for sure need to put a comment in explaining why so people understand.

To add numbers to your lines, simply do this:

[code=C++] // paste your code here

[/code]

Not supposed to use c++ any more. use cplusplus instead of c++.

This article has been dead for over six months. Start a new discussion instead.