简体   繁体   English

如何洗牌存放在阵列中的一副牌?

[英]How to shuffle a deck of cards memorised in an array?

I was developing a simple application that It is simple game of cards, and I had created an array which contains all the card of the game but the problem is that I don't know why I can't shuffle this array? 我正在开发一个简单的应用程序,它是简单的游戏卡,我创建了一个包含游戏所有卡的数组,但问题是我不知道为什么我不能洗牌这个数组?

I tried to use the RANDOM but I didn't succeed. 我试图使用RANDOM,但我没有成功。

public class Mazzo {
    private Carta[] carteNelMazzo ;

    public Mazzo(){// create the deck
        carteNelMazzo = creaMazzo();
        mescolaMazzo(carteNelMazzo);
    }

   /**methods of the deck */

    public Carta PescaCarta (){

        return (carteNelMazzo==null||(carteNelMazzo.length>0)) ? pescaCarta(carteNelMazzo) : null;
    }

    public Carta pescaBriscola(){
        return (carteNelMazzo==null||(carteNelMazzo.length>0)) ? carteNelMazzo[carteNelMazzo.length-1] : null;
    }
    /**
     * @param carte deve avere lunghezza maggiore uguale ad 1
     * @return la prima carta del mazzo
     */
    private Carta pescaCarta(Carta[] carte){
        Carta[] nuoveCarte=new Carta[carte.length-1];
        Carta pescata= carte[0];
        System.arraycopy(carte,1,nuoveCarte,0,carte.length);
        carte = nuoveCarte;
        return pescata;
    }

    private Carta[] creaMazzo(){
        ArrayList<Carta> nuovoMazzo=new ArrayList<>();
        for(int i =0; i<4; i++){
            // selezione del seme

            for(int j = 0;j<10;j++){
                // creation of the card from another calss
                Carta nuovaCarta= new Carta(Carta.SEME.values()[i],j);
                nuovoMazzo.add(nuovaCarta);
            }
        }
        return (Carta[]) nuovoMazzo.toArray();
    }

//shuffle deck

    private void mescolaMazzo(Carta[] carte){

       Random rand = new Random();
       int elements = (int) (40 * Math.random());

       int elements = carteNelMazzo.length;
    }
}

At the end I want this array with all the cards remix at random. 最后,我希望这个数组与所有卡片随机混音。

This here: 这里:

int elements = (int) (40 * Math.random());
int elements = carteNelMazzo.length;

doesn't shuffle anything. 什么都不洗牌。 It assigns a random number to a local variable which is then thrown away. 它将随机数分配给局部变量,然后将其丢弃。 ( I actually think this shouldn't even compile, as you declare the same local variable twice in the same scope ) (我实际上认为这甚至不应该编译,因为你在同一范围内声明了两次相同的局部变量)

What you need instead: to create a function that maps your 40 indexes to new values, like: 您需要的是:创建一个将40个索引映射到新值的函数,例如:

0, 1, 3, ... 39 

becomes

14, 2, 7, ...

An easy way to get there: Collections.shuffle(someList); 到达那里的简单方法: Collections.shuffle(someList); .

For more ideas (also using arrays directly), see here . 有关更多想法(也直接使用数组),请参阅此处

But as you are probably doing this to learn things, I suggest you carefully digest what I told you upfront. 但是你可能正在做这件事来学习,我建议你仔细消化我刚才告诉你的内容。 Start by thinking how you would could shuffle a list of cards "manually" (not by touching them, but when you are told the order they have how you could "mentally" re-order them). 首先想一想你如何“手动”洗牌(不是通过触摸它们,但是当你被告知他们的订单时他们会如何“精神上”重新订购它们)。 From there, think how you could instruct a computer to do that. 从那里开始,想想如何指导计算机这样做。

采用:

Collections.shuffle(<<arrayname>>);

You can try something like this: 你可以尝试这样的事情:

private void shuffleCards (Card[] cards) {

    for (int i = 0; i < cards.length; i++) {
        Card temp = cards[i];

        //random index of the array
        int rnd = Math.floor(Math.random() * cards.length);
        cards[i] = cards[rnd];
        cards[rnd] = temp;
    }
}

PS.: if this code throws an ArrayIndexOutOfBoundsException change the line int rnd = Math.floor(Math.random() * cards.length); PS。:如果此代码抛出ArrayIndexOutOfBoundsExceptionint rnd = Math.floor(Math.random() * cards.length); to int rnd = Math.floor(Math.random() * (cards.length - 1)); to int rnd = Math.floor(Math.random() * (cards.length - 1));

Here's one I learned many years ago. 这是我多年前学到的。

  int[] cards = IntStream.rangeClosed(0, 51).toArray();
  Random r = new Random();

  for (int i = cards.length - 1; i >= 0; i--) {
     int idx = r.nextInt(i + 1);
     int card = cards[idx];
     cards[idx] = cards[i];
     cards[i] = card;
  }

And then there's always the Collections.shuffle() class. 然后总是有Collections.shuffle()类。

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

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