Извините за длинный пост, но хорошо разработанная объектная модель может иметь большое значение.Если вы упаковываете колоду и руки, то вы можете использовать их взаимозаменяемо при раздаче карт и их вставке.
Вот классы, которые вы можете найти ниже:
AbstractDeck.java
Actions.java
Card.java
CardHolder.java
Casino.java
Container.java
Deck.java
Hand.java
Player.java
TexasHoldem.java
Казино
Водительпрограмма для перетасовки колоды карт, раздачи карт игрокам и сортировки их рук.
package casino;
import java.util.*;
import java.util.stream.Collectors;
public class Casino {
public static void main(String[] args) {
List<Player> players = Arrays.asList("Bob", "Jill", "Thomas").stream()
.map(Player::new).collect(Collectors.toList());
TexasHoldem holdem = new TexasHoldem();
holdem.dealCards(players, 2);
holdem.sortHands(players);
holdem.play(players);
holdem.printInfo(players);
}
}
Ouput
# FLOP
Player [name=Bob, hand=Hand [cards=[5S, JS]]] => Hand [cards=[5S, 7D, JD, JS, QD]]
Player [name=Jill, hand=Hand [cards=[9H, AH]]] => Hand [cards=[7D, 9H, JD, QD, AH]]
Player [name=Thomas, hand=Hand [cards=[4C, 8S]]] => Hand [cards=[4C, 7D, 8S, JD, QD]]
# TURN
Player [name=Bob, hand=Hand [cards=[5S, JS]]] => Hand [cards=[5H, 5S, 7D, JD, JS, QD]]
Player [name=Jill, hand=Hand [cards=[9H, AH]]] => Hand [cards=[5H, 7D, 9H, JD, QD, AH]]
Player [name=Thomas, hand=Hand [cards=[4C, 8S]]] => Hand [cards=[4C, 5H, 7D, 8S, JD, QD]]
# RIVER
Player [name=Bob, hand=Hand [cards=[5S, JS]]] => Hand [cards=[5H, 5S, 6C, 7D, JD, JS, QD]]
Player [name=Jill, hand=Hand [cards=[9H, AH]]] => Hand [cards=[5H, 6C, 7D, 9H, JD, QD, AH]]
Player [name=Thomas, hand=Hand [cards=[4C, 8S]]] => Hand [cards=[4C, 5H, 6C, 7D, 8S, JD, QD]]
# INFO
Deck [cards=[8C, TS, 3C, AD, QH, 7C, QC, 8D, TH, 4H, 4S, 6H, 2S, 6S, KC, TC, 3H, 4D, KS, 8H, JC, 9D, KH, 5D, TD, 2C, QS, KD, AC, 7S, 7H, 3S, JH, 2D, 2H, 3D, 5C, AS, 9C, 6D, 9S]]
Hand [cards=[7D, JD, QD, 5H, 6C]]
Player [name=Bob, hand=Hand [cards=[5S, JS]]]
Player [name=Jill, hand=Hand [cards=[9H, AH]]]
Player [name=Thomas, hand=Hand [cards=[4C, 8S]]]
TexasHoldem
package casino;
import java.util.List;
public class TexasHoldem {
private Deck deck;
private Hand communityHand;
public TexasHoldem() {
deck = new Deck();
communityHand = new Hand();
deck.shuffle();
}
void printInfo(List<Player> players) {
System.out.println("# INFO");
System.out.println(deck);
System.out.println(communityHand);
players.stream().forEach(System.out::println);
}
void play(List<Player> players) {
for (int i = 0; i < 5; i++) {
communityHand.insert(deck.draw());
if (i == 2) {
System.out.println("# FLOP");
compareHands(players, communityHand);
System.out.println();
}
if (i == 3) {
System.out.println("# TURN");
compareHands(players, communityHand);
System.out.println();
}
if (i == 4) {
System.out.println("# RIVER");
compareHands(players, communityHand);
System.out.println();
}
}
}
public Hand viewHand(Player player, Hand community) {
Hand view = new Hand();
for (Card card : player.getHand().getCards()) {
view.insert(card);
}
for (Card card : community.getCards()) {
view.insert(card);
}
return view;
}
public void compareHands(List<Player> players, Hand community) {
for (Player player : players) {
Hand view = viewHand(player, community);
view.sort();
System.out.printf("%s => %s%n", player, view);
}
}
public void dealCards(List<Player> players, int cardsPerPlayer) {
for (int round = 0; round < cardsPerPlayer; round++) {
for (Player player : players) {
player.getHand().insert(deck.draw());
}
}
}
void sortHands(List<Player> players) {
for (Player player : players) {
player.getHand().sort();
}
}
}
Колода
Стандартная колода из 52 карт по рангу и масти.
package casino;
import java.util.*;
public class Deck extends AbstractDeck<Card> implements CardHolder {
public Deck() {
items = new Stack<Card>();
for (int cardNo = 0; cardNo < 52; cardNo++) {
items.add(new Card(cardNo));
}
}
public List<Card> getCards() {
return items;
}
public void setCards(Stack<Card> cards) {
this.items = cards;
}
@Override
public String toString() {
return String.format("Deck [cards=%s]", items);
}
}
Игрок
Игрок, который держит карточную руку.
package casino;
public class Player {
private String name;
private Hand hand;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Hand getHand() {
return hand;
}
public void setHand(Hand hand) {
this.hand = hand;
}
public Player(String name, Hand hand) {
this.name = name;
this.hand = hand;
}
public Player(String name) {
this(name, new Hand());
}
@Override
public String toString() {
return String.format("Player [name=%s, hand=%s]", name, hand);
}
}
Рука
Представляет собой комбинацию карт.
package casino;
import java.util.*;
public class Hand extends AbstractDeck<Card> implements CardHolder {
public Hand() {
items = new Stack<Card>();
}
public List<Card> getCards() {
return items;
}
public void setCards(Stack<Card> cards) {
this.items = cards;
}
@Override
public String toString() {
return String.format("Hand [cards=%s]", items);
}
}
Карта
Представляет стандартную игральную карту с мастью и рангом.
package casino;
public class Card implements Comparable<Card> {
public static final String[] SUIT = { "C", "D", "H", "S" };
public static final String[] RANK = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" };
private int id;
private int suit;
private int numeral;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getNumeral() {
return numeral;
}
public void setNumeral(int numeral) {
this.numeral = numeral;
}
public Card(int cardNo) {
assert cardNo >= 0;
assert cardNo < 52;
this.id = cardNo;
this.suit = cardNo % 4;
this.numeral = cardNo / 4;
}
public int compareTo(Card otherCard) {
return Integer.compare(this.id, otherCard.id);
}
public String toString() {
return String.format("%s%s", RANK[this.getNumeral()], SUIT[this.getSuit()]);
}
}
CardHolder
Используется для обращения с колодой и рукой как держателями карт, поскольку они расширяют AbstractDeck.
package casino;
public interface CardHolder extends Container<Card> {
// Just used as a category for Collection utilities of need be.
}
Контейнер
Интерфейс, имитирующий общий набор элементов, например карточек.
package casino;
public interface Container<E> {
E peek();
E draw();
boolean insert(E element);
void insertAt(int index, E element);
void shuffle();
void sort();
}
AbstractDeck
Абстрактная реализация Контейнера, содержащего сопоставимые элементы,Колода и рука очень похожи в этом смысле.
package casino;
import java.util.Stack;
public class AbstractDeck<T extends Comparable<T>> implements Container<T> {
protected Stack<T> items;
@Override
public T peek() {
return items.peek();
}
@Override
public T draw() {
return items.pop();
}
@Override
public boolean insert(T item) {
return items.add(item);
}
@Override
public void insertAt(int index, T item) {
items.add(index, item);
}
@Override
public void shuffle() {
Actions.shuffle(items);
}
@Override
public void sort() {
Actions.sort(items);
}
}
Действия
Статический служебный класс для работы с общими списками.
package casino;
import java.util.*;
public class Actions {
private static final Random rnd = new Random();
/** Fisher–Yates shuffle */
public static <E> void shuffle(List<E> list) {
for (int i = list.size() - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
E tmp = list.get(index);
list.set(index, list.get(i));
list.set(i, tmp);
}
}
public static <E extends Comparable<E>> void sort(List<E> list) {
Collections.sort(list);
}
}