简体   繁体   English

如何创建随机牌组?

[英]How to create a randomized deck of cards?

I need to create a deck of cards by using two string: "HSCD" and "A2345678910JQK".我需要使用两个字符串创建一副牌:“HSCD”和“A2345678910JQK”。

public class Deck {
Random random=new Random();
Queue cards=new Queue(112);
String suits="HSCD";
String rands="A2345678910JQK";

public Deck() {
    for (int i = 0; i < suits.length(); i++) {
        for (int j = 0; j < rands.length(); j++) {
            char suit = suits.charAt(random.nextInt(suits.length()));
            char rand = rands.charAt(random.nextInt(rands.length()));
            if (rand == '1' || rand == '0') {
                String s = Integer.toString(10);
                cards.enqueue(new Card(suit, s));
            } else {
                String s1 = Character.toString(rand);
                cards.enqueue(new Card(suit, s1));
            }
        }
    }
}
public void display(){
    for (int i = 0; i < cards.size(); i++) {
        System.out.print(cards.peek());
        cards.enqueue(cards.dequeue());
    }
}

public Queue getCards() {
    return cards;
}

public void setCards(Queue cards) {
    this.cards = cards;
}

public String getSuits() {
    return suits;
}

public void setSuits(String suits) {
    this.suits = suits;
}

public String getRands() {
    return rands;
}

public void setRands(String rands) {
    this.rands = rands;
}}

I have Deck and Card classes.我有甲板和卡片课程。

public class Card {

private char rand;
private String suit;

public Card(char rand, String suit) {
    this.rand = rand;
    this.suit = suit;
}

public Card(){}

public char getRand() {
    return rand;
}

public void setRand(char rand) {
    this.rand = rand;
}

public String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public String toString(){
    return "\t"+rand + suit;
}}

But i couldn't solve that every suits must have 13 rands.但我无法解决每套西装必须有 13 兰特的问题。 In my program my deck is randomly created.在我的程序中,我的套牌是随机创建的。 And my deck must be shuffled.我的牌组必须洗牌。 I can't use list or array structures because teacher told us so:).我不能使用列表或数组结构,因为老师告诉我们:)。 Can you help me?你能帮助我吗?

  1. You are creating 14 cards per suit, not 13: you're creating 10 twice.每套花色创造 14 张牌,而不是 13 张:你创造了10张牌两次。 Remove with 0 or 1 from rands .01rands中删除。

  2. It will be easier if you first create the cards, then shuffle them.如果您先创建卡片,然后将它们洗牌,会更容易。

Creation of cards should be similar to what you're already doing, minus the randomization - just go through suits and values in order:卡片的创建应该与您已经在做的类似,减去随机化 - 只需 go 依次通过花色和值:

for each suit:
  for each value:
    sortedCards.add(new Card(suit, value));

Then, shuffle the cards as follows:然后,按如下方式洗牌:

while (sortedCards is not empty):
  shuffledCards.add(sortedCards.get(random.nextInt(sortedCards.size())))

Just duplicate four time the second string.只需将第二个字符串复制四次。 After you take two random numbers one correspond to the sign and one to the number.取两个随机数后,一个对应符号,一个对应数字。 Finally you put sign+number in memory and you remove the number in the string:最后将符号+数字放入 memory 并删除字符串中的数字:

String signs = "HSCD";
String numbersH = "A2345678910JQK";
String numbersS = "A2345678910JQK";
String numbersC = "A2345678910JQK";
String numbersD = "A2345678910JQK";
ArrayList<String> deck = new ArrayList<String>();
Random random = new Random();
while(deck.size()<52){
   int sign = random.nextInt(signs.lenght());
   if(sign==0 && numbersH.lenght()>0){
      int number = random.nextInt(numbersH.lenght());
      deck.add(signs.charAt(sign)+numbersH.charAt(number));
      numbersH = numbersH.substring(0,number)+numberH.substring(number+1);
   }//here the same for other signs

}

You haven't included the Queue class so help is going to be limited.您还没有包括Queue class 所以帮助将是有限的。 Here are some other suggestions.这里有一些其他的建议。

        String suits="HSCD";
        String ranks="A23456789TJQK";
        for (int i = 0; i < 52; i++) {
            int suit = i/13; // index into suit 0 to 3 inclusive
            int rank = i%13; // index into rank 0 to 12 inclusive
            cards.enqueue(new Card(suits.charAt(suit), ranks.charAt(rank));
        }

Also, quite often, card game software uses a T for ten, which is what I did in this example.此外,纸牌游戏软件经常使用T表示 10,这就是我在这个示例中所做的。 It can be changed but in your implementation you can't store 10 as as a single character and it is cumbersome to maintain two types for card ranks.它可以更改,但在您的实现中,您不能将 10 作为单个字符存储,并且为卡等级维护两种类型很麻烦。 So you should probably make all your ranks and suits as type String .因此,您可能应该将所有等级和西装设为String类型。

Another method: Create your deck like iluxa say you and shuffle with this:另一种方法:像 iluxa 所说的那样创建你的牌组,然后用这个洗牌:

Random rand = new Random();
for(int i=0;i<300;i++){
   int a = rand.nextInt(deck.size());
   int b = rand.nextInt(deck.size());
   Card cA = deck.get(a);
   deck.set(a, deck.get(b));
   deck.set(b, cA);
}

That's code will suffle your deck by switch two random cards many times.那是代码将通过多次切换两张随机卡来打乱你的牌组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM