Nov 26, 2018 at 12:11am UTC
I was supposed to create a deck of cards ( done ) however while trying to make a game where the computer draws a cards and the user draws a card and whoever has a higher value card gets a point(whoever gets ahead by 2 points wins), I came to a problem. I need to use pointers, arrays, bool, NEW, etc. I dont know how to use those. PLEASE HELP
// CARD.H
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;
const int CARDS_PER_DECK = 52;
class Card
{
public:
static const int totalFaces = 13;
static const int totalSuits = 4;
Card(int cardFace = 1, int cardSuit = 1);
string print() const;
int getFace() const
{
return face;
}
int getSuit() const
{
return suit;
}
private:
int face;
int suit;
static const string faceNames[totalFaces];
static const string suitNames[totalSuits];
};
#endif
// CARD.CPP
#include <iostream>
#include "Card.h"
#include "DeckOfCards.h"
using namespace std;
const string Card::faceNames[totalFaces] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
const string Card::suitNames[totalSuits] = { "Hearts", "Clubs", "Diamonds", "Spades" };
Card::Card(int cardFace, int cardSuit)
{
face = cardFace;
suit = cardSuit;
}
string Card::print() const
{
return faceNames[face] + " of " + suitNames[suit];
}
// DECKOFCARDS.H
#ifndef DECK_OF_CARDS_H
#define DECK_OF_CARDS_H
#include <vector>
#include "Card.h"
using namespace std;
class DeckOfCards
{
public:
DeckOfCards();
void shuffle();
Card dealCard();
void printDeck();
bool moreCards() const;
private:
vector< Card > deck;
unsigned int currentCard;
};
#endif
// DECKOFCARDS.CPP
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "DeckOfCards.h"
#include "Card.h"
using namespace std;
DeckOfCards::DeckOfCards()
{
for (int i = 0; i < Card::totalFaces; i++)
{
for (int j = 0; j < Card::totalSuits; j++)
{
deck.emplace_back(Card(i, j));
}
}
currentCard = 0;
}
void DeckOfCards::printDeck()
{
cout << left;
for (int i = 0; i < CARDS_PER_DECK; i++)
{
cout << setw(19) << deck[i].print();
if ((i + 1) % 4 == 0)
cout << endl;
}
}
void DeckOfCards::shuffle() {
for (int first = 0; first < CARDS_PER_DECK; first++)
{
int second = (rand() + time(0)) % CARDS_PER_DECK;
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
currentCard = 0;
}
Card DeckOfCards::dealCard()
{
return (deck[currentCard++]);
}
bool DeckOfCards::moreCards () const
{
return currentCard < 52;
}
//main.cpp
#include <iostream>
#include <iomanip>
#include "DeckOfCards.h"
using namespace std;
int main()
{
DeckOfCards myDeckOfCards;
Card currentcard;
Card myCard{ 1,2 };
myCard.print();
cout << endl;
system("pause");
return 0;
}