-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
173 lines (143 loc) · 4.38 KB
/
Card.java
File metadata and controls
173 lines (143 loc) · 4.38 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Name: Vince Hammers
* Date: 4/21/2018
* File(s): Card.java
* Course: CPS 176-H2
* Purpose: To represent a standard playing card.
*/
public class Card {
/**
* The possible values for a card's suit
*/
enum Suit {
CLUBS,
DIAMONDS,
HEARTS,
SPADES;
/**
* Gets a string representation if the same naming format as the image
* files. First letter capitalized, the rest lower case.
*
* @return A String representation of the suit
*/
public String getString() {
String str = this.name();
// Keep only the first letter capital
return str.substring(0, 1) +
str.substring(1, str.length()).toLowerCase();
}
}
/**
* The possible values for a card's rank
*/
enum Rank {
ACE, TWO, THREE, FOUR,
FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN,
KING;
/**
* Gets a string representation if the same naming format as the image
* files. First letter capitalized, the rest lower cased for face cards.
* The numerical representation for the rest.
*
* @return A string representation of the rank
*/
public String getString() {
String str;
// Get the numeric value of the non-face cards.
if (this != ACE && this != JACK && this != QUEEN && this != KING) {
str = String.format("%d", this.ordinal() + 1);
}
// Do the same thing as the suit getString method for the face cards.
else {
str = this.name();
return str.substring(0, 1) +
str.substring(1, str.length()).toLowerCase();
}
return str;
}
}
private Rank rank; // The rank of the card.
private Suit suit; // The suit of the card.
/**
* Sets the value of rank and suit by passing enum constants.
*
* @param r The enum constant to set rank to
* @param s The enum constant to set suit to
*/
public Card(Rank r, Suit s) {
rank = r;
suit = s;
}
/**
* Sets the value of rank and suit by comparing their ordinal's against integers.
* Helpful for creating many cards with for loops.
*
* @param r The ordinal of the enum value
* @param s The ordinal of the enum value
*/
public Card(int r, int s) {
// Default values in case of incorrect input
rank = Rank.TWO;
suit = Suit.CLUBS;
for (Rank i : Rank.values()) {
if (r == i.ordinal()) {
rank = i;
}
}
for (Suit i : Suit.values()) {
if (s == i.ordinal()) {
suit = i;
}
}
}
/**
* Accessor method for rank
*
* @return the value of the rank field
*/
public Rank getRank() {
return rank;
}
/**
* Accessor method for suit
*
* @return The value of the suit field
*/
public Suit getSuit() {
return suit;
}
/**
* Creates and returns a filepath to the image that matches this card's rank
* and suit so that it can be rendered.
*
* @return The full filepath to the matching jpg file
*/
public String getFilepath() {
String filepath = "images\\";
filepath += rank.getString() + "_" + suit.getString() + ".jpg";
return filepath;
}
/**
* Returns a string in the format rank of suit. ex. QUEEN of HEARTS
*
* @return A string containing the rank and suit of this card.
*/
public String toString() {
return rank + " of " + suit;
}
/**
* Compares the rank and suit fields of this object against another card object
* to determine if they are equal.
*
* @param c another Card object
* @return true if the fields are equal, false otherwise
*/
public boolean equals(Card c) {
boolean result = false;
if (rank == c.rank && suit == c.suit) {
result = true;
}
return result;
}
}