0

我正在尝试制作一个非常简单的二十一点游戏,我在从数组列表中抽牌并将它们放入玩家手和经销商手时遇到问题......

这是我到目前为止所拥有的,任何愿意提供帮助的人将不胜感激。

我也意识到我很多人没有以最有效的方式做到这一点......

卡片阵列列表

import java.util.ArrayList;

public class deck {
    ArrayList<card> deck = new ArrayList<card>();

public deck () {

    deck = new ArrayList<card>();

    //Spades
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("3", "Spades"));
    deck.add(new card ("4", "Spades"));
    deck.add(new card ("5", "Spades"));
    deck.add(new card ("6", "Spades"));
    deck.add(new card ("7", "Spades"));
    deck.add(new card ("8", "Spades"));
    deck.add(new card ("9", "Spades"));
    deck.add(new card ("10", "Spades"));
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("J", "Spades"));
    deck.add(new card ("Q", "Spades"));
    deck.add(new card ("K", "Spades"));
    deck.add(new card ("A", "Spades"));
    //Clubs
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("3", "Clubs"));
    deck.add(new card ("4", "Clubs"));
    deck.add(new card ("5", "Clubs"));
    deck.add(new card ("6", "Clubs"));
    deck.add(new card ("7", "Clubs"));
    deck.add(new card ("8", "Clubs"));
    deck.add(new card ("9", "Clubs"));
    deck.add(new card ("10", "Clubs"));
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("J", "Clubs"));
    deck.add(new card ("Q", "Clubs"));
    deck.add(new card ("K", "Clubs"));
    deck.add(new card ("A", "Clubs"));
    //Hearts
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("3", "Hearts"));
    deck.add(new card ("4", "Hearts"));
    deck.add(new card ("5", "Hearts"));
    deck.add(new card ("6", "Hearts"));
    deck.add(new card ("7", "Hearts"));
    deck.add(new card ("8", "Hearts"));
    deck.add(new card ("9", "Hearts"));
    deck.add(new card ("10", "Hearts"));
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("J", "Hearts"));
    deck.add(new card ("Q", "Hearts"));
    deck.add(new card ("K", "Hearts"));
    deck.add(new card ("A", "Hearts"));
    //Diamonds
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("3", "Diamonds"));
    deck.add(new card ("4", "Diamonds"));
    deck.add(new card ("5", "Diamonds"));
    deck.add(new card ("6", "Diamonds"));
    deck.add(new card ("7", "Diamonds"));
    deck.add(new card ("8", "Diamonds"));
    deck.add(new card ("9", "Diamonds"));
    deck.add(new card ("10", "Diamonds"));
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("J", "Diamonds"));
    deck.add(new card ("Q", "Diamonds"));
    deck.add(new card ("K", "Diamonds"));
    deck.add(new card ("A", "Diamonds"));
}

public ArrayList<card> getCards(){
    return deck;
}

public card deal(){
    card one = deck.get((int) (Math.random() * deck.size()));
    deck.remove(one);
    return one;
}

}

玩家和经销商手类

这是我希望用数组中的两张随机卡填充 dealPlayer() 和 dealDealer() 的地方,我现在只是在树桩上,似乎无法弄清楚任何可行的方法......提前谢谢帮助,如果还有什么我需要发布的,请告诉我!

import java.util.ArrayList;

public class dealer {

    deck deck = new deck();
    String dealerTopCardString = "";
    card playerHandCard;
    card dealerHandCard;
    ArrayList<card> dealerHand = new ArrayList<card>();
    ArrayList<card> playerHand = new ArrayList<card>();

public void start() {
    dealerHand = new ArrayList<card>();
    playerHand = new ArrayList<card>();
}

public void dealPlayer() {
    //LOOKING FOR HELP HERE
}

public void dealDealer() {
    //LOOKING FOR HELP HERE
}

public String getPlayerHand() {

    String playerHandString = "";
    for (int i = 0; i < playerHand.size(); i++)
        playerHandCard = playerHand.get(i);
        card card = new card(playerHandCard.getValue(), playerHandCard.getSuit());
        playerHandString += "\n";
        playerHandString += card.getValue();
        playerHandString += "\t";
        playerHandString += card.getSuit();

        return playerHandString;

    }

public String getDealerHand() {

        String dealerHandString = "";
        for (int i = 0; i < dealerHand.size(); i++)
            dealerHandCard = dealerHand.get(i);
            card card = new card(dealerHandCard.getValue(), dealerHandCard.getSuit());
            dealerHandString += "\n";
            dealerHandString += card.getValue();
            dealerHandString += "\t";
            dealerHandString += card.getSuit();

            return dealerHandString;
    }

public String getDealerTopCard(){

    return getDealerTopCard;
}

public int getPlayerHandValue(){

    return getPlayerHandValue;
}

public int getDealerHandValue(){


    return getDealerHandValue;
}

public void resetDealerHand(){
    dealerHand.clear();
}

public void resetPlayerHand(){
    playerHand.clear();
}

}
4

2 回答 2

5

当您发布问题时,最好的做法是说出您尝试过的内容。这样,其他人可以帮助您了解您可能出错的地方。

我只是给你一些关于你应该如何解决问题的一般性建议。如果您在实施它时遇到特定问题,您可以提出一个新问题(或根据它的相关程度更新这个问题)。

在真正的二十一点游戏中,你会洗牌,然后取出最上面的 4 张牌。你可以在这里做同样的事情:

  1. 随机播放 ArrayList
  2. 取出最上面的 4 张牌放在闲家/庄家手中。(也许您可以使用该remove方法删除卡组中的第一个或最后一个元素。)

这看起来可能是家庭作业,因此您可能需要使用 ArrayList。不过,这确实不是在这里使用的最佳数据结构。如果您有选择,我会改用ArrayDeque。它具有从集合中取出第一个或最后一个项目的有效方法pop()remove()

(提示:“deque”代表双端队列,但发音为“deck”。这是因为它在概念上就像一副纸牌......)

于 2013-09-10T22:46:33.897 回答
3

只需使用Collections#shuffle洗牌你的牌组

Collections.shuffle(deck);

然后只需使用List#remove从牌组顶部获取下一张牌,所以像这样处理起手牌

playerHand.add(deck.remove(0));   // top card to player
dealerHand.add(deck.remove(0));   // top card to dealer
playerHand.add(deck.remove(0));
dealerHand.add(deck.remove(0));
于 2013-09-10T22:45:04.303 回答