-2

我有一个手类有很多卡。庄家向我发牌,我希望我的游戏显示用户当前拥有的牌。这是我在GameEngine课堂上打印手牌的游戏代码。问题是它会打印出整个甲板!这是打印出来的:Your hand: [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] of Spades。我只希望它打印一张卡,而不是所有卡,那么我的代码有什么问题?

public void beginCardDeal () {
    for (int x = 0; x < 2; x++) {
        System.out.print("\nDealer is dealing a card. . . \n");
        d.dealCard();
        System.out.println("\nYour hand: " + pl.getMyCards());
    }
}

pl 是Player该类的一个实例,因为 Players 有一手。d 是 的一个实例Deck

public class Player extends Person {

private Hand myCards = new Hand();
private int playerCashAmount = 100;

public Player() {

}

public Player(String name) {
    this.name = name;
}

public String getName() {
    return super.getName();
}

public int getPlayerCashAmount() {
    return playerCashAmount;
}

public void setPlayerCashAmount(int playerCashAmount) {
    this.playerCashAmount = playerCashAmount;
}

public Hand getMyCards() {
    return myCards;
}

public void setMyCards(Hand myCards) {
    this.myCards = myCards;
}

另外,这是我的Deck课:

public class Deck {

private ArrayList<Card> deck = new ArrayList<Card>();
private List<Card> cardUsed = new ArrayList<Card>();

int numCards = 0;

public Deck(int numCards) {
    this.createDeck(numCards, 4);
}

private void createDeck(int numCards, int numSuits) {
    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;
    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), j));
        }
    }
}

public Card dealCard( ) {
    Card dealtCard = null;
    if (deck.size() == 0){
        deck.addAll(cardUsed);
        this.shuffle();
        cardUsed = new ArrayList<Card>();
    }

    dealtCard = deck.get(0);
    deck.remove(0);
    cardUsed.add(dealtCard);

    return dealtCard;
}

public void shuffle() {
    Collections.shuffle(deck);
}

public ArrayList<Card> getDeck() {
    return deck;
}

public void setDeck(ArrayList<Card> deck) {
    this.deck = deck;
}

public int getNumUsedCards() {
    return cardUsed.size();
}

public List<Card> getCardUsed() {
    return cardUsed;
}

public void setCardUsed(List<Card> cardUsed) {
    this.cardUsed = cardUsed;
}

编辑:

public class Hand {

int total = 0;
private ArrayList<Card> hand = new ArrayList<Card>();

public Hand(){

}

public String toString(){
    //Suit s = new Suit(total);
    //Card c = new Card(s, total);
    //return (Arrays.toString(c.getCardRanks())) + " of " + s.getSuitName();
    return Arrays.toString(hand.toArray());
}

public boolean discardHand(){
    if (hand.isEmpty()) {
        hand = new ArrayList<Card>();
    }
    return false;
}

public void addCard(Card c) {
    this.hand.add(c);
}

public Card getPosition(int index){
    return hand.get(index);
}

public int handCardCount(){
    return hand.size();
}

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

public ArrayList<Card> getHand() {
    return hand;
}

public void setHand(ArrayList<Card> hand) {
    this.hand = hand;
}
4

2 回答 2

1

当您发牌时,您不会将其添加到玩家手牌中。

于 2013-07-03T20:42:33.450 回答
0

您的对象需要更好地反映它们所代表的性质:一副牌和一手牌都只是一组有序的牌,因此为它们使用底层数组列表(或简单数组)就可以了。“发牌”是将牌从牌组移动到手牌的行为,因此它应该是牌组对象将手牌作为参数处理的方法,例如deck.dealTo(hand),或者返回牌并使用分配,例如hand.add(deck.deal())。你的礼物d.dealCard()从牌堆中取出一张牌,然后把它丢在地板上。它还以一种奇怪的方式进行交易——我无法想象在任何游戏中,当套牌用尽时,你只需洗牌并继续交易!

同样,玩家对象应该包含对手对象的引用(或者可能没有,或者超过一个,具体取决于游戏),但是您不与玩家交易,而是与手交易。此外,有些“手”可能根本与玩家无关,例如纸牌中的“婴儿床”或德州扑克中的“棋盘”。

于 2013-07-04T00:07:36.763 回答