/*---------------------------------------------------------- Elizabeth Sklar Assignment #5 MC140.0X due 6 November 2000 This program simluates dealing and evaluation of two hands of poker. A deck of cards is defined as a 52-element array, as follows: 0 1 2 3 4 5 6 7 8 9 10 11 12 clubs: 2 3 4 5 6 7 8 9 10 J Q K A 13 14 15 16 17 18 19 20 21 22 23 24 25 diamonds: 2 3 4 5 6 7 8 9 10 J Q K A 26 27 28 29 30 31 32 33 34 35 36 37 38 hearts: 2 3 4 5 6 7 8 9 10 J Q K A 39 40 41 42 43 44 45 46 47 48 49 50 51 spades: 2 3 4 5 6 7 8 9 10 J Q K A ----------------------------------------------------------*/ #include #include #include #include #define TRUE 1 #define FALSE 0 int deck[52]; int player1[5]; int player2[5]; /*--------------------------------------------------------*/ /* quit */ /* */ /* This function prints a message when before the program */ /* exits. */ /* */ /*--------------------------------------------------------*/ void quit() { printf( "Thanks for playing!\n" ); } /* end of quit() */ /*--------------------------------------------------------*/ /* shuffle */ /* */ /* This function virtually shuffles the deck, by setting */ /* all the "dealt" flags in the deck to FALSE. */ /* */ /*--------------------------------------------------------*/ void shuffle() { // you have to write this code } /* end of shuffle() */ /*--------------------------------------------------------*/ /* deal_one_card */ /* */ /* This function deals one card, by randomly picking a */ /* card that hasn't been dealt yet and then setting that */ /* card's "dealt" flag to TRUE. */ /* */ /*--------------------------------------------------------*/ int deal_one_card() { // you have to write this code } /* end of deal_one_card() */ /*--------------------------------------------------------*/ /* deal */ /* */ /* This function deals 5 cards to each player. */ /* */ /*--------------------------------------------------------*/ void deal() { // you have to write this code } /* end of deal() */ /*--------------------------------------------------------*/ /* find_rank */ /* */ /* this function returns a single character indicating */ /* the rank of the i-th card in the deck. */ /* */ /* return value: */ /* '2'..'9' = 2..9 */ /* '0' = 10 */ /* 'J' = jack */ /* 'Q' = queen */ /* 'K' = king */ /* 'A' = ace */ /* */ /*--------------------------------------------------------*/ char find_rank( int i ) { // you have to write this code } /* end of find_rank() */ /*--------------------------------------------------------*/ /* find_suit */ /* */ /* this function returns a single character indicating */ /* the suit of the i-th card in the deck. */ /* */ /* return value: */ /* 'C' = clubs */ /* 'D' = diamonds */ /* 'H' = hearts */ /* 'S' = spades */ /* */ /*--------------------------------------------------------*/ char find_suit( int i ) { // you have to write this code } /* end of find_suit() */ /*--------------------------------------------------------*/ /* print_card */ /* */ /* This function prints one card, using two characters, */ /* first the rank ('2'..'9','0','J','Q','K','A') and then */ /* the suit ('C','D','H','S'). */ /* For example: '2C' for the 2 of clubs */ /* */ /*--------------------------------------------------------*/ void print_card( int i ) { // you have to write this code } /* end of print_card() */ /*--------------------------------------------------------*/ /* print_hands */ /* */ /* This function prints out the contents of both players' */ /* hands. */ /* */ /*--------------------------------------------------------*/ void print_hands() { // you have to write this code } /* end of print_hands() */ /*--------------------------------------------------------*/ /* evaluate_hand */ /* */ /* This function returns a the value of the hand argument.*/ /* */ /*--------------------------------------------------------*/ float evaluate_hand( int hand[] ) { // numrank is an array that contains the number of cards // of each rank, in order from 2 to A int numrank[13]; // numsuit is an array that contains the number of cards // in each suit, in order C, D, H, S int numsuit[4]; // num2 is the number of pairs in the hand (0, 1 or 2) int num2; // if there is at least one pair in the hand, then num2x1 // is the index of the rank of the first pair // e.g. if there are a pair of 3's in the hand, then: // num2=1 and num2x1=2 int num2x1; // if there are two pair in the hand, then num2x2 is the // index of the rank of the second pair // e.g. if there is a pair of 3's and a pair of 7's, then // num2=2, num2x1=1 and num2x2=5 int num2x2; // num3 is the number of 3-of-a-kinds in the hand (0 or 1) int num3; // if there is a 3-of-a-kind in the hand, then num3x is // the index of the rank of the 3-of-a-kind // e.g. if there are a three 3's, // num3=1 and num3x=1 int num3x; // num4 is the number of 4-of-a-kinds in the hand (0 or 1) int num4; // if there is a 4-of-a-kind in the hand, then num4x is // the index of the rank of the 4-of-a-kind // e.g. if there are a four 3's, // num4=1 and num4x=1 int num4x; // sort is an array that contains the cards in sorted // order, from lowest rank to highest int sort[5]; // numstraight is the number of cards that occur straight // in a row, in the hand in sorted order int numstraight; // straightx is the index of the first (lowest) card in // the straight, if one exists int straightx; // flush is TRUE if all five cards have the same suit, // otherwise, flush is FALSE int flush; // value is the value of all the cards in the hand, in // sorted order according to rank int value; //***** insert code to initialize the number of cards in // each rank to 0 //***** insert code to initialize the number of cards in // each suit to 0 //***** insert code to count the number cards of each rank //***** insert code to count the number cards of each suit // set the values in the "sort" array k = 0; for ( i=0; i<13; i++ ) { for ( j=0; j num2x2 ) { printf( "2 pair: %c %c %c\n", find_rank( num2x1 ), find_rank( num2x2 ), find_rank( i )); return( 2000000 + num2x1*pow(13,4) + num2x2*pow(13,3) + i*pow(13,2) ); } else { printf( "2 pair: %c %c %c\n", find_rank( num2x2 ), find_rank( num2x1 ), find_rank( i )); return( 2000000 + num2x2*pow(13,4) + num2x1*pow(13,3) + i*pow(13,2) ); } } // if there is one pair, then print it and return the // value of the hand if ( num2 == 1 ) { i = 12; while ( numrank[i] != 1 ) { i--; } j = i - 1; while ( numrank[j] != 1 ) { j--; } printf( "pair: %c %c %c\n", find_rank( num2x1 ), find_rank( i ), find_rank( j )); return( 1000000 + num2x1*pow(13,4) + i*pow(13,3) + j*pow(13,2) ); } //***** insert code that counts the number of cards that // occur straight in a row, in the hand in sorted order; // include in this, code to set "straightx", which is the // index of the first (lowest) card in the straight // e.g., if the hand has 2-3-4-5-6, then // numstraight=5 and straightx=0 // e.g., if the hand has 0-J-Q-K-Q, then we have a royal // flush, and // numstraight=5 and straightx=9 // e.g., if the hand has A-2-3-4-5, then we have a // straight with ace low, and // numstraight=4 and straightx=0 and numrank[12]=1 // ***** insert code to check if there is a flush, which // means that all five cards have the same suit // if there is a straight flush, then print it and return // the value of the hand (we also check for a royal flush // here, which is a straight flush 0-J-Q-K-A) if ( numstraight == 5 ) { if ( flush ) { if ( straightx == 9 ) { printf( "royal flush\n" ); return( 9000000 ); } else { printf( "straight flush: %c\n", find_rank( straightx + 4 )); return( 8000000 + straightx*pow(13,4) ); } } else { printf( "straight: %c\n", find_rank( straightx + 4 )); return( 4000000 + straightx*pow(13,4) ); } } // if there is a straight with ace low, then print it and // return the value of the hand (we also check for a // straight flush with ace low) if (( numstraight == 4 ) && ( straightx == 0 ) && ( numrank[12] == 1 )) { if ( flush ) { printf( "straight flush: %c\n", find_rank( 4 )); return( 8000000 + 4*pow(13,4) ); } else { printf( "straight: %c\n", find_rank( 4 )); return( 4000000 + 4*pow(13,4) ); } } // when we get to here, all that's left is either a flush // or a high card hand, so the rank is computed according // to the sorted order of the cards. // here, we compute value of all 5 cards, in order value = 0; for ( j=0; j<5; j++ ) { value += sort[j] * pow( 13,j ); } // if there is a flush, then print it and return the value // of the hand if ( flush ) { printf( "flush: " ); for ( j=4; j>=0; j-- ) { printf( "%c ",find_rank( sort[j] )); } printf( "\n" ); return( 5000000 + value ); } // otherwise, there is just high card, so print that and // return the value of the hand printf( "high card: " ); for ( j=4; j>=0; j-- ) { printf( "%c ",find_rank( sort[j] )); } printf( "\n" ); return( value ); } /* end of evaluate_hand() */ /*--------------------------------------------------------*/ /* announce_winner */ /* */ /* This function announces the winner, where the argument */ /* v1 is the value of player1's hand and v2 is the value */ /* of player2's hand. */ /* */ /*--------------------------------------------------------*/ void announce_winner( float v1,float v2 ) { // you have to write this code } /* end of announce_winner() */ /*--------------------------------------------------------*/ /* main program */ /* */ /*--------------------------------------------------------*/ void main() { float v1, v2; time_t *t = NULL; srand( (unsigned int)time( t ) ); shuffle(); deal(); print_hands(); v1 = evaluate_hand( player1 ); v2 = evaluate_hand( player2 ); announce_winner( v1,v2 ); quit(); } /* end of main() */