-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardGenerator.java
More file actions
144 lines (124 loc) · 3.88 KB
/
CardGenerator.java
File metadata and controls
144 lines (124 loc) · 3.88 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
package com.pslin.cards;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
/**
* Card generating utils
*
* @author plin
*/
public class CardGenerator {
/**
* Get a hand consisting of a straight with random suits.
*
* @param min - starting value
* @param max - ending value (inclusive)
* @return a hand with a straight
*/
public static Hand getStraight(int min, int max) {
if (max - min != 4) {
return null;
}
Hand hand = new Hand();
for (int i = min; i <= max; i++) {
hand.addCard(new Card(i, getRandomSuit()));
}
return hand;
}
/**
* Get a hand consisting of a flush.
*
* @param suit - the suit of the flush
* @return a hand with a flush
*/
public static Hand getFlush(Card.Suit suit) {
int value = getRandomValue(Card.MIN_VALUE, Card.MAX_VALUE);
Hand hand = new Hand();
for (int i = 0; i < 5; i++) {
Card card;
do {
card = new Card(value, suit);
} while (hand.getCards().contains(card));
hand.addCard(card);
value = getRandomValue(Card.MIN_VALUE, Card.MAX_VALUE);
}
return hand;
}
/**
* Get a full house consisting of the triple and pair values.
*
* @param tripleValue
* @param pairValue
* @return a hand with a full house
*/
public static Hand getFullHouse(int tripleValue, int pairValue) {
// the triple and pairs can't have the same value
if(tripleValue == pairValue) {
return null;
}
Deck deck = new Deck();
Hand hand = new Hand();
Collections.shuffle(deck.getCards());
Iterator<Card> iterator = deck.getCards().iterator();
int tripleCounter = 0;
int pairCounter = 0;
while(iterator.hasNext()) {
Card card = iterator.next();
if(tripleCounter < 3 && card.getValue() == tripleValue) {
hand.addCard(card);
iterator.remove();
tripleCounter++;
}
if(pairCounter < 2 && card.getValue() == pairValue) {
hand.addCard(card);
iterator.remove();
pairCounter++;
}
// once the hand consists of 5 cards, exit loop
if(hand.getCards().size() == 5) {
break;
}
}
return hand;
}
/**
* Get a hand consisting of five random cards.
*
* @return a random hand
*/
public static Hand getFiveRandom() {
int value = getRandomValue(Card.MIN_VALUE, Card.MAX_VALUE);
Hand hand = new Hand();
for (int i = 0; i < 5; i++) {
Card card;
do {
card = new Card(value, getRandomSuit());
} while (hand.getCards().contains(card));
hand.addCard(card);
value = getRandomValue(Card.MIN_VALUE, Card.MAX_VALUE);
}
return hand;
}
/**
* Returns a random suit (club, diamond, heart, spade).
*
* @return a random suit
*/
public static Card.Suit getRandomSuit() {
Random random = new Random();
int r = random.nextInt(Card.Suit.values().length);
Card.Suit[] suits = Card.Suit.values();
return suits[r];
}
/**
* Generates a random integer (card value) between min and max, inclusive.
*
* @param min - minimum value (inclusive)
* @param max - maximum value (inclusive)
* @return - random integer
*/
public static int getRandomValue(int min, int max) {
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
}