i need to make a player class in c++ i have these files to make a blackjack game
ID: 3830076 • Letter: I
Question
i need to make a player class in c++ i have these files to make a blackjack game
header file
#ifndef DECK_H
#define DECK_H
#include <array> //our std::array library
#include "Card.h"
// Add the member variables and member functions needed to create and work with our Deck object
// Hint: our deck will be a std::array of Card objects
class Deck
{
public:
Deck(); //constructor
//extra functions
void shuffle(); //Shuffle our deck
void print(); //Print the whole deck
void print(int cardNumber); //Print an individual card
Card deal(); //Deal out the next available card
int cardsRemaining(); //Count how many cards are left
void reset(); //Reset back to an ordered deck (our previous create function)
protected:
private:
static const int m_deckSize = 52; //number of cards in our deck
std::array<Card, m_deckSize> m_deck; //array of cards
int m_nextCard; //next card to dealing
};
#endif // DECK_H
cpp.file
Deck::Deck()
{
Deck::reset();
}
void Deck::shuffle()
{
for(int x=0;x < m_deckSize;x++){
//swap this card with some random card
swap(m_deck[x], m_deck[ rand()%m_deckSize]);
}
}
void Deck::print()
{
for(int x=0; x < m_deckSize; x++){ //Print each card in the deck
m_deck[x].print();
}
}
void Deck::print(int cardNumber)
{
m_deck[cardNumber];
}
Card Deck::deal()
{
}
int Deck::cardsRemaining()
{
}
//This function resets our deck to 2-A of diamonds, 2-A clubs, 2-A hearts, 2-A spades
void Deck::reset()
{
for(int x=0; x<m_deckSize; x++){
//x%(m_deckSize/4) is between 0 to 12
//add two to get it to be 2 to 14
m_deck[x].Setrank( (Rank)(x%(m_deckSize/4)+2) );
//m_deckSize / 4 is 13
// x/13, integer math, gives us 0 to 3(our suit values)
m_deck[x].Setsuit( (Suit)(x/(m_deckSize/4) ) );
}
}
header file
#ifndef CARD_H
#define CARD_H
//define our rank enumerated type
enum Rank{TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
//define our suit enumerated type
enum Suit{DIAMOND, CLUB, HEART, SPADE};
class Card
{
public:
Card(); //constructor
//extra functions we need
void print(); //pretty-print for our card
//Since our member variables are private, we need helper functions
Rank Getrank() { return m_rank; }
void Setrank(Rank val); //set function prototype
Suit Getsuit() { return m_suit; }
void Setsuit(Suit val); //set function prototype
protected:
private:
Rank m_rank; //member variable, store card's rank
Suit m_suit; //member variable, store card's suit
};
//extra functions
void swap(Card &A, Card &B);
#endif // CARD_H
cpp file
Card::Card()
{
//ctor
//called when we make a new card
//initializing our member variables to default values
m_suit = DIAMOND;
m_rank = FOUR;
}
//setter functions
void Card::Setrank(Rank val)
{
if( val >= TWO && val <= ACE){
m_rank = val;
}
else{
std::cout << "Error! Invalid rank value! " << val << std::endl;
}
}
//we must call these functions by their full names, they are implemented outside of the
void Card::Setsuit(Suit val)
{
if( val >= DIAMOND && val <= SPADE){
m_suit = val;
}
else{
std::cout << "Error! Invalid suit value! " << val << std::endl;
}
}
Explanation / Answer
Please find the whole project required for the program
******************
******************
Card.h
***************
************************
Deck.h
**********************
Game.cpp
********************
Game.h
******************
main.cpp
************************
Another way of doing the program
**********************
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
void hitorstand();
void roundover();
Game game1;
int pWinCnt=0,dWinCnt=0,tieCnt=0,gameCnt=0,roundCnt=0;
main()
{
char input;
game1.start();
//do while loop: lets the player keep playing till he decides to quit
do
{ roundCnt++;
game1.clearHand();
game1.deal(2,'d');
game1.deal(2,'p');
game1.pgame(0);
//Check if either player has 21
if((game1.val('d')>=21)||(game1.val('p')>=21))
{
roundover();
}
else
{
hitorstand();
}
//Shuffle deck after every 4 games
if(roundCnt>4)
{
game1.shuffle();
roundCnt=0;
}
rePrompt: //returns here if invalid input
printf("Do you want to play another game (y/n)?");
scanf(" %c",&input);
//check for valid inputs
if ((input!='y') && (input!='n'))
{printf("Invalid option!!! ");goto rePrompt;}
}while(input=='y');
//Display Results
system("clear");
printf("************************ ");
printf("Final Scores: ");
printf("************************ ");
printf("Dealer: %d ",dWinCnt);
printf("Player: %d ",pWinCnt);
printf("Ties: %d ",tieCnt);
if (dWinCnt<pWinCnt)
printf("Comments: Congratulations... ");
else if (dWinCnt>pWinCnt)
printf("Comments: Stay at home... ");
else
printf("Comments: Better luck next time... ");
printf(" Thanks for playing!!! ");
}
void hitorstand()
{
char opt;
printf("Hit or Stand?(h/s)");
scanf(" %c",&opt);
if (opt=='h')
{
game1.hit('p');
game1.pgame(0);
if(game1.val('p')<21) hitorstand();
else roundover();
}
else if (opt=='s')
{
while(game1.val('d')<17)
{
game1.hit('d');
}
roundover();
}
else {printf("Invalid option!!! ");hitorstand();}
}
void roundover()
{
game1.pgame(1);
printf("Dealer=%d ",game1.val('d'));
printf("Player=%d ",game1.val('p'));
if((game1.val('p')>21)||((game1.val('d')>game1.val('p'))&&(game1.val('d')<=21)))
{ printf("DEALER WINS !!! ");
dWinCnt++;
}
else if ((game1.val('d')>21)||((game1.val('p')>game1.val('d'))&&(game1.val('p')<=21)))
{
printf("PLAYER WINS !!! ");
pWinCnt++;
}
else
{ printf("TIE GAME !!! ");
tieCnt++;
}
}
**********************
The related file to the above program
******************
game.h
//Game Class Definition
class Game
{
public: Game(void); //Constructor
void start(void); //Seeds the random number generator
void shuffle(void); //Shuffle cards & reset deck
void deal(int n,char opt);//Deal n cards based on opt
int val(char opt); //Calculates value of hand
void hit(char opt); //Add card to hand
void pgame(int gameOverFlag);//Print Game
void clearHand(void){handP[0]=NULL;handD[0]=NULL;};//clear hand
private: int hsize(char opt); //returns number of cards in hand
void swap(int,int);
int getran(int); //generates random number
void phand(int flag, char opt);//Print hand
static char *card[53]; //deck of cards
static char *handD[53]; //Dealer's hand
static char *handP[53]; //Player's hand
static int N; //Number of cards in deck
};
**********************
P.S KINDLY FOLLOW BOTH THE PROGRAMS AND CHOOSE WHICH ONE YOU FEEL EASY
#pragma once #include <iostream> class Game { public: Game(); ~Game(); };Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.