-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.h
46 lines (36 loc) · 1.25 KB
/
card.h
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
#ifndef CARD_H
#define CARD_H
#include <stdio.h>
enum Color { BLACK, RED };
enum Suit { SPADE, HEART, DIAMOND, CLUB };
enum Rank { ACE, R2, R3, R4, R5, R6, R7, R8, R9, R10, RJ, RQ, KING };
class Card {
public:
Card() : card_(-1) {}
Card(const Card& card) : card_(card.card_) {}
Card(int card) : card_(card) {}
Card(int suit, int rank) : card_(suit + (rank << 2)) {}
int card() const { return card_; }
int suit() const { return card_ & 3; }
int rank() const { return card_ >> 2; }
int color() const { return suit() == SPADE || suit() == CLUB ? BLACK : RED; }
bool IsMajor() const { return suit() == SPADE || suit() == HEART; }
bool IsAbove(Card foundation_top) const {
return suit() == foundation_top.suit() &&
rank() == foundation_top.rank() + 1;
}
bool IsBelow(Card tableau_top) const {
return color() != tableau_top.color() && rank() + 1 == tableau_top.rank();
}
bool operator==(const Card& card) const { return card_ == card.card_; }
bool operator!=(const Card& card) const { return !(*this == card); }
char* ToString() const {
static char str[32];
sprintf(str, "\e[3%dm%c%c\e[0m", 2 - color(), "SHDC"[suit()],
"A23456789TJQK"[rank()]);
return str;
}
private:
char card_;
};
#endif