Please provide full answer with comments better to understand it is just beginni
ID: 3844885 • Letter: P
Question
Please provide full answer with comments better to understand it is just beginning of c++ so please don't use advances teqniques. I will add screenshots off main.cpp,card.h, deck.h, hand.h, shuffle.h, and deck.cppWrite 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 are 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 determqined by the larger number. If two hands are 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 are of equal strength. Le, all suits are 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 A A is impossible. Here are some examples: 33 is stronger than 9A24 9A24 is stronger than 8 7 8 7 is stronger than 8 6 8 6 is stronger than A J 3 3A has the same strength as 3 3 The public data members card. ci c2 represent the 2 cards of a hand. The public member function bool operator (hand rhs) returns true if rhs is the stronger hand and returns false otherwise. The public member function bool operator hand. rhs) returns true if the two hands are of equal strength and returns false otherwise. The public member functions bool operator (hand rhs) bool operator hand rhs) bool operator hand rhs); are defined similarly. Hint. Using operator all other operators can be defined in one line. In doing so, you may find the this pointer useful.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
//No implementation for the class card is provided. hence Im simulating on my own.
class card
{
public:
int number;
char type;
//numbers : 2 to 10 then Jack = 11, Queen = 12, King = 13 and A = 1
//type : Diamond = 'D', Clover = 'C', Heart = 'H', Spades = 'S'
card()
{
}
card(int num, char cardtype)
{
number = num;
type = cardtype;
}
};
class hand
{
public:
card c1, c2;
bool strongHand = false;
bool operator<(hand rhs)
{
bool islhsSameNumber = this->c1.number == this->c2.number;
bool isrhsSameNumber = rhs.c1.number == rhs.c2.number;
bool islhsSameType = this->c1.type == this->c2.type;
bool isrhsSameType = rhs.c1.type == rhs.c2.type;
if(isrhsSameNumber && isrhsSameType)
{
if(!islhsSameNumber || !islhsSameType)
strongHand = true;
else
{
if(rhs.c1.number > this->c1.number)
strongHand = true;
}
}
else
{
int lhsLargest = this->c1.number > this->c2.number? this->c1.number : this->c2.number;
int rhsLargest = rhs.c1.number > rhs.c2.number? rhs.c1.number : rhs.c2.number;
int lhsSmallest = this->c1.number < this->c2.number? this->c1.number : this->c2.number;
int rhsSmallest = rhs.c1.number < rhs.c2.number? rhs.c1.number : rhs.c2.number;
if(rhsLargest > lhsLargest)
{
strongHand = true;
}
else if(rhsLargest == lhsLargest)
{
if(rhsSmallest > lhsSmallest)
strongHand = true;
}
}
return strongHand;
}
bool operator ==(hand rhs)
{
bool equalhand = false;
if((this->c1.number == this->c2.number) && (rhs.c1.number == rhs.c2.number))
{
equalhand = true;
}
return equalhand;
}
bool operator>(hand rhs)
{
if(!(this < rhs) && ! (this == rhs))
return true;
else return false;
}
bool operator<=(hand rhs)
{
if((this < rhs) || (this == rhs))
return true;
else return false;
}
bool operator>=(hand rhs)
{
if((this > rhs) || (this == rhs))
return true;
else return false;
}
};
int main() {
hand lhs, rhs;
lhs.c1 = card(3,'D');
lhs.c2 = card(3,'D');
rhs.c1 = card(3,'H');
rhs.c2 = card(3,'H');
bool lessthan = lhs<rhs;
bool greaterthan = lhs>rhs
bool lessthanorequal = lhs<=rhs;
bool greaterthanorequal = lhs>=rhs;
bool equalto = lhs==rhs;
cout<<"Less than: "<<lessthan<<endl;
cout<<"Greater than: "<<greaterthan<<endl;
cout<<"Less than or equal: "<<lessthanorequal<<endl;
cout<<"Greater than or equal: "<<greaterthanorequal<<endl;
cout<<"Equal: "<<equalto<<endl;
return 0;
}
OUTPUT:
Less than: 0
Greater than: 0
Less than or equal: 1
Greater than or equal: 1
Equal: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.