Nov 18, 2018 at 9:38pm UTC
I was supposed to create a deck of cards and then use the deck of cards to play a game where I give a card to the user and a card to the cpu, we then both show our cards to each other, and whoever has the highest scoring card gets a point. Whoever gets ahead by 2 points wins. I have created the deck of cards, but i need help in creating the game. How would i assign values to the cards, and would how to give the user and cpu a random card out of the deck. I NEED to use BOOL OPERATOR and Pointers. I have done this game other ways however it wasn't allowed.
// 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;
}
Last edited on Nov 19, 2018 at 1:01am UTC