Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3021

Deck of Cards Game Help

$
0
0
Write a program that will operate on a deck of 52 cards. Your program will:
1. Deal them into four random hands of 5 cards each
a. The four hands need to be kept in an array for later use.
2. Sort each hand so that it shows the cards in sequence from two as the lowest to ace as the highest.
3. Display the cards in each hand using the card face (2, 10, King, etc.) and the suit (Spades, Hearts, etc.)
4. Display what you determined the hand to be. A ranking of poker hands is at http://www.pagat.com/vying/pokerrank.html
a. Straight flush
b. Four of a kind
c. Full House
d. Flush
e. Straight
f. Three of a kind
g. Two pair
h. One pair
i. Highest card
5. Each time the program is run, a different set of hands is to be dealt.

BONUS: Determine which is the winning hand. A tie is possible. (25 points)

Card.cpp
Code:


#include <iostream>

using namespace std;

#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "Card .h"

char * ValueNames[13] = {
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Jack",
        "Queen",
        "King",
        "Ace"
};

/*enum hand_kind
{
        HIGH_CARD,
        ONE_PAIR,
        TWO_PAIR,
        THREE_OF_A_KIND,
        STRAIGHT,
        FLUSH,
        FULL_HOUSE,
        FOUR_OF_A_KIND,
        STRAIGHT_FLUSH,

};
*/
void Display(const Card & C)
{
        cout << (char)C.Suit << ' ' << ValueNames[C.Value] << endl;
}

void InitDeck(Card Deck[])
{
        int                i;
        Suits        S;
        Values        V;

        srand(time(0));        // needed this to initialize the random numbers for use later in shuffling
        i = 0;
        for (S = Hearts; S <= Spades; S = (Suits)(S + 1))
                for (V = Two; V <= Ace; V = (Values)(V + 1))
                {
                        Deck[i].Suit = S;
                        Deck[i].Value = V;
                        i++;
                }
}

void SortHand(Card Hand[])
{
        int                i;
        int                j;
        int                CardOne;
        int                CardTwo;
        Card        Temp;



        for (j = 0; j < 5; j++)
        {
                CardOne = 0;
                CardTwo = 1;
                for (i = 0; i <= 4; i++)
                {
                        if (Hand[CardOne].Value > Hand[CardTwo].Value)
                        {
                                Temp = Hand[CardOne];
                                Hand[CardOne] = Hand[CardTwo];
                                Hand[CardTwo] = Temp;
                        }
                        else;

                        CardOne++;
                        CardTwo++;
                        i++;
                }
        }
        if (Hand[3].Value > Hand[4].Value)
        {
                Temp = Hand[3];
                Hand[3] = Hand[4];
                Hand[4] = Temp;
        }
        if (Hand[2].Value > Hand[3].Value)
        {
                Temp = Hand[2];
                Hand[2] = Hand[3];
                Hand[3] = Temp;
        }

        if (Hand[1].Value > Hand[2].Value)
        {
                Temp = Hand[1];
                Hand[1] = Hand[2];
                Hand[2] = Temp;
        }
        if (Hand[0].Value > Hand[1].Value)
        {
                Temp = Hand[0];
                Hand[0] = Hand[1];
                Hand[1] = Temp;
        }

}

void ShowDeck(const Card Deck[])
{
        int                i;

        for (i = 0; i < NumCardsInDeck; i++)
                Display(Deck[i]);
}

void ShowHand(Card Deck[])
{
        int                i;
        for (i = 0; i < 5; i++)
                Display(Deck[i]);
}
/*
bool Hand::is_straight_flush ()

if (! (is_flush () && is_straight()))
return false;
m_high_rank = find_high_rank();
return true;
}
*/
void Shuffle(Card Deck[])
{
        int                CardOne;
        int                CardTwo;
        Card        Temp;

        for (CardOne = 0; CardOne <NumCardsInDeck; CardOne++)
        {
                CardTwo = rand() % NumCardsInDeck;
                Temp = Deck[CardOne];
                Deck[CardOne] = Deck[CardTwo];
                Deck[CardTwo] = Temp;
        }
}

void Deal(Card Deck[])
{
        int                CardOne;
        int                CardTwo;
        Card        Temp;

        for (CardOne = 0; CardOne < 5; CardOne++)
        {
                CardTwo = rand() % 5;
                Temp = Deck[CardOne];
                Deck[CardOne] = Deck[CardTwo];
                Deck[CardTwo] = Temp;
        }
}

bool        HighestCard(Card Hand[])
{
        if (Hand[0].Value != Hand[1].Value && Hand[0].Value != Hand[2].Value && Hand[0].Value != Hand[3].Value
                && Hand[0].Value != Hand[4].Value && Hand[1].Value != Hand[2].Value && Hand[1].Value != Hand[3].Value &&
                Hand[1].Value != Hand[4].Value && Hand[2].Value != Hand[3].Value && Hand[2].Value != Hand[4].Value && Hand[3].Value != Hand[4].Value)
        {
                return true;
                cout << "The High Card " << endl;
        }
        else
                return false;
}

bool        OnePair(Card Hand[])
{
        if ((Hand[0].Value == Hand[1].Value && Hand[0].Value != Hand[2].Value) ||
                (Hand[1].Value == Hand[2].Value && Hand[1].Value != Hand[3].Value) ||
                (Hand[2].Value == Hand[3].Value && Hand[2].Value != Hand[4].Value) ||
                Hand[3].Value == Hand[4].Value)
        {
                return true;
        }
        else
                return false;
}

bool IsThreeOfAKind(Card Hand[])
{
        if ((Hand[0].Value == Hand[2].Value) || (Hand[1].Value == Hand[3].Value) || (Hand[2].Value == Hand[4].Value))
        {
                return true;
                cout << "Three of a Kind" << endl;
        }
        else
                return false;
}



// Need to input what the deck is here
// and compare which is the best hand

Main.cpp
Code:


#include <iostream>

using namespace std;

#include "Card .h"

void main()
{
        /*        Card        C; -- Dont need this, get rid of it.

        C.Suit        = Diamonds;
        C.Value        = Queen;

        Display (C);
        */

        Card        Deck[NumCardsInDeck];
        Card        Hands[4][5];
        int                i, j;

        InitDeck(Deck);
        cout << "\t The deck starts as " << endl;
        ShowDeck(Deck);
        Shuffle(Deck);
        cout << "\t After shuffling it is" << endl;
        ShowDeck(Deck);

        j = 0;

        for (i = 0; i < 5; i++)
        {
                Hands[0][i] = Deck[j];
                j++;
        }
        SortHand(Hands[0]);
        cout << "Hand 1 is: " << endl;
        if (HighestCard(Hands[0]))
                cout << "\tHighest Card" << endl;
        if (OnePair(Hands[0]))
                cout << "\tOne Pair" << endl;
        if (IsThreeOfAKind(Hands[0]))
                cout << "\tThree of a kind" << endl;

        for (i = 0; i < 5; i++)
        {
                Display(Hands[0][i]);
        }



        for (i = 0; i < 5; i++)
        {
                Hands[1][i] = Deck[j];
                j++;
        }
        SortHand(Hands[1]);
        cout << "Hand 2 is: " << endl;
        for (i = 0; i < 5; i++)
        {
                Display(Hands[1][i]);
        }

        for (i = 0; i < 5; i++)
        {
                Hands[2][i] = Deck[j];
                j++;
        }
        SortHand(Hands[2]);
        cout << "Hand 3 is: " << endl;
        for (i = 0; i < 5; i++)
        {
                Display(Hands[2][i]);
        }


        for (i = 0; i < 5; i++)
        {
                Hands[3][i] = Deck[j];
                j++;
        }
        SortHand(Hands[3]);
        cout << "Hand 4 is: " << endl;
        for (i = 0; i < 5; i++)
        {
                Display(Hands[3][i]);
        }
        /*
        for (i = 0; i < 4; i++)
        Sort (Hands [i]);

        if (IsThreeOfAKind (Hands [0]))
        */

       
}
// Code should function as you have written it, might want to use a reset function so that you dont have to compile and re run everyt time to change hands.
// as far as the Bonus, you should make make the deck of cards have a value, highest value trumps lowest value.

card.h

Code:



#ifndef CARD_H
#define CARD_H

enum Suits { Hearts = 3, Diamonds, Clubs, Spades };
enum Values { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };

const int NumCardsInDeck(52);

struct Card
{
        Suits        Suit;
        Values        Value;
};

void Display(const Card &);        // Structures are normally passed by reference because it is more efficient while usisng large blocks of data.
                                                        // Since we are not going to change the values in Card, make it it is a const to be safer.
void InitDeck(Card[]);
void SortHand(Card[]);
void ShowDeck(const Card[]);
void ShowHand(Card[]);
void Shuffle(Card[]);
void Deal(Card[]);
bool HighestCard(Card[]);
bool OnePair(Card[]);
bool IsThreeOfAKind(Card[]);




#endif


Ok, so this is what i have gathered so far, just having difficulties with #4 and the bonus. Can anyone lend any suggestions to help finish this?

Thanks again!

Viewing all articles
Browse latest Browse all 3021

Trending Articles