ASSIGNMENT IN C++: Write a program that will operate on a deck of 52 cards. Your
ID: 665461 • Letter: A
Question
ASSIGNMENT IN C++:
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. Display 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.
BONUS: Determine which is the winning hand. A tie is possible.
NOTES:
1. Use a structure to hold the face value and suit of each card.
2. Use enumerations for the values of Jack, Queen, King, and Ace and to identify the suits (Diamonds, Clubs, etc.).
Explanation / Answer
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
#include "Card.h"
static char * ValueNames [] ={"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
void InitDeck (Card Deck [])
{
int i;
for (i = 0; i < 52; i++)
{
Deck [i].Suit = (Suits) ((i / 13) + Hearts);
Deck [i].Value = (Values) (i % 13);
}
srand (time (0));
}
void Show (const Card & C)
{
cout << ValueNames [C.Value] << " of " << (char) C.Suit << endl;
}
void ShowDeck (const Card Deck [])
{
int i;
for (i = 0; i < 52; i++)
Show (Deck [i]);
}
void ShuffleDeck (Card Deck [])
{
int i;
bool Picked [52];
Card TempDeck [52];
int Which;
int Counter;
memcpy (TempDeck, Deck, 52 * sizeof (Card));
memset (Picked, false, 52 * sizeof (bool)); // no cards have been picked yet
for (i = 0; i < 52; i++)
{
Counter = 0;
do {
Which = rand () % 52;
Counter++;
} while (Picked [Which]);
Picked [Which] = true;
Deck [i] = TempDeck [Which];
}
}
void Deal(Card Hands[][handlength], Card Deck [])
{
int i;
for (i = 0; i < handlength; i++)
{
Hands[0][i] = Deck[i];
Hands[1][i] = Deck[i + 5];
Hands[2][i] = Deck[i + 10];
Hands[3][i] = Deck[i + 15];
}
cout << "Hand 1" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [0][i]);
}
cout << "Hand 2" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [1][i]);
}
cout << "Hand 3" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [2][i]);
}
cout << "Hand 4" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [3][i]);
}
}
void BubbleSort(Card Hands[][handlength])
{
for (int j = 0; j < 4; j++)
{
int i;
bool Sorted;
Card Temp;
int Sorthand;
Sorthand = handlength - 1;
do {
Sorted = true;
for (i = 0; i < handlength - 1; i++)
if (Hands[j][i].Value > Hands[j][i + 1].Value)
{
Temp = Hands[j][i];
Hands[j][i] = Hands[j][i + 1];
Hands[j][i + 1] = Temp;
Sorted = false;
}
else;
Sorthand--;
} while (!Sorted);
}
int i;
cout << "New Sorted Hands!" << endl;
cout << "Hand 1" << endl;
for (i = 0; i < handlength; i++){
{
Show (Hands [0][i]);
}
cout << "Hand 2" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [1][i]);
}
cout << "Hand 3" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [2][i]);
}
cout << "Hand 4" << endl;
for (i = 0; i < handlength; i++)
{
Show (Hands [3][i]);
}
}
bool IsThreeOfAKind (int check, Card Hands [players][handlength])
{
return ((Hands[check][0].Value == Hands [check][2].Value) ||(Hands[check][1].Value == Hands [check][3].Value) ||
(Hands[check][2].Value == Hands [check][4].Value));
}
}
#ifndef CARD_H
#define CARD_H
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;
int Value;
};
const long handlength(5);
const long players(4);
void InitDeck (Card []);
void Show (const Card &); // default method of passing a struct as a parameter is call by value
// for efficiency, a struct is usually done as call by reference
// with const if we don't want it to change
void ShowDeck (const Card []);
void ShuffleDeck (Card []);
void Deal (Card [][handlength], Card[]);
void BubbleSort(Card[][handlength]);
bool IsThreeOfAKind (int, Card [players][handlength]);
#endif
#include <iostream>
using namespace std;
#include "Card.h"
void main ()
{
Card Deck [52];
InitDeck (Deck);
ShuffleDeck (Deck);
Card Hands[4][5];
cout << "Here Are The Hands!" << endl;
Deal(Hands,Deck);
BubbleSort(Hands);
int check;
for (check = 0; check < 4; check++)
{
if (IsThreeOfAKind(check,Hands) == 1)
cout << "Player " << check <<" has a three of a kind" << endl;
}
}
----------
for the poker game we need to shuffle
observe below code this works successfully
Deck.h
Deck.cpp
To do it correctly.
Or you can use the std::random_shuffle() algorithm.