如何让我的PokerHand课程随机交易一千手?

时间:2014-04-08 18:35:03

标签: poker

public class PokerHand
{
    // ArrayList for cards
    private ArrayList<Card> cards;

    /**
     * Constructor for class Pokerhand
     */
    public PokerHand()
    {
         cards = new ArrayList<Card>(); // arrayList of cards

    }
    /**
     * Add cards to list
     */
    public void addCard(Card card1, Card card2, Card card3)
    {
        cards.add(card1);
        cards.add(card2);
        cards.add(card3);
    }         
}

这是我的卡类

public class Card()
{
    private int value;
    private int suit;

    private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
    private static String[] values  = { "Ace", "2", "3", "4", "5", "6", "7", 
                                       "8", "9", "10", "Jack", "Queen", "King" };

    public static  String valueAsString( int value ) {
        return values[value];
    }

    Card(int suit, int rank)
    {
        this.value=value;
        this.suit=suit;
    }

    public  String toString()
    {
          return values[value] + " of " + suits[suit];
    }

    public int getValue() {
         return value;
    }

    public int getSuit() {
        return suit;
    }
}

如何处理1000张三张牌?我被困在各种各样的事情上,包括我需要的其他方法。我怎样才能构建一个交易方法,如何在每次交易一手牌的同时随意改变交易方法,并且如何进行交易?

1 个答案:

答案 0 :(得分:0)

所有这些实现都基于my card library的代码。

随机

public Card[] shuffle(Card[] deck) {
    Random gen = new Random();
    boolean[] added = new boolean[deck.length];
    int positionToAdd = gen.nextInt(deck.length);
    Card[] newDeck = new Card[deck.length];

    for (int i = 0; i < deck.length; i++) {
        while (newDeck[i] == null) {
            if (!added[positionToAdd]) {
                newDeck[i] = deck[positionToAdd];
                positionToAdd = gen.nextInt(deck.length);
                added[i] = true;
            } else {
                positionToAdd = gen.nextInt(deck.length);
            }
        }
    }


    return newDeck;
}

实施

ArrayList<Card> dealFrom = new ArrayList<Card>();
PokerHand[] hands = new PokerHand[1000];
/*Populate the "dealFrom" yourself here*/

dealFrom = Array.asList(shuffle(dealFrom.toArray()));

for (int i = 0; i < 3000; i += 3) {
    //Write your deal method here
    hands[i] = new PokerHand(dealFrom[i], dealFrom[i + 1], dealFrom[i + 2]);
}

希望这有帮助。