-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
92 lines (89 loc) · 2.79 KB
/
Deck.java
File metadata and controls
92 lines (89 loc) · 2.79 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
import java.util.*;
class Deck {
private ArrayList<Card> deck;
// Constructor initializes the deck
public Deck() {
deck = new ArrayList<>();
createDeck();
}
// Creates a deck of 52 cards
public void createDeck() {
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
for (String rank : ranks) {
for (String suit : suits) {
deck.add(new Card(rank, suit));
}
}
}
// Prints all cards in the deck
public void printDeck() {
for (Card card : deck) {
System.out.println(card);
}
}
// Prints a single card
public void printCard(Card card) {
System.out.println(card);
}
// Shuffle the deck
public void shuffleDeck() {
Collections.shuffle(deck);
System.out.println("Deck has been shuffled.");
}
// Prints all cards of a specific suit
public void sameCard(String suit) {
System.out.println("Cards of suit " + suit + ":");
for (Card card : deck) {
if (card.getSuit().equalsIgnoreCase(suit)) {
System.out.println(card);
}
}
}
// Prints all cards of a specific rank
public void compareCard(String rank) {
System.out.println("Cards with rank " + rank + ":");
for (Card card : deck) {
if (card.getRank().equalsIgnoreCase(rank)) {
System.out.println(card);
}
}
}
// Searches for a specific card in the deck
public boolean findCard(String rank, String suit) {
for (Card card : deck) {
if (card.getRank().equalsIgnoreCase(rank) && card.getSuit().equalsIgnoreCase(suit)) {
return true;
}
}
return false;
}
// Deals 5 random cards
public void dealCard() {
shuffleDeck();
System.out.println("Dealing 5 random cards:");
for (int i = 0; i < 5; i++) {
System.out.println(deck.remove(0));
}
}
// Prints 2 random cards from the deck
public void randomCards() {
shuffleDeck();
System.out.println("Random Card 1: " + deck.get(0));
System.out.println("Random Card 2: " + deck.get(1));
}
// Distributes 5 cards to 3 players
public void cardPlayers() {
if (deck.size() < 15) {
System.out.println("Not enough cards to deal to players.");
return;
}
for (int i = 1; i <= 3; i++) {
System.out.println("Player " + i + "'s cards:");
for (int j = 0; j < 5; j++) {
System.out.println(deck.remove(0));
}
System.out.println();
}
}
}