C++ programming! A deck of cards problem! Please help me out Write the member fu
ID: 3776580 • Letter: C
Question
C++ programming! A deck of cards problem! Please help me out
Write the member function implementations for the class hand, which simulates a hand of 2 cards, into the file hand.cpp. The relative strength of 2 hands arc determined by the following rules: A pair (two cards of the same number) is the strongest hand. Two cards of the same suit is the next strongest hand. Two cards of different numbers and suits is the weakest hand. Within the same kind of hands, the stronger hand is determined by the larger number. If two hands arc of the same kind and largest numbers are the same, the stronger hand is determined by the smaller number. If all above fails, the two hands arc of equal strength. I.e., all suits arc of equal strength. 2 is the weakest number. An Ace is stronger than a King. You are using a single deck of cards. So a hand of AV A V is impossible. Here arc some examples: 3 3 is stronger than 9 2. 9 2 is stronger than 8 7 is stronger than 8 8. 8 6 is stronger than A .J. 3 3 has the same strength as 3 3. The public data members card cl, c2; represent the 2 cards of a hand. The public member function bool operator (hand rhs); bool operator = (hand rhs); are defined similarly.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 52;
class card
{
public:
card();
card(string cardFace, string cardSuit);
string print();
private:
string face;
string suit;
};
class deckOfCards
{
public:
deckOfCards();
void shuffle();
card dealCard();
private:
card deck[SIZE]; // an array of cards of size SIZR
int currentCard;
};
int main()
{
deckOfCards deck;
deck.shuffle();
for( int i = 0; i <= 2; i++)
{
currentCard = deck.dealCard();
cout << currentCard.print() << endl;
}
return 0;
}
card::card()
{
}
card::card(string cardFace, string cardSuit)
{
face = cardFace;
suit = cardSuit;
}
string card::print()
{
return (face + " of " + suit);
}
deckOfCards::deckOfCards()
{
string faces[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new card[SIZE];
currentCard = 0;
for(int count = 0; count < SIZE; count++)
{
deck[count] = card(faces[count % 13], suits[count / 13]);
}
}
void deckOfCards::shuffle()
{
currentCard = 0;
for(int first = 0; first < SIZE; first++)
{
int second = (rand() + time(0)) % SIZE;
card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
card deckOfCards::dealCard()
{
if(currentCard > SIZE)
{
shuffle();
}
if( currentCard < SIZE)
{
return (deck[currentCard++]);
}
return (deck[0]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.