2

我正在设计一个二十一点计划。我的问题是,我使用开关盒来生成随机卡。但是当需要比较卡片的价值时......可以说它if(pCard1 + pCard2 > 21)不允许我比较它们,因为它们是字符串。但是,它们必须是字符串,因为我必须能够分配String "Queen",但我还需要“Queen”的 int 值为 10。我不知道该怎么做。

这是我到目前为止所拥有的。我没有包括我的整个代码,因为它有几百行长。仅包括二十一点部分。

System.out.println("---------------------Black Jack--------------------------");
    System.out.println("Welcome to BlackJack!");
    System.out.println("Available balance is $"+balance);
    System.out.print("How much would you like to bet on this hand?: ");
    int bet = input.nextInt();
    balance -= bet;
    System.out.println("You just bet $"+bet+"......Dealing cards!");
    System.out.println("----------------------------------------------------------");

    String pCard1 = dealCard();
    String pCard2 = dealCard();

    System.out.println("Your hand is a "+pCard1+" and "+pCard2);
    System.out.print("Would you like to Hit or Stand? :");
    String hOs = input.next();

    if(hOs.equals("Hit")) {
        String pCard3 = dealCard();
        if(pCard1 + pCard2 + pCard3);
    }
    }
}
public static String dealCard() {
    int value = (int)(Math.random() * 13);
    String returnString = "";
    switch ( value ) {
        case 1:   returnString = "Ace"; break;
        case 2:   returnString = "2"; break;
        case 3:   returnString = "3"; break;
        case 4:   returnString = "4"; break;
        case 5:   returnString = "5"; break;
        case 6:   returnString = "6"; break;
        case 7:   returnString = "7"; break;
        case 8:   returnString = "8"; break;
        case 9:   returnString = "9"; break;
        case 10:  returnString = "10"; break;
        case 11:  returnString = "Jack"; break;
        case 12:  returnString = "Queen"; break;
        case 13:  returnString = "King"; break;
    }
    return returnString;
  }
}    
4

3 回答 3

3

我认为枚举是解决此类问题的最佳方法。

如果我们像这样为 Suit 和 Pips 定义枚举

enum Suit {
    Hearts, Diamonds, Clubs, Spades;
}

enum Pips {
    Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King;
}

如果我们定义一个类 Card:

class Card {
    Suit suit;
    Pips pips;

    public Card(Suit suit, Pips pips) {
        this.suit = suit;
        this.pips = pips;
    }

    @Override
    public String toString() {
        return pips.toString() + " of " + suit.toString();
    }
}

Card用于toString()将 Suit 和 Pips 转换为字符串,而可以使用以下方法获得零索引数值ordinal()

    Card card = new Card(Suit.Spades, Pips.Ace);
    System.out.println("Card: " + card);
    System.out.println("Numeric suit: " + card.suit.ordinal());
    System.out.println("Numeric pips: " + card.pips.ordinal());

这个简单的实现有几个限制。首先,花色或点子的名称只能是枚举名称的字符串版本。如果您希望所有枚举都以大写形式呈现,该怎么办?第二个问题是序数总是零索引的。如果您需要其他一些 id 系统来获取点数怎么办?

在这种情况下,我们将映射添加到每个枚举以保存这些映射。不幸的是,我们不能使用继承在枚举之间共享代码,所以样板文件变得有点罗嗦。

enum Suit {
    HEARTS(1, "Hearts"), //
    DIAMONDS(2, "Diamonds"), //
    CLUBS(3, "Clubs"), //
    SPADES(4, "Spades");

    private static Map<String, Suit> nameToEnum = new HashMap<>();
    private static Map<Integer, Suit> idToEnum = new HashMap<>();

    static {
        for (Suit suit : Suit.values()) {
            nameToEnum.put(suit.getName(), suit);
            idToEnum.put(suit.getId(), suit);
        }
    }

    private int id;
    private String name;

    private Suit(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public static Suit fromName(String name) {
        return nameToEnum.get(name);
    }

    public static Suit fromId(int id) {
        return idToEnum.get(id);
    }
}

enum Pips {
    ACE(1, 1, "Ace"), //
    TWO(2, 2, "Two"), //
    THREE(3, 3, "Three"), //
    FOUR(4, 4, "Four"), //
    FIVE(5, 5, "Five"), //
    SIX(6, 6, "Six"), //
    SEVEN(7, 7, "Seven"), //
    EIGHT(8, 8, "Eight"), //
    NINE(9, 9, "Nine"), //
    TEN(10, 10, "Ten"), //
    JACK(11, 10, "Jack"), //
    QUEEN(12, 10, "Queen"), //
    KING(13, 10, "King");

    private static Map<String, Pips> nameToEnum = new HashMap<>();
    private static Map<Integer, Pips> idToEnum = new HashMap<>();

    static {
        for (Pips pips : Pips.values()) {
            nameToEnum.put(pips.getName(), pips);
            idToEnum.put(pips.getId(), pips);
        }
    }

    private int id;
    private int value;
    private String name;

    private Pips(int id, int value, String name) {
        this.id = id;
        this.value = value;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public static Pips fromName(String name) {
        return nameToEnum.get(name);
    }

    public static Pips fromId(int id) {
        return idToEnum.get(id);
    }

}

使用这些新枚举,我们修改Card.toString()为使用新getName()方法:

    return pips.getName() + " of " + suit.getName();

现在,当我们使用 Cards 时,我们可以使用任何 ID 或名称或枚举并在它们之间自由转换:

    Card card = new Card(Suit.SPADES, Pips.ACE);
    System.out.println("Card: " + card);
    System.out.println("Numeric suit: " + card.suit.getId());
    System.out.println("Numeric pips: " + card.pips.getId());
    System.out.println("Suit by ID: " + Suit.fromId(3).getName());
    System.out.println("Pips by Name: " + Pips.fromName("Ace").getName());
    System.out.println("Pips value by Name: " + Pips.fromName("King").getValue());
于 2018-04-08T06:21:50.867 回答
3

我建议使用 Enum,这样您就可以轻松地比较卡片。您想要的枚举类将是:

public enum Cards {
    ace (1),
    two (2),
    three (3),
    four (4),
    five (5),
    six (6),
    seven (7),
    eight (8),
    nine (9),
    ten (10),
    jack (11),
    queen (12),
    king (13)
    ;

    private final int code;

    Cards(int code){
        this.code = code;
    }
    public Integer value(){
        return code;
    }

    public static Cards getRelatedEnum(Integer code){
        if(code == null)
            return null;

        switch (code){
            case 1: return ace;
            case 2: return two;
            case 3: return three;
            case 4: return four;
            case 5: return five;
            case 6: return six;
            case 7: return seven;
            case 8: return eight;
            case 9: return nine;
            case 10: return ten;
            case 11: return jack;
            case 12: return queen;
            case 13: return king;
            default: return null;
        }
    }

    public static String toString(Cards card){
        if(card == null)
            return "";
        switch (card){
            case ace: return "ace";
            case two: return "2";
            case three: return "3";
            case four: return "4";
            case five: return "5";
            case six: return "6";
            case seven: return "7";
            case eight: return "8";
            case nine: return "9";
            case ten: return "10";
            case jack: return "jack";
            case queen: return "queen";
            case king: return "king";
            default: return null;
        }
    }

}

那么您可以将dealCard()方法更改为:

public static Cards dealCard() {
    int value = (int)(Math.random() * 13);
    return Cards.getRelatedEnum(value);
}  

比较卡值使用类似的东西:

public static void main(String[] args){
    System.out.println("---------------------Black Jack--------------------------");
    System.out.println("Welcome to BlackJack!");
    System.out.println("Available balance is $"+balance);
    System.out.print("How much would you like to bet on this hand?: ");
    int bet = input.nextInt();
    balance -= bet;
    System.out.println("You just bet $"+bet+"......Dealing cards!");
    System.out.println("-------------------------------------------");

    Cards pCard1 = dealCard();
    Cards pCard2 = dealCard();

    System.out.println("Your hand is a "+pCard1.toString() +" and "+pCard2.toSring());
    System.out.print("Would you like to Hit or Stand? :");
    String hOs = input.next();

    if(hOs.equals("Hit")) {
        Cards pCard3 = dealCard();
        if(pCard1.value() + pCard2.value() > pCard3.value());
    }
}
于 2018-04-08T06:19:10.400 回答
2

十、杰克、皇后和国王都是不同的等级,但共享相同的值 10。由于它们都具有相同的值,因此您需要的不仅仅是一张卡的值来确定它的等级。诚然,您可以将 Jack 的值表示为 11,Queen 表示为 12 等,然后对您的方法进行编码以将任何超过 10 的值识别为 10,但这是一种不好的编码习惯。

相反,您应该对每张牌中的点数和值进行编码(此外,您可能希望包括花色;尽管这对于二十一点可能无关紧要)。

我建议不要将您的卡表示为字符串,而是创建一个具有字符串等级和整数值的 Card 类。此外,正如其他人评论的那样,这将是一个探索 Java 枚举的好机会。

于 2018-04-08T06:13:04.187 回答