-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.cpp
101 lines (89 loc) · 2.31 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
//
// card.cpp
//
//
// Project Finished by Adrian Melo and David Fernandez
//
#include <stdio.h>
#include "card.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
Card::Card() { //default, ace of spades when creating a card
mySuit = spades;
myRank = 1;
}
Card::Card(int rank, Suit s) { //set suit and rank from input parameters
mySuit = s;
myRank = rank;
}
string Card::toString() const { //returns the string version i.e. Ac 4h Js
return (rankString(myRank) + suitString(mySuit));
}
bool Card::sameSuitAs(const Card& c) const{
if(mySuit == c.mySuit){
return true;
}else{
return false;
}
}
//Converts Enum to String
//Output: suit - s or h or d or c
string Card::suitString(Suit s) const {
if (s == spades) {
return "s";
} else if (s == hearts) {
return "h";
} else if (s == diamonds) {
return "d";
} else if (s == clubs) {
return "c";
} else {
return "Error";
}
}
//Converts rank int to String
//Output: rank - A to K
string Card::rankString(int r) const {
if (r == 1) {
return "A";
} else if (r > 1 && r < 11) {
return (to_string(r));
} else if (r == 11) {
return "J";
} else if (r == 12) {
return "Q";
} else if (r == 13) {
return "K";
} else{
return "Error";
}
}
//Compares two cards
//Output: True if equal
bool Card::operator==(const Card &rhs) const {
if (myRank != rhs.myRank) {
return false;
} else if (mySuit != rhs.mySuit) {
return false;
} else {
return true;
}
}
//Compares two cards
//Output: True if not equal
bool Card::operator!=(const Card &rhs) const {
if (myRank == rhs.myRank) {
return false;
} else if (mySuit == rhs.mySuit) {
return false;
} else {
return true;
}
}
//Operator to be able to outputcards
ostream& operator << (ostream& out, const Card& c){
out << c.toString();
return out;
}