Question: Write a program that will operate on a deck of 52 cards . Your program will: 1. Deal them into four random hands of 5

Write a program that will operate on a deck of 52 cards

. Your program will:

1.

Deal them into four

random

hands of 5 cards each

a.

The four hands need to be kept in an array for later use.

2.

Sort each hand so that it

shows the cards in sequence from two as the lowest

to ace as the highest.

3.

Display the cards in each hand using the card face (2, 10, King, etc.) and the

suit (Spades, Hearts, etc.)

4.

Di

s

play what you determined the hand to be. A ranking of poker hands is at

http://www.pagat.com/vying/pokerrank.html

a.

Straight flush

b.

Four of a kind

c.

Full House

d.

Flush

e.

Straight

f.

Three of a kind

g.

Two pair

h.

One pair

i.

Highest card

5.

Each time the

program is run, a different set

of hands is to be dealt.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SHOW ME HOW TO SOLVE #4 and #5

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

here is my code.

Main.cpp

#include

using namespace std;

#include "Cards.h"

void main(){

Card Deck[NumCardsInDeck];

Card Hands[4][5];

int i, j;

InitDeck(Deck);

cout << "\t The deck starts " << endl;

DisplayDeck(Deck);

Shuffle(Deck);

cout << "\tAfter shuffling, your Deck is" << endl;

DisplayDeck(Deck);

j = 0;

for (i = 0; i < 5; i++) {

Hands[0][i] = Deck[j];

j++;

}

SortHand(Hands[1]);

cout << "First hand is: " << endl;

if (HighestCard(Hands[0]))

cout << "\tHighest Card" << endl;

if (IsTwoPair(Hands[0]))

cout << "\tTwo pair" << endl;

if (IsThreeOfAKind(Hands[0]))

cout << "\tThree of a kind" << endl;

for (i = 0; i < 5; i++) {

DisplayCard(Hands[0][i]);

}

for (i = 0; i < 5; i++) {

Hands[1][i] = Deck[j];

j++;

}

SortHand(Hands[1]);

cout << "Second hand is: " << endl;

for (i = 0; i < 5; i++) {

DisplayCard(Hands[1][i]);

}

for (i = 0; i < 5; i++) {

Hands[2][i] = Deck[j];

j++;

}

SortHand(Hands[2]);

cout << "3rd Hand is: " << endl;

for (i = 0; i < 5; i++) {

DisplayCard(Hands[2][i]);

}

for (i = 0; i < 5; i++) {

Hands[3][i] = Deck[j];

j++;

}

SortHand(Hands[3]);

cout << "4th Hand is: " << endl;

for (i = 0; i < 5; i++) {

DisplayCard(Hands[3][i]);

}

}

Cards.cpp

#include

using namespace std;

#include // for random number stuff

#include

#include

#include "Cards.h"

char * ValueNames [] = {

"Two",

"Three",

"Four",

"Five",

"Six",

"Seven",

"Eight",

"Nine",

"Ten",

"Jack",

"Queen",

"King",

"Ace"

};

void DisplayCard (const Card & C)

{

cout << ValueNames [C.Value] << " of " << (char) C.Suit << endl;

}

void DisplayDeck (const Card Deck [])

{

int i;

for (i = 0; i < 52; i++)

DisplayCard (Deck [i]);

}

void DisplayHand(Card Deck[]) {

int i;

for (i = 0; i < 5; i++)

DisplayCard(Deck[i]);

}

void InitDeck (Card Deck [])

{

int i;

Suits S;

Values V;

srand (time (0));

i = 0;

for (S = Hearts; S <= Spades; S = (Suits) (S + 1))

for (V = Two; V <= Ace; V = (Values) (V + 1))

{

Deck [i].Suit = S;

Deck [i].Value = V;

i++;

}

}

bool IsThreeOfAKind (Card Hand [])

{

return ((Hand [0].Value == Hand [2].Value) ||

(Hand [1].Value == Hand [3].Value) ||

(Hand [2].Value == Hand [4].Value));

}

bool IsTwoPair (Card Hand [])

{

if (Hand [0].Value == Hand [1].Value)

if ((Hand [2].Value == Hand [3].Value) || (Hand [3].Value == Hand [4].Value))

return true;

else

return false;

else

if ((Hand [2].Value == Hand [3].Value) && (Hand [3].Value == Hand [4].Value))

return true;

else

return false;

}

bool HighestCard (Card Hand[])

{

if (Hand[0].Value != Hand[1].Value && Hand[0].Value != Hand[2].Value && Hand[0].Value != Hand[3].Value

&& Hand[0].Value != Hand[4].Value && Hand[1].Value != Hand[2].Value && Hand[1].Value != Hand[3].Value &&

Hand[1].Value != Hand[4].Value && Hand[2].Value != Hand[3].Value && Hand[2].Value != Hand[4].Value && Hand[3].Value != Hand[4].Value)

{

return true;

cout << "a HighCard " << endl;

}

else

return false;

}

void Deal(Card Deck[]) {

int i;

int j;

Card Temp;

for (i = 0; i < 5; i++) {

j = rand() % 5;

Temp = Deck[i];

Deck[i] = Deck[j];

Deck[j] = Temp;

}

}

void SortHand(Card Hand[]) {

int i;

int j;

int C1;

int C2;

Card Temp;

for (j = 0; j < 5; j++)

{

C1 = 0;

C2 = 1;

for (i = 0; i <= 4; i++)

{

if (Hand[C1].Value > Hand[C2].Value)

{

Temp = Hand[C1];

Hand[C1] = Hand[C2];

Hand[C2] = Temp;

}

else;

C1++;

C2++;

i++;

}

}

if (Hand[3].Value > Hand[4].Value)

{

Temp = Hand[3];

Hand[3] = Hand[4];

Hand[4] = Temp;

}

if (Hand[2].Value > Hand[3].Value)

{

Temp = Hand[2];

Hand[2] = Hand[3];

Hand[3] = Temp;

}

if (Hand[1].Value > Hand[2].Value)

{

Temp = Hand[1];

Hand[1] = Hand[2];

Hand[2] = Temp;

}

if (Hand[0].Value > Hand[1].Value)

{

Temp = Hand[0];

Hand[0] = Hand[1];

Hand[1] = Temp;

}

}

void Shuffle (Card Deck []){

int i;

int j;

Card Temp;

for (i = 0; i < 52; i++)

{

j = rand () % 52;

Temp = Deck [i];

Deck [i] = Deck [j];

Deck [j] = Temp;

}

}

Cards.h

#ifndef CARDS_H

#define CARDS_H

const int NumCardsInDeck(52);

enum Suits {

Hearts = 3,

Diamonds,

Clubs,

Spades

};

enum Values {

Two,

Three,

Four,

Five,

Six,

Seven,

Eight,

Nine,

Ten,

Jack,

Queen,

King,

Ace

};

struct Card

{

Suits Suit;

Values Value;

};

//void DisplayCard (Card); // passing a structure will automatically be done by value if I do not use an &

void DisplayCard (const Card &); // using & (pass by reference) makes the program a little faster and smaller

void DisplayDeck (const Card []);

void InitDeck (Card []);

bool IsThreeOfAKind (Card []);

void Shuffle (Card []);

void SortHand (Card[]);

bool HighestCard (Card[]);

void Deal (Card[]);

bool IsTwoPair (Card[]);

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!