Question
void card:: setNum(int n) {
assert(n >= 1 && n <= 13);
num = n;
}
void card::setSuit(char s) {
assert(s == 'C' || s == 'D' || s == 'H' || s == 'S');
suit = s;
}
int card::getNum() {
assert(num >= 1 && num <= 13);
return num;
}
char card::getSuit() {
assert(suit == 'C' || suit == 'D' || suit == 'H' || suit == 'S');
return suit;
}
string card:: read() {
string output="";
if (num == 1) {
output= "Ace";
}
else if (num == 11) {
output = "Jack";
}
else if (num == 12) {
output = "Queen";
}
else if (num == 13) {
output = "King";
}
else {
output = getNum();
}
output += " of ";
if (suit == 'C') {
output += "Clubs";
}
else if (suit == 'D') {
output += "Diamonds";
}
else if (suit == 'H') {
output += "Hearts";
}
else if (suit == 'S') {
output += "Spades";
}
return output;
}
card:: card(){
num = 0;
suit = 'X';
}
card::card(int n, char s) {
setNum(n);
setSuit(s);
}
Write C++ function implementations.
Problem 3: (Hand) Write the member function implem
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete Program:

NOTE:
Please include the card.h, card.cpp, deck.h, deck.cpp, shuffle.h, and main.cpp files to run this program.

Note: Please include the card.h, card.cpp, deck.h, deck.cpp, shuffle.h, and main.cpp files to run this program.
Then run the program.

Sample Output:


CODE TO COPY:


File: hand.h

// hand class specification
#pragma once
#include "card.h"

class hand
{
public:
   card c1, c2;
   bool operator<(hand rhs);
   bool operator>(hand rhs);
   bool operator<=(hand rhs);
   bool operator>=(hand rhs);
   bool operator==(hand rhs);
}; // end of hand.h


File: hand.cpp

// deck class implementation
#include "hand.h"
#include <cassert>

// operator< implementation
bool hand::operator<(hand rhs)
{
   int lhsc1n = this->c1.getNum();
   int lhsc2n = this->c2.getNum();
   char lhsc1s = this->c1.getSuit();
   char lhsc2s = this->c2.getSuit();

   int rhsc1n = rhs.c1.getNum();
   int rhsc2n = rhs.c2.getNum();
   char rhsc1s = rhs.c1.getSuit();
   char rhsc2s = rhs.c2.getSuit();

   // make the Ace as the largest number
   if (lhsc1n == 1)
       lhsc1n = 14;

   if (lhsc2n == 1)
       lhsc2n = 14;

   if (rhsc1n == 1)
       rhsc1n = 14;

   if (rhsc2n == 1)
       rhsc2n = 14;
  

   // A pair (two cards of the same number) is the strongest hand.
   if (lhsc1n != lhsc2n && rhsc1n == rhsc2n)
   {
       return true;
   }
  
   if (lhsc1n == lhsc2n && rhsc1n != rhsc2n)
   {
       return false;
   }

   // Two cards of the same suit is the next strongest hand.
   if (lhsc1s != lhsc2s && rhsc1s == rhsc2s)
   {
       return true;
   }
  
   if (lhsc1s == lhsc2s && rhsc1s != rhsc2s)
   {
       return false;
   }

   // Two cards of different numbers and suits is the weakest hand.
   if ((lhsc1n != lhsc2n && lhsc1s != lhsc2s)
       && (rhsc1n == rhsc2n || rhsc1s == rhsc2s))
   {
       return true;
   }
  
   if ((lhsc1n == lhsc2n || lhsc1s == lhsc2s)
       && (rhsc1n != rhsc2n && rhsc1s != rhsc2s))
   {
       return false;
   }

   // Within the same kind of hands, the stronger hand is
   // determined by the larger number.
   if (lhsc1s == lhsc2s && rhsc1s == rhsc2s)
   {
       if ((lhsc1n > rhsc1n && lhsc1n > rhsc2n)
           || (lhsc2n > rhsc1n && lhsc2n > rhsc2n))
       {
           return false;
       }
       else if ((lhsc1n < rhsc1n && lhsc2n < rhsc1n)
           || (lhsc1n < rhsc2n && lhsc2n < rhsc2n))
       {
           return true;
       }
   }

   // If two hands are of the same kind and the larger
   // numbers are the same, the stronger hand is given by
   // the hand whose smaller number is larger.
   if (lhsc1s == lhsc2s && rhsc1s == rhsc2s)
   {
       int larger1, larger2;
       int smaller1, smaller2;

       if (lhsc1n >= lhsc2n)
       {
           larger1 = lhsc1n;
           smaller1 = lhsc2n;
       }
       else
       {
           larger1 = lhsc2n;
           smaller1 = lhsc1n;
       }

       if (rhsc1n >= rhsc2n)
       {
           larger2 = rhsc1n;
           smaller2 = rhsc2n;
       }
       else
       {
           larger2 = rhsc2n;
           smaller2 = rhsc1n;
       }

       if (larger1 == larger2)
       {
           if (smaller1 < smaller2)
               return true;
           else
               return false;
       }
   }

   // If all above fails, the two hands are of equal strength.
   // i.e., all suits are of equal strength.
   return false;

}

// operator== implementation
bool hand::operator==(hand rhs)
{
   return !(*this < rhs || rhs < *this);
}

// operator<= implementation
bool hand::operator<=(hand rhs)
{
   return !(rhs < *this);
}

// operator> implementation
bool hand::operator>(hand rhs)
{
   return !(*this <= rhs);
}

// operator>= implementation
bool hand::operator>=(hand rhs)
{
   return !(*this < rhs);
}

Add a comment
Know the answer?
Add Answer to:
void card:: setNum(int n) { assert(n >= 1 && n <= 13); num = n; }...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • python . Write the function poker_hand that takes a list of exactly five distinct Card objects...

    python . Write the function poker_hand that takes a list of exactly five distinct Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand: "Four of a kind' (four cards of the same rank) 'Full house' (three cards of one rank, and two cards of a different rank) . 'Flush' (five cards of the same suit) 'Three of a kind' (exactly three cards of the same rank) • 'One pair' (at...

  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (sy...

    A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (symbols A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, and K). Every card in the deck has both a value and a suit.1 A poker hand is any set of 5 cards from the standard poker deck. There are some special hands in poker, and these have ranks (i.e. some are better, some are worse). From best...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games...

    Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT