-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnoCard.cpp
99 lines (87 loc) · 2.47 KB
/
UnoCard.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
#pragma once
#include <iostream>
using namespace std;
// User defined color and rank types (user-defined integers)
enum color { RED = 1, BLUE = 2, GREEN = 3, YELLOW = 4, black = 5 };
enum rank2 { N1 = 1, N2 = 2, N3 = 3, N4 = 4, N5 = 5 };
class Card
{
public:
Card(color COLOR, rank2 rank); // constructor with some default values
~Card(); // destructor
inline color getcolor(void) const { return m_color; }
inline rank2 getrank(void) const { return m_rank; }
char toCharcolor(void); // returns a char that "describes" card's color
char toCharrank(void); // returns a char that "describes" card's rank
private:
color m_color; // private storage of color
rank2 m_rank; // private storage of rank
friend std::ostream& operator<<(std::ostream& os, const Card &c);
};
// global operator functions comparing ranks of two cards
// The cards a and b are passed by reference
inline bool operator<(const Card& a, const Card& b) {
if (a.getcolor() == b.getcolor()) return (a.getrank()<b.getrank());
else return (a.getcolor()<b.getcolor());
}
inline bool operator>(const Card& a, const Card& b) {
if (a.getcolor() == b.getcolor()) return (a.getrank()>b.getrank());//ASK??
else return (a.getcolor()>b.getcolor());
}
// checks equality of colors and ranks for two cards
inline bool operator==(Card& a, Card& b) { return (a.getrank() == b.getrank() && a.getcolor() == b.getcolor()); }
Card::Card(color color = RED, rank2 rank) : m_color(color), m_rank(rank) {}
// destructor
Card::~Card()
{
}
char Card::toCharcolor()
{
if (m_color == RED) return 'R';
else if (m_color == BLUE) return 'B';
else if (m_color == GREEN) return 'G';
else return 'Y';
}
char Card::toCharrank()
{
if (m_rank == N2) return '2';
else if (m_rank == N3) return '3';
else if (m_rank == N4) return '4';
else if (m_rank == N5) return '5';
else return '1';
}
std::ostream& operator<<(std::ostream& os, const Card &c) {
int color = c.getcolor();
int rank2 = c.getrank();
switch (rank2)
{
case 1:
os << "1";
break;
case 2:
os << "2";
break;
case 3:
os << "3";
break;
case 4:
os << "4";
break;
// default:
// os << m_rank;
}
switch (color) {
case 1:
os << " of RED";
break;
case 2:
os << " of BLUE";
break;
case 3:
os << " of GREEN";
break;
default:
os << " of YELLOW";
}
return os;
}