【问题标题】:Calculate cardSum (Hands of Strings)计算 cardSum(字符串的手)
【发布时间】:2019-03-02 02:26:30
【问题描述】:

我正在尝试创建二十一点游戏。游戏本身很简单(至少我的版本)。玩家从洗好的牌堆中抽一张牌,并计算已抽出的牌的总和。我有一个 String 类型的卡片组列表,现在这对我来说是个大问题。我不知道如何计算总和,因为它们是字符串类型。你对我能做什么有任何指导吗?我发现的唯一解决方案非常糟糕,是将卡片与字符串进行比较并给它一个值。比如drawnCard.equals("Four of hearts") = "4";

    public class Player {


    private String nickName;
    private int playerNumOfCards;
    ArrayList<Card> playerHand = new ArrayList<>();


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

    public String getNickName() {
        return nickName;
    }

    public void addCard(Card aCard){
        playerHand.add(aCard);
        this.playerNumOfCards++;

    }

    public void getHandSum(){

    }

    public void getPlayerHand(){
        for(Card cards: playerHand){
            System.out.println(cards.toString());
        }
    }


  }


    public class DeckOfCards {

    private Card[] deck;
    private static final Random random = new Random();

    private int currentCard; //index of next Card to be deal (0-51)
    private static int NUMBER_OF_CARDS = 52; //Constant number of cards

    public DeckOfCards(){

        String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"};
        String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

        deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
        currentCard = 0;

        //Populate deck with Cards
        for(int count = 0; count < deck.length; count++)
            deck [count] = new Card(faces [count % 13], suits [count / 13]);
    }

    public void shuffleDeck(){
        currentCard = 0;

        for (int first = 0; first < deck.length; first++){

            int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)

            //Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
            Card temp = deck[first];
            deck [first] = deck [second];
            deck [second] = temp;
        }
    }

    public void getCardDeck(){
        int start = 1;
        for(Card k : deck) {
            System.out.println("" + start + "/52 " + k);
            start++;
        }
    }

    public Card dealNextCard(){

        //Get the top card
        Card topCard = this.deck[0];

        //shift all the subsequent cards to the left by one index
        for(int currentCard = 1; currentCard < NUMBER_OF_CARDS; currentCard ++){
            this.deck[currentCard-1] = this.deck[currentCard];
        }
        this.deck[NUMBER_OF_CARDS-1] = null;

        //decrement the number of cards in our deck
        this.NUMBER_OF_CARDS--;

        return topCard;
    }

}


    public class Card {
    private String face; //Face of card, i.e "King" & "Queen"
    private String suit; //Suit of card, i.e "Hearts" & "diamonds"

    public Card (String cardFace, String cardSuit){ //Constructor which initializes card's face and suit
        this.face = cardFace;
        this.suit = cardSuit;
    }

    public String toString(){ //return String representation of Card
        return face + " of " + suit;
    }
}


 public class BlackJackGame {

    public static void main(String[] args) {

        DeckOfCards deck1 = new DeckOfCards();
        Player player1 = new Player("mille");
        deck1.shuffleDeck();

    }
}

【问题讨论】:

  • 这个问题的答案将取决于你打算用抽到的牌做什么。如果你想有多个参赛者,那么考虑实施一个“手”类来评估它自己的重量也许是一个更好的主意。手类也应该很容易与其他手类进行比较。考虑从牌库中取出抽到的牌,然后将它们加入玩家的手牌。
  • 字符串是为人设计的——计算机用数字做事。如果您将卡片表示为数字,并且只转换为字符串进行输出,您将省去很多麻烦,并使您的代码更小、更快。

标签: java arrays arraylist blackjack


【解决方案1】:

创建一个枚举来表示人脸

enum Face {
    Ace(number), //I don't know what number is for Ace and others.
    Deuce(number),
    Three(3),
    Four(4),
    Five(5),
    Six(6),
    Seven(7),
    Eight(8),
    Nine(9),
    Ten(10),
    Jack(number),
    Queen(number),
    King(number);

    private final int number;

    Faces(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }
}

将人脸类型从字符串更改为人脸。

class Card {

    private Face face;
    private String suit;

    public Card (Face cardFace, String cardSuit){card's face and suit
        this.face = cardFace;
        this.suit = cardSuit;
    }

    public String toString(){
        return face + " of " + suit;
    }

    public int getNumber() {
        return face.getNumber();
    }
}

添加一个从卡片类中获取人脸号码的方法,然后相应地使用它。您还需要更改其他部分或您的项目,但我会留给您去做。

还建议对 Suit 使用枚举。

【讨论】:

  • 这是我希望在 Java 中重载 + 运算符的情况之一。我个人很想向Card 添加一个静态工厂方法来进行累加,这样你就不会泄露Card 的内部信息,但这可能是多余的。
  • 是的,我知道痛苦 :P 使用 C# 已经 7 个月了,我喜欢这个功能 :) 我采用了不将卡片变成域对象的方法。宁愿通过服务或其他东西对它们进行总结。但这又归结为偏好。
  • 是的,总是不止一种方式。一直在探索 Swift,它有运算符重载,我认为在有限的受控方式下它可能是一个非常有用的工具。
  • @Sneh 首先感谢您的回答。我之前没有接触过枚举,但我已经阅读了一些枚举,我理解为什么以及何时可以/应该使用它们。我在用新的枚举 thp 填充卡片组时遇到了一些问题。我是否需要设置器我每个枚举“类”以便在我填充卡片组时设置值?
  • 更新:我修好了。我只是替换了 DeckOfCards 中的 face 和 suit 数组中的值。再次感谢@Sneh
【解决方案2】:

我在那里看不到套牌列表,但是如果您的卡片字符串始终遵循相同的命名约定(即“红心四”),您可以通过用“”空格字段分隔每个字符串来简化您的工作然后比较字符串中的第一个单词以获得数字(或对应的 10 表示国王/王后等)

 String cardName = "four of hearts" (or whatever the variable name is);
   String[] parts = string.split(" ");
   String number = parts[0];

因此,数字将仅等于“四”,而不必比较“红心四”来返回数字 4。

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多