Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Card Shuffling and Dealing Program. Can shuffle a deck of cards and deal a hand to the computer and the player, but cannot compare all hand types or play a game of poker. Includes main.cpp, Card.cpp, Card.hpp, DeckOfCards.cpp, and DeckOfCards.hpp
- //Card.hpp code
- #ifndef CARD_HPP
- #define CARD_HPP
- #include <iostream>
- #include <array>
- #include <string>
- enum class Face{
- ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
- EIGHT, NINE, TEN, JACK,QUEEN, KING
- };
- enum class Suit{CLUBS, SPADES, HEARTS, DIAMONDS};
- class Card{
- public:
- static const size_t FACEMAX{13};
- static const size_t SUITMAX{4};
- static const std::array<Face, FACEMAX> faces;
- static const std::array<Suit, SUITMAX> suits;
- Card (Face face, Suit suit);
- Card ();
- std::string toString () const;
- std::string toFaceString() const;
- std::string toSuitString() const;
- Face getFace() const;
- Suit getSuit() const;
- private:
- Face cardFace;
- Suit cardSuit;
- static const std::array<std::string, FACEMAX> faceString;
- static const std::array<std::string, SUITMAX> suitString;
- };
- std::ostream& operator<<(std::ostream& out, const Card& cardObject);
- bool operator==(const Card& left, const Card& right);
- bool operator!=(const Card& left, const Card& right);
- void operator+=(Card& left, int right);
- #endif // CARD_HPP
- //Card.cpp code
- #include "Card.hpp"
- const std::array<std::string, Card::FACEMAX> Card::faceString{"Ace", "Two", "Three", "Four",
- "Five", "Six", "Seven", "Eight",
- "Nine", "Ten", "Jack", "Queen", "King"};
- const std::array<std::string, Card::SUITMAX> Card::suitString{"Clubs", "Spades", "Hearts", "Diamonds"};
- const std::array<Face, Card::FACEMAX> Card::faces {
- Face::ACE, Face::TWO, Face::THREE, Face::FOUR, Face::FIVE,
- Face::SIX, Face::SEVEN, Face::EIGHT, Face::NINE, Face::TEN,
- Face::JACK, Face::QUEEN, Face::KING
- };
- const std::array<Suit, Card::SUITMAX>
- Card::suits {Suit::CLUBS, Suit::DIAMONDS, Suit::HEARTS, Suit::SPADES};
- Card::Card (Face face, Suit suit)
- :cardFace{face}, cardSuit{suit}
- {}
- Card::Card()
- : Card(Face::ACE, Suit::CLUBS)
- {}
- std::string Card::toString()const {
- std::string cardString{};
- cardString += faceString[static_cast<int>(cardFace)];
- cardString += " of ";
- cardString += suitString[static_cast<int>(cardSuit)];
- return cardString;
- }
- std::string Card::toFaceString() const{
- std::string myString{faceString[static_cast<int>(cardFace)]};
- return myString;
- }
- std::string Card::toSuitString() const{
- std::string myString{suitString[static_cast<int>(cardSuit)]};
- return myString;
- }
- Face Card::getFace() const{
- return cardFace;
- }
- Suit Card::getSuit() const{
- return cardSuit;
- }
- std::ostream& operator<<(std::ostream& out, const Card& cardObject){
- out << cardObject.toString();
- return out;
- }
- bool operator==(const Card& left, const Card& right){
- return (left.toString() == right.toString());
- }
- bool operator!=(const Card& left, const Card& right){
- return (left.toString() != right.toString());
- }
- void operator+=(Card& left, const int right) {
- ;
- }
- //DeckOfCards.hpp code
- #ifndef DECKOFCARDS_HPP
- #define DECKOFCARDS_HPP
- #include "Card.hpp"
- #include <cstdlib>
- #include <iomanip>
- class DeckOfCards {
- public:
- static const size_t DECKMAX{52};
- static const size_t HANDMAX{5};
- DeckOfCards();
- void printDeck() const;
- void shuffle();
- Card dealCard();
- std::array<Card, HANDMAX> dealHand();
- bool moreCards();
- int cardsRemaining();
- int getSize();
- bool validateDeck();
- private:
- std::array<Card, DECKMAX> deck;
- int currentCard{0};
- static std::array<Card, DECKMAX> initDeck();
- };
- #endif // DECKOFCARDS_HPP
- //DeckOfCards.cpp code
- #include "DeckOfCards.hpp"
- DeckOfCards::DeckOfCards()
- :deck{initDeck()}
- {
- //empty body
- }
- std::array<Card, DeckOfCards::DECKMAX> DeckOfCards::initDeck() {
- std::array<Card, DECKMAX> newDeck{};
- for(size_t i{0}; i < Card::SUITMAX; i += 1) {
- for(size_t j{0}; j < Card::FACEMAX; j += 1) {
- Card myCard{Card::faces[j], Card::suits[i]};
- newDeck[i * Card::FACEMAX + j] = myCard;
- }
- }
- return newDeck;
- }
- void DeckOfCards::printDeck() const{
- size_t i{0};
- do {
- std::cout << std::left << std::setw(20)<< deck[i];
- ++i;
- std::cout << std::setw(20)<< deck[i];
- ++i;
- std::cout << std::setw(20)<< deck[i];
- ++i;
- std::cout << std::setw(20)<< deck[i] << std::endl;
- ++i;
- }while (i < DECKMAX);
- }
- void DeckOfCards::shuffle() {
- int position{};
- Card temp{};
- int i{0};
- for (auto item : deck){
- position = rand() % 52;
- temp = deck[position];
- deck[position] = item;
- deck[i] = temp;
- ++i;
- }
- }
- bool DeckOfCards::validateDeck() {
- bool isValid{true};
- for(size_t i{0}; i < DECKMAX; i += 1){
- for(size_t j{0}; j < DECKMAX; j += 1){
- if (i != j) {
- if (deck[i] == deck[j]) {
- std::cout << deck[i] << " in position " << i << " "<< deck[j] << " in position " << j;
- isValid = false;
- }
- }
- else{};
- }
- }
- return isValid;
- }
- int DeckOfCards::getSize() {
- return deck.size();}
- Card DeckOfCards::dealCard() {
- Card deal{deck[currentCard]};
- ++currentCard;
- return deal;
- }
- std::array<Card, DeckOfCards::HANDMAX> DeckOfCards::dealHand() {
- std::array<Card, HANDMAX> myHand{};
- for (size_t i{0}; i < HANDMAX; i += 1){
- myHand[i] = dealCard();
- }
- return myHand;
- }
- bool DeckOfCards::moreCards() {
- return (currentCard != DECKMAX);}
- int DeckOfCards::cardsRemaining() {
- return DECKMAX - currentCard;
- }
- //main.cpp code (includes test cases and hand evaluation methods)
- #include <iostream>
- #include "Card.hpp"
- #include "DeckOfCards.hpp"
- using namespace std;
- const size_t HANDMAX{5};
- bool isPair(const array<Card, HANDMAX>& hand);
- bool isTwoPair(const array<Card, HANDMAX>& hand);
- bool isThreeOfAKind(const array<Card, HANDMAX>& hand);
- bool isFourOfAKind(const array<Card, HANDMAX>& hand);
- bool isFlush(const array<Card, HANDMAX>& hand);
- void sortHand(array<Card, HANDMAX>& hand);
- bool isStraight(const array<Card, HANDMAX>& hand);
- int main()
- {
- Card card1{Face::JACK, Suit::CLUBS};
- Card card2{Face::KING, Suit::CLUBS};
- Card card3{Face::QUEEN, Suit::CLUBS};
- Card card4{Face::TEN, Suit::CLUBS};
- Card card5{Face::ACE, Suit::CLUBS};
- DeckOfCards deck{};
- deck.printDeck();
- cout << "Are there 52 unique cards in the deck? " << boolalpha << deck.validateDeck() << deck.getSize() << endl;
- deck.shuffle();
- cout << "\n\n\n" << endl;
- deck.printDeck();
- cout << endl;
- cout << "Are there 52 unique cards in the deck after shuffling? " << deck.validateDeck() << " " << deck.getSize() << endl;
- array<Card, HANDMAX> playerHand{deck.dealHand()};
- // for (size_t i{0}; i < HANDMAX; i++){
- // cout << playerHand[i] << endl;
- // }
- // cout << isPair(playerHand);
- playerHand[0] = card1;
- playerHand[1] = card2;
- playerHand[2] = card3;
- playerHand[3] = card4;
- playerHand[4] = card5;
- for (size_t i{0}; i < HANDMAX; i++){
- cout << playerHand[i] << endl;
- }
- sortHand(playerHand);
- for (size_t i{0}; i < HANDMAX; i++){
- cout << playerHand[i] << endl;
- }
- // cout << isFlush(playerHand);
- // cout << isStraight(playerHand);
- // cout << isFourOfAKind(playerHand);
- // cout << isTwoPair(playerHand);
- // cout << isThreeOfAKind(playerHand);
- // cout << "Test dealing all cards and tracking the status of the deck: \n";
- // for (int i{0}; i < 52; i++){
- // cout << "Player is dealt: " << deck.dealCard();
- // cout << " Cards still in deck: " << deck.cardsRemaining();
- // cout << " Deck contains cards? " << deck.moreCards() << endl;
- // }
- }
- bool isPair(const array<Card, HANDMAX>& hand){
- int sameFace{0};
- Face kind{};
- for(size_t i{0}; i < HANDMAX; i += 1) {
- for(size_t j{i+1}; j < HANDMAX; j += 1) {
- if (hand[i].getFace() == hand[j].getFace()){
- kind = hand[i].getFace();
- }
- }
- }
- for(size_t k{0}; k < HANDMAX; k += 1) {
- if (kind == hand[k].getFace()){
- sameFace += 1;
- }
- }
- return (sameFace == 2);
- }
- bool isTwoPair(const array<Card, HANDMAX>& hand){
- Face kind{};
- Face kind2{};
- for(size_t i{0}; i < HANDMAX; i += 1) {
- for(size_t j{i+1}; j < HANDMAX; j += 1) {
- if ((hand[i].getFace() == hand[j].getFace()) && (kind == kind2)){
- kind = hand[i].getFace();
- }
- if ((hand[i].getFace() == hand[j].getFace()) && (hand[i].getFace() != kind)){
- kind2 = hand[i].getFace();
- }
- }
- }
- return (kind != kind2);
- }
- bool isThreeOfAKind(const array<Card, HANDMAX>& hand){
- int sameFace{0};
- Face kind{};
- for(size_t i{0}; i < HANDMAX; i += 1) {
- for(size_t j{i+1}; j < HANDMAX; j += 1) {
- if (hand[i].getFace() == hand[j].getFace()){
- kind = hand[i].getFace();
- }
- }
- }
- for(size_t k{0}; k < HANDMAX; k += 1) {
- if (kind == hand[k].getFace()){
- sameFace += 1;
- }
- }
- return (sameFace == 3);
- }
- bool isFourOfAKind(const array<Card, HANDMAX>& hand){
- int sameFace{0};
- Face kind{};
- for(size_t i{0}; i < HANDMAX; i += 1) {
- for(size_t j{i+1}; j < HANDMAX; j += 1) {
- if (hand[i].getFace() == hand[j].getFace()){
- kind = hand[i].getFace();
- }
- }
- }
- for(size_t k{0}; k < HANDMAX; k += 1) {
- if (kind == hand[k].getFace()){
- sameFace += 1;
- }
- }
- return (sameFace == 4);
- }
- bool isFlush(const array<Card, HANDMAX>& hand) {
- Suit suit{};
- int suitCount{0};
- for(size_t i{0}; i < HANDMAX; i += 1) {
- for(size_t j{i+1}; j < HANDMAX; j += 1) {
- if (hand[i].getSuit() == hand[j].getSuit()){
- suit = hand[i].getSuit();
- }
- }
- }
- for (size_t i{0}; i < HANDMAX; i += 1){
- if (hand[i].getSuit() == suit) {
- suitCount += 1;
- }
- }
- return (suitCount == 5);
- }
- void sortHand(array<Card,HANDMAX>& hand){
- for(size_t i{0}; i < HANDMAX; i += 1){
- for(size_t j{i+1}; j < HANDMAX; j += 1){
- if (hand[j].getFace() < hand[i].getFace()){
- Card temp{hand[j]};
- hand[j] = hand[i];
- hand[i] = temp;
- }
- }
- }
- }
- //I dont think this straight code does what it should, but it runs.
- bool isStraight(const array<Card, HANDMAX>& hand) {
- Card currentCard{hand[0]};
- bool straight{true};
- for(size_t i{0}; i < HANDMAX; i += 1){
- if ((currentCard.getFace()) != (hand[i].getFace())){
- straight = false;
- }
- }
- return straight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement