-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
141 lines (127 loc) · 3.57 KB
/
Card.java
File metadata and controls
141 lines (127 loc) · 3.57 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
package org.example;
import java.awt.*;
/**
* Represents a playing card with a rank, suit, and face-up state.
*
* @author Mika Edenfield
*/
public class Card {
/**
* Constructs a new Card object.
*
* @param rank The rank of the card (Ace, 2-10, Jack, Queen, King).
* @param suit The suit of the card (Hearts, Diamonds, Clubs, Spades).
* @param faceUp The initial face-up state of the card (true if face up, false otherwise).
*/
public Card(Rank rank, Suit suit, boolean faceUp) {
this.suit = suit;
this.rank = rank;
this.faceUp = faceUp;
}
/**
* Represents the four suits of a standard deck of playing cards.
*/
public enum Suit {
HEARTS(Color.RED),
DIAMONDS(Color.RED),
CLUBS(Color.BLACK),
SPADES(Color.BLACK);
private final Color color;
/**
* Constructs a new Suit with a specified color.
*
* @param color The color associated with the suit.
*/
Suit(Color color) {
this.color = color;
}
/**
* Returns the color of the suit.
*
* @return The color of the suit.
*/
public Color getColor() {
return color;
}
}
/**
* Represents the thirteen ranks of a standard deck of playing cards.
*/
public enum Rank {
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING
}
private final Suit suit;
private final Rank rank;
private boolean faceUp;
/**
* Returns the suit of the card.
*
* @return The suit of the card.
*/
public Suit getSuit() {
return suit;
}
/**
* Returns the rank of the card.
*
* @return The rank of the card.
*/
public Rank getRank() {
return rank;
}
/**
* Checks if the card is currently face up.
*
* @return True if the card is face up, false otherwise.
*/
public boolean isFaceUp() {
return faceUp;
}
/**
* Flips the card to its opposite face-up state.
*/
public void flip() {
this.faceUp = !this.faceUp;
}
/**
* Returns a string representation of the card.
* If the card is face up, it returns the rank and suit (e.g., "A♥", "K♠").
* If the card is face down, it returns "Face Down".
*
* @return A string representation of the card.
*/
@Override
public String toString() {
String rankSymbol;
if (rank == Rank.ACE) {
rankSymbol = "A";
} else if (rank == Rank.JACK) {
rankSymbol = "J";
} else if (rank == Rank.QUEEN) {
rankSymbol = "Q";
} else if (rank == Rank.KING) {
rankSymbol = "K";
} else {
rankSymbol = String.valueOf(rank.ordinal() + 1);
}
String suitSymbol;
switch (suit) {
case HEARTS:
suitSymbol = "♥";
break;
case DIAMONDS:
suitSymbol = "♦";
break;
case CLUBS:
suitSymbol = "♣";
break;
case SPADES:
suitSymbol = "♠";
break;
default:
suitSymbol = "?"; // Fallback if needed
}
return faceUp ? rankSymbol + suitSymbol : "Face Down"; // Show card or default to face down
}
}