#include #include #include typedef struct { char four,highest,pairs[2],three; int flush,full_house,royal_flush; int straight,straight_flush; } score_struct; int cardscmp(const void *card1,const void *card2); int cardsval(char *card); void dbghand(char cards_player1[5][3],char cards_player2[5][3]); int handwinner(char cards_player1[5][3],char cards_player2[5][3]); score_struct score(char player_cards[5][3]); void possibly_prevent_window_closing(); int main() { int card,counter=0; char cards_player1[5][3],cards_player2[5][3],*char_pointer,hand[32]; FILE *file_pointer=fopen("poker.txt","r"); if(file_pointer==NULL) { printf("Error opening file 'poker.txt'\n"); possibly_prevent_window_closing(); return 1; } while((*hand=fgetc(file_pointer))!=EOF) { char_pointer=hand; /* Here will be read 32 characters, including * * \r\n (poker.txt is encoded in dos). */ fgets(hand+1,32,file_pointer); for(card=0;card<5;card++,char_pointer++) { cards_player1[card][0]=*(char_pointer++); cards_player1[card][1]=*(char_pointer++); cards_player1[card][2]='\0'; } for(card=0;card<5;card++,char_pointer++) { cards_player2[card][0]=*(char_pointer++); cards_player2[card][1]=*(char_pointer++); cards_player2[card][2]='\0'; } if(handwinner(cards_player1,cards_player2)==1) counter++; } printf("\n\t%i\n\n",counter); possibly_prevent_window_closing(); return 0; } int cardscmp(const void *card1,const void *card2) { int card1_val=cardsval((char *)card1),card2_val=cardsval((char *)card2); return card1_val > card2_val ? 1 : card1_val == card2_val ? 0 : -1 ; } int cardsval(char *card) { switch(*card) { case 'T':return 10; case 'J':return 11; case 'Q':return 12; case 'K':return 13; case 'A':return 14; default: return *card-48; } } void dbghand(char cards_player1_[5][3],char cards_player2_[5][3]) { char cards_player1[5][3],cards_player2[5][3]; int card=0,print_straight=0; score_struct score_player1,score_player2; for(;card<5;card++) { strcpy(cards_player1[card],cards_player1_[card]); strcpy(cards_player2[card],cards_player2_[card]); } printf("\n"); printf(" Player 1 | Player 2\n"); printf(" *********|*********\n"); printf("Cards: |\n"); printf("****** |\n"); qsort(cards_player1,5,3*sizeof(char),cardscmp); qsort(cards_player2,5,3*sizeof(char),cardscmp); for(card=0;card<5;card++) printf("\t %s | %s\n",cards_player1[card],cards_player2[card]); printf("Scores: |\n"); printf("******* |\n"); score_player1=score(cards_player1); score_player2=score(cards_player2); printf("highest: %c | %c\n",score_player1.highest,score_player2.highest); if(score_player1.pairs[0]!=' ' || score_player2.pairs[0]!=' ') printf("pairs: \t%c | %c\n",score_player1.pairs[0],score_player2.pairs[0]); if(score_player1.pairs[1]!=' ' || score_player2.pairs[1]!=' ') printf("pairs: \t%c | %c\n",score_player1.pairs[1],score_player2.pairs[1]); if(score_player1.three!=' ' || score_player2.three!=' ') printf("three: \t%c | %c\n",score_player1.three,score_player2.three); if(score_player1.straight==0 && score_player1.straight_flush==0 && score_player1.royal_flush==0) { for(card=0;card<5;card++) strcpy(cards_player1[card]," "); } else print_straight=1; if(score_player2.straight==0 && score_player2.straight_flush==0 && score_player2.royal_flush==0) { for(card=0;card<5;card++) strcpy(cards_player2[card]," "); } else print_straight=1; if(print_straight==1) { if(score_player1.royal_flush!=0 || score_player2.royal_flush!=0) printf("royal_flush: "); else { printf("straight"); if(score_player1.straight_flush!=0 || score_player2.straight_flush!=0) printf("_flush:"); else printf(": "); } printf(" |\n"); for(card=0;card<5;card++) printf("\t %s | %s\n",cards_player1[card],cards_player2[card]); } if(score_player1.flush==1 || score_player2.flush==1) printf("flush: %i | %i\n",score_player1.flush,score_player2.flush); if(score_player1.full_house==1 || score_player2.full_house==1) { printf("full_house: %i | ",score_player1.full_house); printf("%i\n",score_player2.full_house); } if(score_player1.four!=' ' || score_player2.four!=' ') printf("four: %c | %c\n",score_player1.four,score_player2.four); printf("\n"); } /* The function below returns 1, if the first * * player won the hand, 2 if the second won or * * 0 if the players have the same cards. */ int handwinner(char cards_player1[5][3],char cards_player2[5][3]) { qsort(cards_player1,5,3*sizeof(char),cardscmp); qsort(cards_player2,5,3*sizeof(char),cardscmp); score_struct score_player1=score(cards_player1),score_player2=score(cards_player2); if(score_player1.royal_flush!=score_player2.royal_flush) { if(score_player1.royal_flush==1) return 1; else return 2; } if(score_player1.straight_flush!=score_player2.straight_flush) { if(score_player1.straight_flush==1) return 1; else return 2; } if(score_player1.four!=score_player2.four) { if(cardscmp(&score_player1.four,&score_player2.four)==1) return 1; else return 2; } if(score_player1.full_house!=score_player2.full_house) { if(score_player1.full_house==1) return 1; else return 2; } if(score_player1.flush!=score_player2.flush) { if(score_player1.flush==1) return 1; else return 2; } if(score_player1.straight!=score_player2.straight) { if(score_player1.straight==1) return 1; else return 2; } if(score_player1.three!=score_player2.three) { if(cardscmp(&score_player1.three,&score_player2.three)==1) return 1; else return 2; } if(score_player1.pairs[1]!=score_player2.pairs[1]) { if(cardscmp((score_player1.pairs)+1,(score_player2.pairs)+1)==1) return 1; else return 2; } else if(score_player1.pairs[0]!=score_player2.pairs[0]) { if(cardscmp(score_player1.pairs,score_player2.pairs)==1) return 1; else return 2; } if(score_player1.highest!=score_player2.highest) { if(cardscmp(&score_player1.highest,&score_player2.highest)==1) return 1; else return 2; } else { int card=3; for(;card>0;card--) if(cards_player1[card]!=cards_player2[card]) { if(cardscmp(*(cards_player1+card),*(cards_player2+card))==1) return 1; else return 2; } } return 0; } score_struct score(char player_cards[5][3]) { int card=4,identical=1,no_straight=0,pairs=0; score_struct score={' ',player_cards[4][0],{' ',' '},' ',0,0,0,0,0}; for(;card>0;card--) { if(( !( player_cards[card][0]==player_cards[card-1][0] ? identical++,no_straight=1 : 0 ) && identical!=1 ) || card==1) { switch(identical) { case 2: { if(score.three!=' ') { score.full_house=1; score.three=' '; } else { score.pairs[pairs]=player_cards[card][0]; pairs++; } }break; case 3: { if(score.pairs[0]!=' ') { pairs=0; score.full_house=1; score.pairs[0]=' '; } else score.three=player_cards[card][0]; }break; case 4: { score.four=player_cards[card][0]; score.three=' '; } } identical=1; } } if(score.four==' ' && score.full_house==0) { while(card++<4 && player_cards[card][1]==player_cards[card-1][1]); if(card==5) score.flush=1; card=0; } if(no_straight==0) { while(card++<4 && cardsval(player_cards[card])==cardsval(player_cards[card-1])+1); if(card==5) { if(score.flush==0) score.straight=1; else { score.flush=0; if(player_cards[4][0]=='A') score.royal_flush=1; else score.straight_flush=1; } } } return score; } void possibly_prevent_window_closing() { #ifdef _WIN32 system("PAUSE"); #endif }