Hello, I need help with this poker game example in C++ The first page and half i
ID: 3854408 • Letter: H
Question
Hello, I need help with this poker game example in C++
The first page and half is general explanation about the source code (only if needed) and 2nd page are the actual instructions for the lab example. The source codes (card & deck) will be below the photo instructions.
-------------------------------------------------------------------------------------------------------------------------
Provided Codes:
/ / card.h
#ifndef _CARD_H
#define _CARD_H
class Card {
public:
// Define types for the suit and value
enum Suit { Diamonds, Hearts, Clubs, Spades };
enum Value { NullCard, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
private:
static const char *snames[4];
static const char *vnames[14];
Suit s;
Value v;
public:
// Constructors initialize a card
Card();
Card(Suit newSuit, Value newValue);
Suit getSuit(); // Returns a card's suit.
Value getValue(); // Returns a card's value.
void printSuit(); // Print a card's suit.
void printValue(); // Print a card's value.
void printCard(); // Print a card's suit and value.
};
// Return the next suit or card value in succession
Card::Suit nextSuit(Card::Suit);
Card::Value nextValue(Card::Value);
#endif
-------------------------------------------------------------------------------------------------------------------------
/ / card.cpp
#include
#include "card.h"
using namespace std;
const char * Card::snames[4] = {" Diamonds", "Hearts", "Clubs", "Spades" };
const char * Card::vnames[14] = {" Bad Card", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
Card::Card() {
v = NullCard;
}
Card::Card(Suit newSuit, Value newValue) {
s = newSuit;
v = newValue;
}
Card::Suit Card::getSuit() {
return s;
}
Card::Value Card::getValue() {
return v;
}
void Card::printSuit() {
cout << snames[s];
}
void Card::printValue() {
cout << vnames[v];
}
void Card::printCard() {
printValue();
cout << " of ";
printSuit();
}
Card::Suit nextSuit(Card::Suit s) {
return(s+1 > Card::Spades) ? Card::Diamonds : (Card::Suit) (s+1);
}
Card::Value nextValue(Card::Value v) {
return(v+1 > Card::King) ? Card::Ace : (Card::Value) (v+1);
}
-------------------------------------------------------------------------------------------------------------------------
/ / deck.h
#ifndef _DECK_H
#define _DECK_H
#include "card.h"
const int DECKSIZE = 52;
class Deck {
private:
Card inDeck[DECKSIZE]; // These are private data members and
int nextCard; // can be used only by member functions.
public:
Deck(); // Initialization. Called automatically
// when a Deck variable is declared.
void shuffle(int); // Exchange random pairs of cards.
Card getCard(); // Returns top card from the deck.
void addCard(Card); // Put named card in the deck.
int totalCards(); // Returns number of cards left in deck.
};
#endif
-------------------------------------------------------------------------------------------------------------------------
/ / deck.cpp
#include
#include "deck.h"
Deck::Deck() {
int i;
Card::Suit curSuit;
Card::Value curValue;
nextCard = 0;
for(i = 0, curSuit = Card::Diamonds, curValue = Card::Ace; i < DECKSIZE; i++) {
inDeck[i] = Card(curSuit, curValue);
curValue = nextValue(curValue);
if (curValue == Card::Ace)
curSuit = nextSuit(curSuit);
}
}
void Deck::shuffle(int swaps) {
Card temp;
for (int i = 0; i < swaps; i++) {
int i1 = rand() % DECKSIZE;
int i2 = rand() % DECKSIZE;
temp = inDeck[i1];
inDeck[i1] = inDeck[i2];
inDeck[i2] = temp;
}
}
Card Deck::getCard() {
return(nextCard < DECKSIZE) ? inDeck[nextCard++] : Card(Card::Clubs, Card::NullCard);
}
void Deck::addCard(Card newCard) {
if (nextCard > 0)
inDeck[--nextCard] = newCard;
}
int Deck::totalCards() {
return DECKSIZE - nextCard;
}
Classes Lab (Poker) For this this assignment you will be given the code for two classes (Card and Deck) enting a set of playing cards. Using these classes, you will write a m, game1.h and gamel.cpp, that calculates the representing a set of of certain poker hands. frequency of occurrence be necessary to understand the code (and at this point I don't expect you to) not Note that that while I will give a simple description of each of the classes below, it will for understand the classes in order to use the classes in your program. You will only in order to use the classes in your programYou will only need to need to be able to compile able to compile these classes without modification. In fact, I do not want you classes. You should to modity the the classes, unless your include statement requires a change to modify the cla ostream to iostream.h and your compiler does not require a namespace from iostreanm declaration. The Card class is defined in card. h and card.cpp. These files define a class (data type) for representing individual playing cards. A card can be initialized inspected, and printed using the following methods getSuit (1 getValue () printsuit() printValue () printCard )) Note that the Card class defines enumerated data types to represent the suit and value. You can use these types in your program, but since they are defined within the Card class, you have to use the scope resolution operator to access them. That is, if your program needs to define a variable to contain a card value, it would be declared: Card::Value cv If you want to assign the value ACE to this variable, you would write cv Card:ACE; = Page Focus on Object-Oriented Programming with C++Explanation / Answer
Here is the code for the game1.h and game1.cpp along with output. In case of any issues, post a comment and I will respond. Please rate the answer if it helped.
Note: you will have to use card.h card.cpp, deck.h and deck.cpp from the question. Also if deck.cpp shows error for rand(), please include <cstdlib> in deck.cpp.
game1.h
#ifndef game1_h
#define game1_h
#include "card.h"
#include "deck.h"
class Game
{
private:
Card hand[5]; //5 cards to represent the hand
Deck deck;
int maxHands; // the number of hands to deal
int flushs[100], pairs[100]; // the count of flushes and pairs for each trial
int trials;
void dealHand(); //deal a hand
void returnCards(); // return the cards in hand back to deck
void checkHand(int trial); //check the type of hand
bool isPair(); //to check if hand is a pair
bool isFlush(); //to check if hand is flush
public:
Game(int numHands = 10000, int numTrials = 10); //constructor to initialize with how many hands to be dealt
void start();
void printStats();
};
#endif /* game1_h */
game1.cpp
#include "game1.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
Game::Game(int numHands, int trials) //constructor to initialize with how many hands to be dealt
{
this->maxHands = numHands;
this->trials = trials;
}
void Game::start()
{
srand(time(0));
for(int t = 0; t < trials; t++)
{
for(int i = 1; i <= maxHands; i++)
{
dealHand();
checkHand(t);
returnCards();
}
}
}
void Game::dealHand() //deal a hand
{
deck.shuffle(100);
for(int i = 0; i < 5; i++)
{
hand[i] = deck.getCard();
//hand[i].printCard();
}
}
void Game::returnCards() // return the cards in hand back to deck
{
for(int i = 0; i < 5; i++)
deck.addCard(hand[i]);
}
void Game::checkHand(int trial)//check the type of hand
{
if(isPair())
pairs[trial]++;
else if(isFlush())
flushs[trial]++;
}
bool Game::isPair()
{
int count[14] = {0}; //counter to keep the count of cards of same value, 1 counter each for ACE, TWO ....etc
Card::Value v;
for(int i = 0; i < 5; i++)
{
v = hand[i].getValue();
count[v] ++;
if(count[v] == 2)
return true;
}
return false;
}
bool Game::isFlush()
{
for(int i = 1; i < 5; i ++)
{
if(hand[i].getSuit() != hand[0].getSuit()) //check to see if all cards are from same suit as 1st card?, if not return false
return false;
}
return true;
}
void Game::printStats()
{
double pairPc, flushPc;
double avgPairPc = 0, avgFlushPc = 0;
cout << fixed << setprecision(2);
cout << setw(10) << "Trial" << setw(10)<< "No. Cards" << setw(10) << "Pairs" << setw(10) << "Flushes"
<< setw(10) << "Pair %" << setw(10) << "Flush %" << endl;
for(int i = 0; i < trials; i++)
{
pairPc = pairs[i] * 100.0 / maxHands;
flushPc = flushs[i] * 100.0 / maxHands;
avgPairPc += pairPc;
avgFlushPc += flushPc;
cout << setw(10) << (i+1) << setw(10) << maxHands << setw(10)<< pairs[i] << setw(10) << flushs[i] << setw(10) << pairPc << setw(10) << flushPc << endl;
}
avgPairPc /= trials;
avgFlushPc /= trials;
cout << " Average pair pecentage: " << avgPairPc << endl;
cout << "Average flush pecentage: " << avgFlushPc << endl;
}
int main()
{
Game g;
g.start();
g.printStats();
}
output
Trial No. Cards Pairs Flushes Pair % Flush %
1 10000 4936 16 49.36 0.16
2 10000 4930 24 49.30 0.24
3 10000 4949 16 49.49 0.16
4 10000 4941 14 49.41 0.14
5 10000 4909 18 49.09 0.18
6 10000 4946 19 49.46 0.19
7 10000 4896 18 48.96 0.18
8 10000 4894 21 48.94 0.21
9 10000 4880 16 48.80 0.16
10 10000 5041 16 50.41 0.16
Average pair pecentage: 49.32
Average flush pecentage: 0.18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.