Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me understand how to do this. Samples and text below //card3.txt 11

ID: 3826462 • Letter: P

Question

Please help me understand how to do this. Samples and text below

//card3.txt

11
6 H 3 S 4 S 7 S 5 S
9 S T S q s k s a H
2 H 3 H 5 H 2 C 5 S
8 H 9 H T H j h q h
k h k D 2 D 3 D 4 D
5 D 6 D 6 S 6 c 5 s
q d j d q C q S 5 C
6 C 8 C 9 C T C j c
2 c k c 2 S 2 h 2 D
4 H j s a D k d a C
5 S 6 S 2 S 3 S 4 S

//Help

//Help

How to Rank a Poker Hand

1. count the frequency of each rank and each suit in the hand
2. set hand rank to nothing
3. examine the frequencies of the ranks for the following cases
a. only one frequency is greater than 1
i. f == 2 - one pair
ii. f == 3 - three of a kind
iii. f == 4 - four of a kind
b. two frequencies are greater than 1
i. f1 == 2 and f2 == 2 - two pair
ii. f1 == 3 and f2 == 2 (or vice versa) - full house
c. all frequencies are one
i. check for all five cards in a sequence - straight
4. examine the frequencies of the suits
a. one suit occurs 5 times
i. if the rank is nothing - flush
ii. if the rank is straight - straight flush

//sample

//sample

In poker, a 5 card hand is ranked according to the highest of the following combinations of cards it contains. 0. Nothing the hand has none of the following combinations 1. One pair two of the cards have the same rank 2. Two Pair two of the cards have the same rank, and two of the other cards have the same rank which is different than the rank of the first pair 3. Three of a Kind three of the cards have the same rank 4. Straight the ranks of all 5 cards are in a sequence with no gaps (e.g. 7 8 9 10 J) (Ace is higher than King) 5. Flush the suits of all 5 cards are the same 6. Full House three of the cards have the same rank, and the remaining two cards also have the same rank 7. Four of a Kind four of the cards have the same rank 8. Straight Flush the ranks of all 5 cards are in a sequence and the suits are all the same For this assignment, write a PokerHand class (PokerHand.java) hose constructor accepts an array of 5 Cards (from the previous assignment), copies the cards into the Poker Hand instance, and ranks the hand The toString ethod of d should display the and and its rank see the example below) The attached file contains a number of poker hands, one hand per line. Each hand has five cards, each card is represented by two characters, as in the previous assignment. The first line in the file is a single number which is a count of the number of hands that follow The main0 ethod of PokerHand should read the hands, create an instance of PokerHand for each hand, and display it using PokerHand.toString0. Example output. 6 H 3 S 4 S 7 S 5 S Straight

Explanation / Answer

This is a c++ code for ranking a poker hand

#include <iostream>

#include <sstream>

#include <algorithm>

#include <vector>

using namespace std;

class poker

{

public:

    poker() { face = "A23456789TJQK"; suit = "SHCD"; }

    string analyze( string h )

    {

       memset( faceCnt, 0, 13 ); memset( suitCnt, 0, 4 ); vector<string> hand;

       transform( h.begin(), h.end(), h.begin(), toupper ); istringstream i( h );

       copy( istream_iterator<string>( i ), istream_iterator<string>(), back_inserter<vector<string> >( hand ) );

       if( hand.size() != 5 ) return "invalid hand."; vector<string>::iterator it = hand.begin();

       sort( it, hand.end() ); if( hand.end() != adjacent_find( it, hand.end() ) ) return "invalid hand.";

       while( it != hand.end() )

       {

           if( ( *it ).length() != 2 ) return "invalid hand.";

           int n = face.find( ( *it ).at( 0 ) ), l = suit.find( ( *it ).at( 1 ) );

           if( n < 0 || l < 0 ) return "invalid hand.";

           faceCnt[n]++; suitCnt[l]++; it++;

       }

       cout << h << ": "; return analyzeHand();

    }

private:

    string analyzeHand()

    {

       bool p1 = false, p2 = false, t = false, f = false, fl = false, st = false;

       for( int x = 0; x < 13; x++ )

           switch( faceCnt[x] )

           {

              case 2: if( p1 ) p2 = true; else p1 = true; break;

              case 3: t = true; break;

              case 4: f = true;

           }

       for( int x = 0; x < 4; x++ )if( suitCnt[x] == 5 ){ fl = true; break; }

       if( !p1 && !p2 && !t && !f )

        {

           int s = 0;

           for( int x = 0; x < 13; x++ )

           {

              if( faceCnt[x] ) s++; else s = 0;

              if( s == 5 ) break;

           }

           st = ( s == 5 ) || ( s == 4 && faceCnt[0] && !faceCnt[1] );

       }

       if( st && fl ) return "straight-flush";

       else if( f ) return "four-of-a-kind";

       else if( p1 && t ) return "full-house";

       else if( fl ) return "flush";

       else if( st ) return "straight";

       else if( t ) return "three-of-a-kind";

       else if( p1 && p2 ) return "two-pair";

       else if( p1 ) return "one-pair";

        return "high-card";

    }

    string face, suit;

    unsigned char faceCnt[13], suitCnt[4];

};

int main( int argc, char* argv[] )

{

    poker p;

    cout << p.analyze( "2h 2d 2s ks qd" ) << endl; cout << p.analyze( "2h 5h 7d 8s 9d" ) << endl;

    cout << p.analyze( "ah 2d 3s 4s 5s" ) << endl; cout << p.analyze( "2h 3h 2d 3s 3d" ) << endl;

    cout << p.analyze( "2h 7h 2d 3s 3d" ) << endl; cout << p.analyze( "2h 7h 7d 7s 7c" ) << endl;

    cout << p.analyze( "th jh qh kh ah" ) << endl; cout << p.analyze( "4h 4c kc 5d tc" ) << endl;

    cout << p.analyze( "qc tc 7c 6c 4c" ) << endl << endl; return system( "pause" );

}