1

我已经学习 Java 1 个月了。这一次,我想创建一个扑克游戏。关于我的程序有两个问题。我希望有人可以帮助我解决它。

  1. 每张牌都有它的价值,A 为 1,king 为 10,但我发现这是一个字符串数组。我如何将每个“字符串值”作为“int 值”来进行操作?

    String[] suits = { "hearts", "spades", "diamonds", "clubs" };
    String[] number = {"A","2","3"......};
    
  2. 我希望这个系统第一次可以使用随机数来选择卡片,第二次运行时可以取消之前的号码(有52张卡片)。我可以做些什么来解决这个问题吗?

4

4 回答 4

2

对于这种分类,您应该考虑的是枚举:

public enum Suit {
  Hearts, Spades, Diamonds, Clubs;
}
public enum Card {
  A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K;
}

这有利于您的情况,因为枚举可以很容易地分配给您喜欢的任何值,然后可以根据该值进行比较。事实上,Java 文档提供了一个纸牌游戏来介绍 enums

于 2012-03-04T12:42:18.917 回答
1

对于您的第一个问题,您可以将卡名称映射到其值:

Map<String, Integer> values = new HashMap<String, Integer>();
values.put("A", 11);
values.put("2", 2);
// ...
values.put("King", 10);

// lookup a value:
int kingValue = values.get("King");

其次,我会依赖 java shuffle函数。创建所有卡片的列表

ArrayList<Card> deck = someMagicMethodToPopulateTheList();

然后简单地洗牌

Collections.shuffle(deck);

现在你可以遍历牌组,你会看到随机顺序的牌:

for (Card card:deck) {
  System.out.println(card);
}

注意:Card该类不存在(尚)。我只是为了简单起见而发明了这个名字)

于 2012-03-04T12:40:40.090 回答
1

我不认为你想要一个用于卡片的 String 数组,你真正需要的是一个enum. enum这是关于类型 的一个很好的教程:http: //docs.oracle.com/javase/tutorial/java/javaOO/enum.html

此外,这是在 Java 中生成随机数的方法:http: //javamex.com/tutorials/random_numbers/

于 2012-03-04T12:42:03.537 回答
0

这可能超出了您的需要,但是您可以使用枚举做很多事情:

package sample;
import java.util.*;
enum Rank { 
    joker(new Character('*'),"Joker"),aceLow(new Character('a'),"Ace(low)"),deuce(new Character('2'),"Deuce"),trey(new Character('3'),"Trey"),four(new Character('4'),"Four"),five(new Character('5'),"Five"),six(new Character('6'),"Six"),seven(new Character('7'),"Seven"),eight(new Character('8'),"Eight"),nine(new Character('9'),"Nine"),ten(new Character('T'),"Ten"),jack(new Character('J'),"Jack"),queen(new Character('Q'),"Queen"),king(new Character('K'),"King"),aceHigh(new Character('A'),"Ace");
    Rank(final Character c,final String s) {
        this.c=c;
        this.s=s;
    }
    public static Rank fromInt(int n) {
        for(Rank r:Rank.values())
            if(r.ordinal()==n)
                return r;
        return null;
    }
    public static Rank fromCharacter(char c) {
        for(Rank r:Rank.values())
            if(r.c==c)
                return r;
        return null;
    }
    public static Rank[] fromCharacters(final String cards) {
        final Rank[] rank=new Rank[5];
        for(int i=0;i<5;i++)
            rank[i]=Rank.fromCharacter(cards.charAt(i));
        return rank;
    }
    public Character toCharacter() {
        return c;
    }
    public String toString() {
        return s;
    }
    public static String toString(final Rank[] rank) {
        String s="";
        for(Rank r:rank)
            s+=r.toCharacter();
        return s;
    }
    final Character c;
    final String s;
}
enum Suit {
    joker(new Character('*'),"Joker"),clubs(new Character('c'/* try character for a real dimond? */),"Club"),diamonds(new Character('d'),"Diamond"),hearts(new Character('h'),"Heart"),spades(new Character('s'),"Spade");
    Suit(final Character c,final String s) {
        this.c=c;
        this.s=s;
    }
    public static Suit fromCharacter(char c) {
        for(Suit s:Suit.values())
            if(s.c==c)
                return s;
        return null;
    }
    public Character toCharacter() {
        return c;
    }

    public String toString() {
        return s;
    }
    public static String toString(final Suit[] suit) {
        String s_="";
        for(Suit s:suit)
            s_+=s.toCharacter();
        return s_;
    }
    public static boolean areSuited(final Suit[] suit) {
        final int n=suit.length;
        for(int i=0;i<n-1;i++)
            if(suit[i]!=suit[i+1])
                return false;
        return true;
    }
    final Character c;
    final String s;
}
enum Card {
    joker(Rank.joker,Suit.joker),
    aceLowOfClubs(Rank.aceLow,Suit.clubs),
    aceLowOfDiamonds(Rank.aceLow,Suit.diamonds),
    aceLowOfHearts(Rank.aceLow,Suit.hearts),
    aceLowOfSpades(Rank.aceLow,Suit.spades),
    dueceOfClubs(Rank.deuce,Suit.clubs),
    dueceOfDiamonds(Rank.deuce,Suit.diamonds),
    dueceOfHearts(Rank.deuce,Suit.hearts),
    dueceOfSpades(Rank.deuce,Suit.spades),
    treyOfClubs(Rank.trey,Suit.clubs),
    treyOfDiamonds(Rank.trey,Suit.diamonds),
    treyOfHearts(Rank.trey,Suit.hearts),
    treyOfSpades(Rank.trey,Suit.spades),
    fourOfClubs(Rank.four,Suit.clubs),
    fourOfDiamonds(Rank.four,Suit.diamonds),
    fourOfHearts(Rank.four,Suit.hearts),
    fourOfSpades(Rank.four,Suit.spades),
    fiveOfClubs(Rank.five,Suit.clubs),
    fiveOfDiamonds(Rank.five,Suit.diamonds),
    fiveOfHearts(Rank.five,Suit.hearts),
    fiveOfSpades(Rank.five,Suit.spades),
    sixOfClubs(Rank.six,Suit.clubs),
    sixOfDiamonds(Rank.six,Suit.diamonds),
    sixOfHearts(Rank.six,Suit.hearts),
    sixOfSpades(Rank.six,Suit.spades),
    sevenOfClubs(Rank.seven,Suit.clubs),
    sevenOfDiamonds(Rank.seven,Suit.diamonds),
    sevenOfHearts(Rank.seven,Suit.hearts),
    sevenOfSpades(Rank.seven,Suit.spades),
    eightOfClubs(Rank.eight,Suit.clubs),
    eightOfDiamonds(Rank.eight,Suit.diamonds),
    eightOfHearts(Rank.eight,Suit.hearts),
    eightOfSpades(Rank.eight,Suit.spades),
    nineOfClubs(Rank.nine,Suit.clubs),
    nineOfDiamonds(Rank.nine,Suit.diamonds),
    nineOfHearts(Rank.nine,Suit.hearts),
    nineOfSpades(Rank.nine,Suit.spades),
    tenOfClubs(Rank.ten,Suit.clubs),
    tenOfDiamonds(Rank.ten,Suit.diamonds),
    tenOfHearts(Rank.ten,Suit.hearts),
    tenOfSpades(Rank.ten,Suit.spades),
    jackOfClubs(Rank.jack,Suit.clubs),
    jackOfDiamonds(Rank.jack,Suit.diamonds),
    jackOfHearts(Rank.jack,Suit.hearts),
    jackOfSpades(Rank.jack,Suit.spades),
    queenOfClubs(Rank.queen,Suit.clubs),
    queenOfDiamonds(Rank.queen,Suit.diamonds),
    queenOfHearts(Rank.queen,Suit.hearts),
    queenOfSpades(Rank.queen,Suit.spades),
    kingOfClubs(Rank.king,Suit.clubs),
    kingOfDiamonds(Rank.king,Suit.diamonds),
    kingOfHearts(Rank.king,Suit.hearts),
    kingOfSpades(Rank.king,Suit.spades),
    aceOfClubs(Rank.aceHigh,Suit.clubs),
    aceOfDiamonds(Rank.aceHigh,Suit.diamonds),
    aceOfHearts(Rank.aceHigh,Suit.hearts),
    aceOfSpades(Rank.aceHigh,Suit.spades);
    private Card(Rank rank,Suit suit) {
        this.rank=rank;
        this.suit=suit;
    }

    public Rank rank() {
        return rank;
    }
    public Suit suit() {
        return suit;
    }
    public String toString() {
        if(rank.equals(Rank.joker)||suit.equals(Suit.joker))
            return "Joker";
        else return rank+" of "+suit+'s';
    }
    public static String toString(final Card[] card) {
        String s=new String();
        for(int i=0;i<card.length;i++) {
            if(i>0)
                s+=", ";
            s+=card[i].toCharacters();
        }
        return s;
    }
    public String toCharacters() {
        return ""+rank().toCharacter()+suit().toCharacter();
    }
    public static Card instance(Rank rank,Suit suit) { // some hack for speed
        final int n=4*rank.ordinal()+suit.ordinal();
        //System.out.println(rank+" "+suit+" "+n);
        return element[4*rank.ordinal()+suit.ordinal()];
    }
    static Card[] create(int standards,int jokers) {
        int cards=0;
        Card card[]=new Card[standards*52+jokers];
        for(int i=0;i<standards;i++)
            for(Suit suit:EnumSet.range(Suit.clubs,Suit.spades))
                for(Rank rank:EnumSet.range(Rank.deuce,Rank.aceHigh))
                    card[cards++]=instance(rank,suit);
        for(int i=0;i<jokers;i++)
            card[cards++]=instance(Rank.joker,Suit.joker);
        return card;
    }
    private final Rank rank;
    private final Suit suit;
    private static final Card element[]={joker,joker,joker,joker,joker,aceLowOfClubs,aceLowOfDiamonds,aceLowOfHearts,aceLowOfSpades,dueceOfClubs,dueceOfDiamonds,dueceOfHearts,dueceOfSpades,treyOfClubs,treyOfDiamonds,treyOfHearts,treyOfSpades,fourOfClubs,fourOfDiamonds,fourOfHearts,fourOfSpades,fiveOfClubs,fiveOfDiamonds,fiveOfHearts,fiveOfSpades,sixOfClubs,sixOfDiamonds,sixOfHearts,sixOfSpades,sevenOfClubs,sevenOfDiamonds,sevenOfHearts,sevenOfSpades,eightOfClubs,eightOfDiamonds,eightOfHearts,eightOfSpades,nineOfClubs,nineOfDiamonds,nineOfHearts,nineOfSpades,tenOfClubs,tenOfDiamonds,tenOfHearts,tenOfSpades,jackOfClubs,jackOfDiamonds,jackOfHearts,jackOfSpades,queenOfClubs,queenOfDiamonds,queenOfHearts,queenOfSpades,kingOfClubs,kingOfDiamonds,kingOfHearts,kingOfSpades,aceOfClubs,
        aceOfDiamonds,aceOfHearts,aceOfSpades};
}
于 2012-03-07T20:10:59.890 回答