-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
31 lines (26 loc) · 1.03 KB
/
Deck.java
File metadata and controls
31 lines (26 loc) · 1.03 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
import java.util.*;
//Represents a deck of playing cards.
public class Deck {
private List<Card> cards = new ArrayList<>(); // List to hold the cards in the deck
public Deck() {
initializeDeck(); // Initialize and shuffle the deck
}
private void initializeDeck() {
String[] suits = {"hearts", "diamonds", "clubs", "spades"}; // All possible suits
for (String suit : suits) {
for (int value = 2; value <= 14; value++) { // Values from 2 to Ace (14)
cards.add(new Card(suit, value)); // Create and add a new card to the deck
}
}
shuffle(); // Shuffle the deck after initialization
}
public void shuffle() {
Collections.shuffle(cards); // Shuffle the cards using Collections.shuffle
}
public Card drawCard() {
if (cards.isEmpty()) {
initializeDeck(); // If the deck is empty, reinitialize and shuffle it
}
return cards.remove(cards.size() - 1); // Draw and return the top card from the deck
}
}