-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.cpp
More file actions
71 lines (63 loc) · 1.89 KB
/
Card.cpp
File metadata and controls
71 lines (63 loc) · 1.89 KB
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
#include "Card.h"
#include <stdexcept>
using namespace std;
// Constructor: initializes rank/suit using an initializer list.
Card::Card(int rank, Suit suit) : rank_(rank), suit_(suit) {
if (rank_ < 1 || rank_ > 13) {
// Throwing an exception makes the error obvious immediately,
// instead of letting invalid cards silently break the game.
throw std::invalid_argument("Card rank must be 1-13");
}
}
// Simple getters (const because they do not modify the Card).
int Card::rank() const {
return rank_;
}
Card::Suit Card::suit() const {
return suit_;
}
// Bài Cào scoring rule:
// A counts as 1, 2..9 count as their face value, 10/J/Q/K count as 0.
// (Total score in the game is usually sum % 10.)
int Card::caoValue() const {
if (rank_ == 1)
return 1;
if (rank_ >= 2 && rank_ <= 9)
return rank_;
return 0; // 10/J/Q/K
}
// Helper: convert rank number into printable text.
// Face cards and Ace use letters; all other ranks use their numeric string.
string Card::rankToString(int r) {
if (r == 1)
return "A";
if (r == 11)
return "J";
if (r == 12)
return "Q";
if (r == 13)
return "K";
return std::to_string(r);
}
// Helper: convert suit enum into a short printable label.
string Card::suitToString(Suit s) {
switch (s) {
case Suit::Clubs:
return "C";
case Suit::Diamonds:
return "D";
case Suit::Hearts:
return "H";
case Suit::Spades:
return "S";
}
throw invalid_argument("Invalid suit value");
}
//Returns a compact string like "AS", "10H", "KD".
string Card::toString() const {
return rankToString(rank_) + suitToString(suit_);
}
ostream& operator<<(ostream& os, const Card& c) {
os << c.toString();
return os;
}