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

Hello, I need help with my game2.h and game2.cpp for this poker game example in

ID: 3854610 • Letter: H

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 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 << 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;

}

-------------------------------------------------------------------------------------------------------------------------

/ / 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();

}

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++ Pag

Explanation / Answer

//main.cpp

#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <fstream>
#include "game1.h"
#include "game2.h"
#include "hand.h"


using namespace std;

ofstream csis1;
ofstream csis2;

int main(int argc, const char * argv[])
{
Game1 game1;
Game2 game2;

srand((unsigned)time(NULL));

csis1.open("csis1.txt");
csis2.open("csis2.txt");

cout << "Game 1: ";
csis1 << "Game 1: ";
game1.runGame(10000);

cout << "Game 2: ";
csis2 << "Game 2: ";
game2.runGame(10000);

csis1.close();
csis2.close();

return 0;
}

==================================================================================

// card.cpp

#include <iostream>
#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);
}

==============================================================================

// 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

==============================================================================

// deck.cpp

#include <stdlib.h>
#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;
}

========================================================================

// 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

=============================================================================

// game1.cpp

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "game1.h"

using namespace std;

extern ofstream csis1;


void Game1::draw()
{
deck.shuffle(100);
for (int i = 0; i < HANDSIZE; i++) {
hand[i] = deck.getCard();
}
}

void Game1::fold()
{
for (int i = 0; i < HANDSIZE; i++) {
deck.addCard(hand[i]);
}
}

bool Game1::pair(Card curr_hand[])
{
if (curr_hand[0].getValue() == curr_hand[1].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[2].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[2].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[2].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[2].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[3].getValue() == curr_hand[4].getValue()) {
return true;
}
else {
return false;
}
}

bool Game1::flush(Card curr_hand[])
{
int i = 0;

if (curr_hand[i].getSuit() == curr_hand[i+1].getSuit() && curr_hand[i+2].getSuit() == curr_hand[i+3].getSuit() && curr_hand[i].getSuit() == curr_hand[i+4].getSuit()) {
return true;
}

return false;
}

void Game1::runGame(int trials)
{
Game1 g;
int no_pair,
no_flush,
trial_no = 1;
double pair_per,
flush_per;

cout << setw(9) << "Trial #" << setw(12) << "# of Hands" << setw(12) << "# of Pairs" << setw(15) << "# of Flushes" << setw(20) << "Pair Percentage" << setw(20) << "Flush Percentage" << endl;
cout << setw(9) << "-------" << setw(12) << "----------" << setw(12) << "----------" << setw(15) << "------------" << setw(20) << "---------------" << setw(20) << "----------------" << endl;
csis1 << setw(9) << "Trial #" << setw(12) << "# of Hands" << setw(12) << "# of Pairs" << setw(15) << "# of Flushes" << setw(20) << "Pair Percentage" << setw(20) << "Flush Percentage" << endl;
csis1 << setw(9) << "-------" << setw(12) << "----------" << setw(12) << "----------" << setw(15) << "------------" << setw(20) << "---------------" << setw(20) << "----------------" << endl;
for (int i = 0; i < 10; i++ ) {
no_pair = 0;
no_flush = 0;

for (int i = 0; i < trials; i++ ) {
g.draw();

if (g.pair(g.hand)) {
no_pair++;
}

if (g.flush(g.hand)) {
no_flush++;
}

g.fold();
}
pair_per = double(no_pair) / trials;
flush_per = double(no_flush) / trials;

cout << fixed << setprecision(2) << setw(9) << trial_no << setw(12) << trials << setw(12) << no_pair << setw(15) << no_flush << setw(20) << pair_per << setw(20) << flush_per << endl;
csis1 << fixed << setprecision(2) << setw(9) << trial_no << setw(12) << trials << setw(12) << no_pair << setw(15) << no_flush << setw(20) << pair_per << setw(20) << flush_per << endl;
trial_no++;
}
}

===========================================================================

// game1.h

#ifndef Poker_Lab_game1_h
#define Poker_Lab_game1_h

#include "card.h"
#include "deck.h"

const int HANDSIZE = 5;

class Game1 {
private:
Card hand[HANDSIZE];
Deck deck;
public:
void draw();
void fold();
bool pair(Card curr_hand[]);
bool flush(Card curr_hand[]);
void runGame(int trials);
};

#endif

=============================================================================

// game2.cpp

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "hand.h"
#include "game2.h"

using namespace std;

extern ofstream csis2;

void Game2::runGame(int trials)
{
Hand h;
Card *hp;
int no_pair,
no_flush,
trial_no = 1;
double pair_per,
flush_per;

cout << setw(9) << "Trial #" << setw(12) << "# of Hands" << setw(12) << "# of Pairs" << setw(15) << "# of Flushes" << setw(20) << "Pair Percentage" << setw(20) << "Flush Percentage" << endl;
cout << setw(9) << "-------" << setw(12) << "----------" << setw(12) << "----------" << setw(15) << "------------" << setw(20) << "---------------" << setw(20) << "----------------" << endl;
csis2 << setw(9) << "Trial #" << setw(12) << "# of Hands" << setw(12) << "# of Pairs" << setw(15) << "# of Flushes" << setw(20) << "Pair Percentage" << setw(20) << "Flush Percentage" << endl;
csis2 << setw(9) << "-------" << setw(12) << "----------" << setw(12) << "----------" << setw(15) << "------------" << setw(20) << "---------------" << setw(20) << "----------------" << endl;
for (int i = 0; i < 10; i++ ) {
no_pair = 0;
no_flush = 0;

for (int i = 0; i < trials; i++ ) {
h.draw();
hp = h.getHand();

if (h.pair(hp)) {
no_pair++;
}

if (h.flush(hp)) {
no_flush++;
}

h.fold();
}
pair_per = double(no_pair) / trials;
flush_per = double(no_flush) / trials;

cout << fixed << setprecision(2) << setw(9) << trial_no << setw(12) << trials << setw(12) << no_pair << setw(15) << no_flush << setw(20) << pair_per << setw(20) << flush_per << endl;
csis2 << fixed << setprecision(2) << setw(9) << trial_no << setw(12) << trials << setw(12) << no_pair << setw(15) << no_flush << setw(20) << pair_per << setw(20) << flush_per << endl;
trial_no++;
}

}

=============================================================================

// game2.h

#ifndef __Poker_Lab__game2__
#define __Poker_Lab__game2__

#include "hand.h"

class Game2 {
public:
void runGame(int trials);
};

#endif

============================================================================

// hand.cpp

#include "hand.h"

void Hand::draw()
{
deck.shuffle(100);
for (int i = 0; i < HAND_SIZE; i++) {
hand[i] = deck.getCard();
}
}

Card * Hand::getHand()
{
return hand;
}

void Hand::fold()
{
for (int i = 0; i < HAND_SIZE; i++) {
deck.addCard(hand[i]);
}
}

bool Hand::pair(Card curr_hand[])
{
if (curr_hand[0].getValue() == curr_hand[1].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[2].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[0].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[2].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[1].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[2].getValue() == curr_hand[3].getValue()) {
return true;
}
else if (curr_hand[2].getValue() == curr_hand[4].getValue()) {
return true;
}
else if (curr_hand[3].getValue() == curr_hand[4].getValue()) {
return true;
}
else {
return false;
}
}

bool Hand::flush(Card curr_hand[])
{
int i = 0;

if (curr_hand[i].getSuit() == curr_hand[i+1].getSuit() && curr_hand[i+2].getSuit() == curr_hand[i+3].getSuit() && curr_hand[i].getSuit() == curr_hand[i+4].getSuit()) {
return true;
}

return false;
}

===============================================================================

// hand.h

#ifndef __Poker_Lab__hand__
#define __Poker_Lab__hand__

#include "card.h"
#include "deck.h"

const int HAND_SIZE = 5;

class Hand {
private:
Deck deck;
Card hand[HAND_SIZE];
public:
void draw();
Card * getHand();
void fold();
bool pair(Card curr_hand[]);
bool flush(Card curr_hand[]);
};

#endif

===============================================================================

csis1.txt

Game 1:

Trial # # of Hands # of Pairs # of Flushes Pair Percentage Flush Percentage
------- ---------- ---------- ------------ --------------- ----------------
1 10000 4981 114 0.50 0.01
2 10000 4914 117 0.49 0.01
3 10000 4936 139 0.49 0.01
4 10000 4863 140 0.49 0.01
5 10000 4918 122 0.49 0.01
6 10000 4963 134 0.50 0.01
7 10000 4930 126 0.49 0.01
8 10000 4998 108 0.50 0.01
9 10000 4890 118 0.49 0.01
10 10000 4918 129 0.49 0.01

======================================================================

csis2.txt

Game 2:

Trial # # of Hands # of Pairs # of Flushes Pair Percentage Flush Percentage
------- ---------- ---------- ------------ --------------- ----------------
1 10000 5013 112 0.50 0.01
2 10000 4948 119 0.49 0.01
3 10000 4897 142 0.49 0.01
4 10000 4862 134 0.49 0.01
5 10000 4979 123 0.50 0.01
6 10000 4939 127 0.49 0.01
7 10000 4965 130 0.50 0.01
8 10000 4959 134 0.50 0.01
9 10000 4943 117 0.49 0.01
10 10000 4969 122 0.50 0.01

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote