【问题标题】:Java code for dealing cards from a deck从一副牌中发牌的 Java 代码
【发布时间】:2012-10-31 14:16:01
【问题描述】:

我正在尝试编写一个方法来从牌组顶部移除指定数量的卡片并将它们作为数组返回。

这是我到目前为止所做的。创建牌组、复制牌组的方法、返回数组中指定位置的牌的方法、返回牌组大小的方法、洗牌和切牌(不确定这些是正确的)。这是卡片类

public class Card {

    private final int suit; // 0, 1, 2, 3 represent Spades, Hearts, Clubs,
                            // Diamonds, respectively

    private final int value; // 1 through 13 (1 is Ace, 11 is jack, 12 is
                                // queen, 13 is king)

    /*
     * Strings for use in toString method and also for identifying card images
     */
    private final static String[] suitNames = { "s", "h", "c", "d" };
    private final static String[] valueNames = { "Unused", "A", "2", "3", "4",
            "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

    /**
     * Standard constructor.
     * 
     * @param value
     *            1 through 13; 1 represents Ace, 2 through 10 for numerical
     *            cards, 11 is Jack, 12 is Queen, 13 is King
     * @param suit
     *            0 through 3; represents Spades, Hearts, Clubs, or Diamonds
     */
    public Card(int value, int suit) {
        if (value < 1 || value > 13) {
            throw new RuntimeException("Illegal card value attempted.  The "
                    + "acceptable range is 1 to 13.  You tried " + value);
        }
        if (suit < 0 || suit > 3) {
            throw new RuntimeException("Illegal suit attempted.  The  "
                    + "acceptable range is 0 to 3.  You tried " + suit);
        }
        this.suit = suit;
        this.value = value;
    }

    /**
     * "Getter" for value of Card.
     * 
     * @return value of card (1-13; 1 for Ace, 2-10 for numerical cards, 11 for
     *         Jack, 12 for Queen, 13 for King)
     */
    public int getValue() {
        return value;
    }

    /**
     * "Getter" for suit of Card.
     * 
     * @return suit of card (0-3; 0 for Spades, 1 for Hearts, 2 for Clubs, 3 for
     *         Diamonds)
     */
    public int getSuit() {
        return suit;
    }

    /**
     * Returns the name of the card as a String. For example, the 2 of hearts
     * would be "2 of h", and the Jack of Spades would be "J of s".
     * 
     * @return string that looks like: value "of" suit
     */
    public String toString() {
        return valueNames[value] + " of " + suitNames[suit];
    }

    /**
     * [STUDENTS SHOULD NOT BE CALLING THIS METHOD!] Used for finding the image
     * corresponding to this Card.
     * 
     * @return path of image file corresponding to this Card.
     */
    public String getImageFileName() {

        String retValue;
        retValue = suitNames[suit];
        if (value <= 10)
            retValue += value;
        else if (value == 11)
            retValue += "j";
        else if (value == 12)
            retValue += "q";
        else if (value == 13)
            retValue += "k";
        else
            retValue += "Unknown!";
        return "images/" + retValue + ".gif";
    }
}

Deck 方法是我需要帮助的一种方法

public class Deck {

    private Card[] cards;

    public Deck() {
        cards = new Card[52];
        int numberOfCard = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int value = 1; value <= 13; value++) {
                cards[numberOfCard] = new Card(value, suit);
                numberOfCard++;
            }
        }
    }

    public Deck(Deck other) {

        cards = new Card[other.cards.length];
        for (int i = 0; i < other.cards.length; i++) {
            cards[i] = other.cards[i];
        }
    }

    public Card getCardAt(int position) {
        if (position >= cards.length) {
            throw new IndexOutOfBoundsException("Values are out of bounds");
        } else {
            return cards[position];
        }
    }

    public int getNumCards() {
        return cards.length;
    }

    public void shuffle() {
        int temp = 0;
        for (int row = 0; row < cards.length; row++) {
            int random = (int) (Math.random() * ((cards.length - row) + 1));
            Deck.this.cards[temp] = this.getCardAt(row);
            cards[row] = cards[random];
            cards[random] = cards[temp];
        }

    }

    public void cut(int position) {
        Deck tempDeck = new Deck();
        int cutNum = tempDeck.getNumCards() / 2;
        for (int i = 0; i < cutNum; i++) {
            tempDeck.cards[i] = this.cards[52 - cutNum + i];
        }
        for (int j = 0; j < 52 - cutNum; j++) {
            tempDeck.cards[j + cutNum] = this.cards[j];
        }
        this.cards = tempDeck.cards;
    }

    public Card[] deal(int numCards) {
        return cards;
    }

}

【问题讨论】:

  • 您还没有说出问题所在或需要帮助的地方。附带说明一下,您的复制构造函数只制作一个浅拷贝,可能不是您所希望的。
  • 他从“我正在尝试编写一个方法来从牌组顶部移除指定数量的牌并将它们作为数组返回”开始,而Deck.deal() 只是一个存根。帖子很乱,是的,但说明了问题所在。
  • 考虑将您的卡片存储在 List 中以便于访问,然后 deal 方法可以调用 remove()。附带说明;考虑使用枚举来表示卡片docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html

标签: java arrays shuffle poker


【解决方案1】:

如果您不能使用Lists,最好使用另一个变量作为牌组顶牌的索引:

public class Deck {

    private Card[] cards;
    private int topCardIndex;

    public Deck() {
        cards = new Card[52];
        int numberOfCard = 0;
        for(int suit = 0; suit <= 3; suit++){
            for(int value = 1; value <= 13; value++){
                cards[numberOfCard] = new Card(value, suit);
                numberOfCard++;
            }
        }
        topCardIndex = 0;
    }

    public Card getCardAt(int position) {
        if (position >= cards.length - topCardIndex || position < topCardIndex) {
            throw new IndexOutOfBoundsException("Values are out of bounds");
        } else {
            return cards[topCardIndex + position];
        }
    }

    public Card[] deal(int numCards) {
        // FIXME: check bounds, or make method "Card pickCardAt(int position)"
        // that removes the card from the deck
        Card[] drawnCards = new Card[numCards];
        for(int index = 0; index < numCards; index ++) {
            drawnCards[index] = cards[topCard];
            topCard++;
        }
        return drawnCards;
    }

我认为你最好将 Deck 的 card 集合设为 List 而不是 Card[],这样你就有更好的操作,如 remove()

如果你想从牌组中取出n 的第一张牌,那么你只需将remove() n 次放到你的结果数组中(我也会制作一个列表)。

public List<Card> deal(int numCards) {
    List<Card> drawnCards = new ArrayList<Card>(numCards);
    for(int index = 0; index < numCards; index++) {
        drawnCards.add(cards.remove(0));
    }
    return drawnCards;
}

【讨论】:

  • 我必须尝试将 Deck 的卡片集合设为 Card[],无法使用 List。我知道使用 Lis 会容易得多
  • @user1817301 所以最好让卡片“不可变”并存储牌组顶部卡片的索引。我会尝试修复我的帖子。
【解决方案2】:

这就是我在游戏中处理牌组的方式。我也有 read() 方法,可以从 XML 文件中读取卡片并将它们推入。

import java.util.Stack;

public class CardStack extends Stack<Card> {

    public CardStack () {}

    public void shuffle() {
        List<Card> list = this.subList(0, this.size());
        //changes in this list are reflected in original Stack
        Collections.shuffle(list);
    }

    public Card pop () {
        try {
            Card card = super.pop();
            return card;
        } catch (EmptyStackException e) {
            return null;
        }
    }
}

【讨论】:

  • 我必须尝试将Deck的卡片集合设为Card[],不能使用任何其他方式。我在想这个,但我得到了错误 public Card[] deal(int numCards) { int numCards; for (int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2014-02-27
  • 2012-12-15
  • 2012-06-07
相关资源
最近更新 更多