-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.hpp
158 lines (124 loc) · 4.87 KB
/
Card.hpp
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
#ifndef CARD_HPP
#define CARD_HPP
/* Card.hpp
*
* Represents a single playing card
*
* by Andrew DeOrio
* 2014-12-21
*/
#include <iostream>
// Represent a Card's Rank.
// Rank is a type that can represent the specific values
// listed in the definition below, but no others. (In memory
// the Rank values are represented as integers from 0 to 12.)
// Use the values as constants, for example:
// Rank r1 = FOUR;
// Rank r2 = NINE;
// Ranks may be compared (this compares the underlying int values):
// if (r1 < r2) { // this condition is true
// Overloaded << and >> operators are also defined farther below to
// enable stream input/output for Rank objects:
// cout << r1 << endl; // prints "Four"
// cin >> r2 << endl; // reads a string from cin and updates r2
// Because the underlying representation of the enum is an integer,
// objects of type Rank are ok to pass/return by value.
enum Rank {
TWO = 0,
THREE = 1,
FOUR = 2,
FIVE = 3,
SIX = 4,
SEVEN = 5,
EIGHT = 6,
NINE = 7,
TEN = 8,
JACK = 9,
QUEEN = 10,
KING = 11,
ACE = 12,
};
//REQUIRES str represents a valid rank ("Two", "Three", ..., "Ace")
Rank string_to_rank(const std::string &str);
//EFFECTS Prints Rank to stream, for example "Two"
std::ostream & operator<<(std::ostream &os, Rank rank);
//REQUIRES If any input is read, it must be a valid rank
//EFFECTS Reads a Rank from a stream, for example "Two" -> TWO
std::istream & operator>>(std::istream &is, Rank &rank);
// Represent a Card's suit
enum Suit {
SPADES = 0,
HEARTS = 1,
CLUBS = 2,
DIAMONDS = 3,
};
//REQUIRES str represents a valid suit ("Spades", "Hearts", "Clubs", or "Diamonds")
Suit string_to_suit(const std::string &str);
//EFFECTS Prints Suit to stream, for example "Spades"
std::ostream & operator<<(std::ostream &os, Suit suit);
//REQUIRES If any input is read, it must be a valid suit
//EFFECTS Reads a Suit from a stream, for example "Spades" -> SPADES
std::istream & operator>>(std::istream &is, Suit &suit);
class Card {
public:
//EFFECTS Initializes Card to the Two of Spades
Card();
//EFFECTS Initializes Card to specified rank and suit
Card(Rank rank_in, Suit suit_in);
//EFFECTS Returns the rank
Rank get_rank() const;
//EFFECTS Returns the suit. Does not consider trump.
Suit get_suit() const;
//EFFECTS Returns the suit
//HINT: the left bower is the trump suit!
Suit get_suit(Suit trump) const;
//EFFECTS Returns true if card is a face card (Jack, Queen, King or Ace)
bool is_face_or_ace() const;
//EFFECTS Returns true if card is the Jack of the trump suit
bool is_right_bower(Suit trump) const;
//EFFECTS Returns true if card is the Jack of the next suit
bool is_left_bower(Suit trump) const;
//EFFECTS Returns true if the card is a trump card. All cards of the trump
// suit are trump cards. The left bower is also a trump card.
bool is_trump(Suit trump) const;
private:
Rank rank;
Suit suit;
// This "friend declaration" allows the implementation of operator>>
// to access private member variables of the Card class.
friend std::istream & operator>>(std::istream &is, Card &card);
};
//EFFECTS Prints Card to stream, for example "Two of Spades"
std::ostream & operator<<(std::ostream &os, const Card &card);
//EFFECTS Reads a Card from a stream in the format "Two of Spades"
//NOTE The Card class declares this operator>> "friend" function,
// which means it is allowed to access card.rank and card.suit.
std::istream & operator>>(std::istream &is, Card &card);
//EFFECTS Returns true if lhs is lower value than rhs.
// Does not consider trump.
bool operator<(const Card &lhs, const Card &rhs);
//EFFECTS Returns true if lhs is lower value than rhs or the same card as rhs.
// Does not consider trump.
bool operator<=(const Card &lhs, const Card &rhs);
//EFFECTS Returns true if lhs is higher value than rhs.
// Does not consider trump.
bool operator>(const Card &lhs, const Card &rhs);
//EFFECTS Returns true if lhs is higher value than rhs or the same card as rhs.
// Does not consider trump.
bool operator>=(const Card &lhs, const Card &rhs);
//EFFECTS Returns true if lhs is same card as rhs.
// Does not consider trump.
bool operator==(const Card &lhs, const Card &rhs);
//EFFECTS Returns true if lhs is not the same card as rhs.
// Does not consider trump.
bool operator!=(const Card &lhs, const Card &rhs);
//EFFECTS returns the next suit, which is the suit of the same color
Suit Suit_next(Suit suit);
//EFFECTS Returns true if a is lower value than b. Uses trump to determine
// order, as described in the spec.
bool Card_less(const Card &a, const Card &b, Suit trump);
//EFFECTS Returns true if a is lower value than b. Uses both the trump suit
// and the suit led to determine order, as described in the spec.
bool Card_less(const Card &a, const Card &b, const Card &led_card, Suit trump);
#endif // CARD_HPP