-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.cpp
204 lines (165 loc) · 4.86 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "card.h"
#include <QString>
#include <QStringRef>
#include <iostream>
#include "exception.h"
const unsigned int Card::min_value(2);
const unsigned int Card::max_value(14);
const unsigned int Card::defaultValue(15);
const QChar Card::Ten('T');
const QChar Card::Jack('J');
const QChar Card::Queen('Q');
const QChar Card::King('K');
const QChar Card::Ace('A');
const unsigned int Card::tenInt(10);
const unsigned int Card::jackInt(11);
const unsigned int Card::queenInt(12);
const unsigned int Card::kingInt(13);
const unsigned int Card::aceInt(14);
const QString Card::JackString("jack");
const QString Card::QueenString("queen");
const QString Card::KingString("king");
const QString Card::AceString("ace");
const QString Card::SpadesString("spades");
const QString Card::ClubsString("clubs");
const QString Card::HeartsString("hearts");
const QString Card::DiamondsString("diamonds");
const QChar Card::SpadesColor('s');
const QChar Card::ClubsColor('c');
const QChar Card::HeartsColor('h');
const QChar Card::DiamondsColor('d');
const QChar Card::defaultColor('u');
const QChar Card::Colors[4] = {Card::SpadesColor, Card::ClubsColor, Card::HeartsColor, Card::DiamondsColor};
Card::Card(unsigned int val, QChar col):
value(val),
color(col),
CardException(Exception("Card")) {
}
Card::Card(Card const& c):
value(c.getValue()),
color(c.getColor()),
CardException(Exception("Card")) {
}
bool Card::operator==(const Card &c) const {
return this->getColor() == c.getColor() &&
this->getValue() == c.getValue();
}
bool Card::operator!=(const Card &c) const {
return !(*this == c);
}
Card::~Card() {
}
unsigned int Card::getValue() const {
return value;
}
QChar Card::getColor() const {
return color;
}
void Card::setValue(const int val) {
value = val;
}
void Card::setColor(const QChar col) {
color = col;
}
bool Card::setCard(const Card c) {
if(c.getValue() == Card::defaultValue || c.getColor() == Card::defaultColor) {
CardException.throwException("setCard(const Card c)", "Something wrong, setting card to default values");
return false;
}
setValue(c.getValue());
setColor(c.getColor());
if(getValue() != c.getValue() || getColor() != c.getColor()) {
CardException.throwException("setCard(const Card c)", "Card hasn't been correctly set");
return false;
}
return true;
}
bool Card::setCard(const QString title) {
Card MyCard = getCard(title);
return this->setCard(MyCard);
}
Card Card::getCard(const QString title) const {
if(title.length() != 2) CardException.throwException("getCard(QString title)", "Invalid title : size != 2");
QString::const_iterator i(title.begin());
Card MyCard = Card();
if((*i).isDigit()) MyCard.setValue(title.left(1).toInt());
else if (*i == Ten) MyCard.setValue(tenInt);
else if (*i == Jack) MyCard.setValue(jackInt);
else if (*i == Queen) MyCard.setValue(queenInt);
else if (*i == King) MyCard.setValue(kingInt);
else if (*i == Ace) MyCard.setValue(aceInt);
else CardException.throwException("getCard(QString title)", "Invalid title begin : not known value");
i++;
if (*i != SpadesColor && *i != ClubsColor && *i != HeartsColor && *i != DiamondsColor)
CardException.throwException("getCard(QString title)", "Invalid title end : not know color");
MyCard.setColor(*i);
return MyCard;
}
QString Card::getStyleSheet() const {
//background-image: url(:/10_of_diamonds.png);
QString result = "_of_";
if (getColor() == DiamondsColor) result.append(DiamondsString);
else if (getColor() == SpadesColor) result.append(SpadesString);
else if (getColor() == HeartsColor) result.append(HeartsString);
else if (getColor() == ClubsColor) result.append(ClubsString);
else CardException.throwException("getStyleSheet()", "Invalid color");
unsigned int val = getValue();
if (val <= tenInt) result.prepend(QString::number(val));
else {
switch(val) {
case jackInt:
result.prepend(JackString);
break;
case queenInt:
result.prepend(QueenString);
break;
case kingInt:
result.prepend(KingString);
break;
case aceInt:
result.prepend(AceString);
break;
default:
CardException.throwException("getStyleSheet()", "Invalid value");
break;
}
}
result.prepend(QString("background-image: url(:/"));
result.append(QString(".png);"));
return result;
}
QString Card::getStringValue() const {
unsigned int val = getValue();
QString result;
if (val < tenInt) {
result.append(QString::number(val));
}
else {
switch(val) {
case tenInt:
result.append(Ten);
break;
case jackInt:
result.append(Jack);
break;
case queenInt:
result.append(Queen);
break;
case kingInt:
result.append(King);
break;
case aceInt:
result.append(Ace);
break;
case defaultValue:
break;
default:
CardException.throwException("getStringValue()", "Invalid value");
break;
}
}
return result;
}
QString Card::getTitle() const {
return getStringValue().append(getColor());
}