我的牌Shuffler无法正常工作,任何一个人都能分辨出Shuffler的模糊性

时间:2014-08-24 08:31:03

标签: java android

尝试使用随机编号随机播放一张牌组的52张牌。但这会返回给定的相同列表。 cardS Deck = 52;

public ArrayList<Card> cardShuffler() {
    int newI;
    Card temp,temp2,temp3;
    Random randIndex = new Random();

    for (int i = 0; i < cardsInDeck; i++) {
        newI = randIndex.nextInt(cardsInDeck);
        Log.i("Nulll", String.valueOf(newI));
        temp = cards.get(i);
        temp2= cards.get(newI);

        //temp3 = temp;
        //temp = temp2;
        //temp2 = temp3;
        cards.set(i, temp2);
        cards.set(newI, temp);

    }
    return cards;
}

3 个答案:

答案 0 :(得分:3)

考虑使用Collections.shuffle()

你可以像这样使用它:

// assume `cards` is a `List`, 
Collections.shuffle(cards); 
// cards is now shuffled

对于好奇的人,请查看第514行的http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java

答案 1 :(得分:1)

我认为问题不在您提供的代码中。试试这个(添加日志记录和卡片初始化),它可以工作:

private void tryToSuffle()
{
    int newI;
    Integer temp,temp2,temp3;
    Random randIndex = new Random();


    //initializing data.
    Integer cardsInDeck = 52;
    ArrayList<Integer> cards = new ArrayList<Integer>();
    for (int i =0; i < cardsInDeck; i++) {
        cards.add(i);
    }
    //....

    //no change in the algorythm itself
    for (int i = 0; i < cardsInDeck; i++) {
        newI = randIndex.nextInt(cardsInDeck);

        temp = cards.get(i);
        temp2= cards.get(newI);

        cards.set(i, temp2);
        cards.set(newI, temp);

    }

    //printing the result.
    for (int i =0; i < cardsInDeck; i++) {
        System.out.println(cards.get(i));
    }
}

因此,如果不改变主要的算法,它就按计划运作。 所以可能你没做的就是将返回值赋给适当的变量。为什么在此方法中使用某个实例字段cards?难道你不能把它们作为论点传递出来吗?

修改 只是为了支持我上面所写的内容,我做了这个测试,并且它可以工作(不改变洗牌算法):

public class ShufflerTest {

    private final Integer cardsInDeck = 52;
    public static void main(String args[])
    {
        new ShufflerTest().run();
    }

    private void run() {
        ArrayList<Integer> cards = new ArrayList<Integer>();
        for (int i =0; i < cardsInDeck; i++) {
            cards.add(i);
        }

        cards = tryToShuffle(cards);

        //printing the result.
        System.out.println(cards.toString());
    }

    private ArrayList<Integer> tryToShuffle(final ArrayList<Integer> cards)
    {
        int newI;
        Integer temp,temp2;
        Random randIndex = new Random();

        ArrayList<Integer> shuffledCards = new ArrayList<Integer>();
        shuffledCards.addAll(cards);

        for (int i = 0; i < cardsInDeck; i++) {
            newI = randIndex.nextInt(cardsInDeck);

            temp = shuffledCards.get(i);
            temp2= shuffledCards.get(newI);

            shuffledCards.set(i, temp2);
            shuffledCards.set(newI, temp);

        }

        return shuffledCards;
    }
}

在示例中,我添加了shuffledCards数组,而不是更改原始cards数组。这是一个很好的做法,不要混淆输入param集合,因为你不知道其他类是否也使用它。但如果你决定不再使用这个额外的shuffledCards变量,那么它也会起作用。

我的示例输出是(我已将其更改为cards.toString()替换循环): [46,16,23,21,28,8,37,4,47,17,9,41,51,30,20,26,10,3,2,12,29,40,25,33,34,34 ,42,15,27,32,43,39,6,22,45,31,35,48,13,5,1,12,19,49,50,44,11,7,8,18,24 ,38,36]

答案 2 :(得分:0)

你需要这个修复:

public ArrayList<Card> cardShuffler() {
    int newI;
    Card temp,temp2,temp3;
    Random randIndex = new Random();

    for (int i = 0; i < cardsInDeck; i++) {
        newI = randIndex.nextInt(cardsInDeck - i) + i; // fixed here
        Log.i("Nulll", String.valueOf(newI));
        temp = cards.get(i);
        temp2= cards.get(newI);

        //temp3 = temp;
        //temp = temp2;
        //temp2 = temp3;
        cards.set(i, temp2);
        cards.set(newI, temp);

    }
    return cards;
}