This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcard.cpp
86 lines (72 loc) · 2.01 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
#include "card.h"
int iCard::compare(const iCard* first, const iCard* second) {
return (global::iranks[first->_rank] - global::iranks[second->_rank]);
}
iCard::iCard(std::string suit, std::string rank) {
this->_rank = rank; this->_suit = suit;
}
std::string iCard::rank(void) const {
return this->_rank;
}
std::string iCard::suit(void) const {
return this->_suit;
}
bool iCard::operator>(const iCard*& card) {
int diff = this->compare(this, card);
if (diff > 0) return true;
return false;
}
bool iCard::operator<(const iCard*& card) {
int diff = this->compare(this, card);
if (diff < 0) return true;
return false;
}
bool iCard::operator>=(const iCard*& card) {
int diff = this->compare(this, card);
if (diff >= 0) return true;
return false;
}
bool iCard::operator<=(const iCard*& card) {
int diff = this->compare(this, card);
if (diff <= 0) return true;
return false;
}
bool iCard::operator==(const iCard*& card) {
int diff = this->compare(this, card);
if (diff == 0) return true;
return false;
}
std::ostream& operator<<(std::ostream& out, const iCard& card) {
out << card._suit << ' ' << card._rank;
return out;
}
CardManager::CardManager(void) {
for (auto& suit : global::suits) {
for (auto& rank : global::ranks) {
iCard* card = new iCard(suit, rank);
auto key = std::make_pair(suit, rank);
this->library[key] = card;
this->all.push_back(card);
}
}
}
// Free all memory
CardManager::~CardManager(void) {
for (auto& card : this->all)
delete card;
}
// Get copy for all cards
std::vector<iCard*> CardManager::getall(void) {
return this->all;
}
// Get the memory reference of the specified card
iCard* CardManager::get(std::string& suit, std::string& rank) {
auto key = std::make_pair(suit, rank);
// If found card
if (this->library.find(key) != this->library.end())
return this->library[key];
return nullptr;
}
iCard* CardManager::get(std::pair<std::string, std::string>& key) {
return this->library[key];
}