-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.cpp
92 lines (77 loc) · 1.71 KB
/
Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "headers/Card.h"
Card::Card() {}
Card::Card(int cardInit, Suit suitInit) {
this->suit = suitInit;
this->cardinalValue = cardInit;
}
/*
Gets the cardinal number of the card.
Jack, Queen, and King are assigned the value of 10
Ace is assigned the value 1 and 11
*/
int Card::getCardinal(bool getValue) {
if (getValue && this->cardinalValue > 10) return 10;
return this->cardinalValue;
}
/*
Returns the suit of the card.
Suites are Diamond, Hearts, Clubs, or Spades, all in one alphabetical value.
*/
Card::Suit Card::getSuit() {
return this->suit;
}
/*
Prints out the value of the card as follows:
-> 4D
The first number corresponds to the cardinal value, and the second value corresponds
to the suit.
*/
string Card::print() {
string result = "";
int cardinal = this->getCardinal(false);
Card::Suit suit = this->getSuit();
result = this->_convertCardinal(cardinal);
//result += " of ";
result += this->_convertSuit(suit);
cout << result;
return result;
}
/*
Helper function that converts face cards into their appropriate cardinal values
*/
string Card::_convertCardinal(int cardinal) {
string result = "";
if (cardinal > 10) {
switch (cardinal) {
case 11: result += "J";
break;
case 12: result += "Q";
break;
case 13: result += "K";
break;
default: break;
}
}
else if (cardinal == 1) {
result += "A";
}
else {
ostringstream convert;
convert << cardinal;
result += convert.str();
}
return result;
}
/*
Given a Suit, it will convert it from a Suit to a string representation
*/
string Card::_convertSuit(Suit suit) {
static string Suits[4] = {
"D", "H", "C", "S"
};
return Suits[suit];
}