Description (It has to work with c++) The purpose of this challenge is to define
ID: 3713263 • Letter: D
Question
Description (It has to work with c++)
The purpose of this challenge is to define several aggregated classes with overloaded operators. This challenge simulates playing cards and a modified poker game.
Requirements
Define a class called Card as below. Complete the as_string() function and the overloaded operators as described in the comments below.
This is what I need to check to see if is working thank you .
To test your code, write a main function that will instantiate 2 or three Cards with various values. For example:
int main()
{
Card test1(1,1);
Card test2(2,1);
cout << test1; // should show "Ace of Diamonds"
if (test2 <= test1)
cout << test1 << " beats " << test2 << endl;
}
Explanation / Answer
Here is the required code to make your program work. The less than or equal to operator is defined to compare two cards by values, giving a special consideration for Ace. There is also another code provided in case if the suit values are also included while comparing. (In comments). Thanks.
//code.cpp
#include<iostream>
#include<sstream>
using namespace std;
class Card
{
private:
// suit contains values 1-4, to represent suits below
// 1 - Diamonds, 2 - Hearts, 3 - Clubs, 4 - Spades
int suit;
// value contains values 1 - 13
// Ace - 1, 2-10, J - 11, Q - 12, K - 13
int value;
public:
// set values
void set(int s, int v)
{
suit = s;
value = v;
}
//constructor to set both values
Card(int s, int v){
suit = s;
value = v;
}
// function to return a string that describes the card
string as_string()
{
string suitValue;
string txtValue;
//finding the value in suit
if(value==1){
txtValue="Ace";
}else if(value==11){
txtValue="Jack";
}else if(value==12){
txtValue="Queen";
}else if(value==13){
txtValue="King";
}else{
//converting value into a string
stringstream ss;
ss << value;
txtValue=ss.str();
}
//finding the suit name
switch(suit){
case 1: suitValue="Diamonds";
break;
case 2: suitValue="Hearts";
break;
case 3: suitValue="Clubs";
break;
case 4: suitValue="Spades";
break;
}
//returning a concatenated string containing both values
return txtValue+" of "+suitValue;
// for example, if suit == 1 and value == 1
// this function would return "Ace of Diamonds"
}
// an overloaded operator for less-than-or-equal-to
// this function allows you to compare two cards.
// despite an Ace having a value of 1, it is
// actually greater in value than all the other cards
bool operator <=(const Card c){
/*
//assuming suit values are arranged in proper ranks
//i.e assuming diamond has the highest rank (1), followed by hearts(2),
//clubs(3) and finally spades(4).
if(suit>c.suit){
//c has higher rank (lower suit value)
return true;
}else if(suit<c.suit){
//c has lower rank (higher suit value)
return false;
}else{
//suits are equal, so we compare by values
if(value==1){
//current card is an ace
if(c.value==1){
//the other card is also ace, so ranks are equal
return true;
}else{
//ace has the highest rank, so any other values will be of
//low rank
return false;
}
}else{
if(c.value==1){
//the other card is ace, so it is greater
return true;
}else{
//now comparing by ascending order of the value
if(value>c.value){
return false;
}else{
return true;
}
}
}
}
return false; */
/*if you just want to compare cards by value, use this code, or else
uncomment the above code*/
if(value==1){
//current card is an ace
if(c.value==1){
//the other card is also ace, so ranks are equal
return true;
}else{
//ace has the highest rank, so any other values will be of
//low rank
return false;
}
}else{
if(c.value==1){
//the other card is ace, so it is greater
return true;
}else{
//now comparing by ascending order of the value
if(value>c.value){
return false;
}else{
return true;
}
}
}
}
};
/*method to overload << operator to display a card info*/
ostream& operator<<(ostream& os, const Card& c) {
os << c.as_string();
return os;
}
int main()
{
//demonstrating the class methods
Card test1(1,5);
Card test2(2,1);
Card test3(4,13);
cout<<test1<<endl; // should show "Ace of Diamonds"
cout<<test2<<endl;
cout<<test3<<endl;
if (test2 <= test1)
cout << test1 << " beats " << test2 << endl;
else
cout << test2 << " beats " << test1 << endl;
}
//output
5 of Diamonds
Ace of Hearts
King of Spades
Ace of Hearts beats 5 of Diamonds