package ClassWork.exam.Card;
import java.io.Flushable;
public class Card {
public static enum Flush {
HEART(0),
SPADE(1),
DIAMOND(2),
CLUB(3),
JOKER(4);
private int value;
Flush(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
public static Flush fromValue(int value) {
for (Flush f : Flush.values())
if (f.getValue() == value)
return f;
throw new RuntimeException("Unknown flush");
}
}
private Flush flush;
private int number;
public Card(Flush flush, int number) {
this.flush = flush;
this.number = number;
}
public Flush getFlush() {
return this.flush;
}
public int getNumber() {
return this.number;
}
@Override
public String toString() {
return this.flush + " : " + this.number;
}
}
package ClassWork.exam.Card;
import java.util.ArrayList;
import java.util.List;
public class Deck {
private static final int MAX_NUM = 18;
private List<Card> cards;
public Deck() {
cards = new ArrayList<>();
}
public void add(Card.Flush flush, int number) {
add(new Card(flush, number));
}
public void add(Card card) {
cards.add(card);
}
public boolean contain(Card.Flush flush, int number) {
for (Card c : cards) {
if (c.getFlush() == flush && c.getNumber() == number)
return true;
}
return false;
}
public boolean isFull() {
return cards.size() == Deck.MAX_NUM;
}
public void reset() {
cards.clear();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Card c : cards) {
if (sb.length() > 0)
sb.append(", ");
sb.append(c);
}
return sb.toString();
}
}
package ClassWork.exam.Card;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Poker {
private static final int TOTAL_CARDS = 54;
private Deck deckA;
private Deck deckB;
private Deck deckC;
private Random rand;
public Poker() {
this.deckA = new Deck();
this.deckB = new Deck();
this.deckC = new Deck();
this.rand = new Random();
}
public Deck getDeckA() {
return this.deckA;
}
public Deck getDeckB() {
return this.deckB;
}
public Deck getDeckC() {
return this.deckC;
}
public void deal() {
this.rand.setSeed(System.currentTimeMillis());
deckA.reset();
deckB.reset();
deckC.reset();
List<Integer> cache = new ArrayList<>();
for (; cache.size() < Poker.TOTAL_CARDS; ) {
int num = rand.nextInt(TOTAL_CARDS);
if (Poker.contains(cache, num))
continue;
cache.add(num);
}
for (int i = 0; i < Poker.TOTAL_CARDS / 3; i++) {
int num = cache.get(i);
Poker.deal(deckA, num);
}
for (int i = 18; i < 2 * Poker.TOTAL_CARDS / 3; i++) {
int num = cache.get(i);
Poker.deal(deckB, num);
}
for (int i = 36; i < Poker.TOTAL_CARDS; i++) {
int num = cache.get(i);
Poker.deal(deckC, num);
}
}
private static boolean contains(List<Integer> cache, int num) {
return cache.contains(num);
}
public static void deal(Deck deck, int num) {
if (num < TOTAL_CARDS - 2)
deck.add(Card.Flush.fromValue(num % 4), num / 4 + 1);
else {
if (num == TOTAL_CARDS - 2)
deck.add(Card.Flush.JOKER, 0);
else
deck.add(Card.Flush.JOKER, 1);
}
}
}
加油,少年