Question
Hello, I need help with my game2.h and game2.cpp for this poker game example in C++ The first part is complete (game1.h & game1.cpp), I
Hello, I need help with my game2.h and game2.cpp for this poker game example in C++
The first part is complete (game1.h & game1.cpp), I just need help with this second part please. I will Provide all the source/given codes below the pictured instructions.
Thank you
-------------------------------------------------------------------------------------------------------------------------
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
}
void Card::printValue() {
cout
}
void Card::printCard() {
printValue();
cout
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
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
int i1 = rand() % DECKSIZE;
int i2 = rand() % DECKSIZE;
temp = inDeck[i1];
inDeck[i1] = inDeck[i2];
inDeck[i2] = temp;
}
}
Card Deck::getCard() {
return(nextCard
}
void Deck::addCard(Card newCard) {
if (nextCard > 0)
inDeck[--nextCard] = newCard;
}
int Deck::totalCards() {
return DECKSIZE - nextCard;
}
-------------------------------------------------------------------------------------------------------------------------
/ / 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
#include
#include
#include
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
{
for(int i = 1; i
{
dealHand();
checkHand(t);
returnCards();
}
}
}
void Game::dealHand() //deal a hand
{
deck.shuffle(100);
for(int i = 0; i
{
hand[i] = deck.getCard();
//hand[i].printCard();
}
}
void Game::returnCards() // return the cards in hand back to deck
{
for(int i = 0; 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
{
v = hand[i].getValue();
count[v] ++;
if(count[v] == 2)
return true;
}
return false;
}
bool Game::isFlush()
{
for(int i = 1; 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
cout
for(int i = 0; i
{
pairPc = pairs[i] * 100.0 / maxHands;
flushPc = flushs[i] * 100.0 / maxHands;
avgPairPc += pairPc;
avgFlushPc += flushPc;
cout
}
avgPairPc /= trials;
avgFlushPc /= trials;
cout
cout
}
int main()
{
Game g;
g.start();
g.printStats();
}
the Card and and Deck classes outlined above (again without making any changes in these two classes.) However, instead of placing all of the code for a Poker Lab- Part 2 for a poker hand Next, create a new program, game2. h and game2. cpp, using the ca er hand will in game1 o as you did in the first part of the lab, the code for ker h All of the code that you wrote to manipulate the poker hand (check for for flush, deal hand, return cards to the deck at the end of each ha now become the member functions of this new Hand class. The priva now reside within its own class, Hand end of each hand, etc.) will w an array of cards in the hand (i.e., Hand class will contain the members of the Card objects). This new class hand. cpp should reside in its own source files, hand.h you will Keep in mind that as you write the member functi also need to rewrite main() to accommodate these changes. You should fin that there is much less code in game2.cpp than game1.cpp. Hint: There is no need for a declaration of a Deck in the Hand class. However a ever, a Deck object could be passed to a member function of the Hand class. Finally, be sure to turn in separate runs for both the game1 and game2 Also make sure to use separate output file for each run: csisl.txt for programs. for gamel and csis2.txt for game2. Focus on Object-Oriented Programming with C++ PagStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started