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.