1

我刚刚为我的 Java2 类启动了一个项目,但我已经完全停止了。我只是无法理解这种方法。尤其是当分配不允许我们使用任何其他 DATA STRUCTURE 或 java 中的 shuffle 方法时。

所以我有一个 Deck.class 我已经在其中创建了一个包含 52 个节点的链表,其中包含 52 张卡片。

public class Deck {

    private Node theDeck;
    private int numCards;

    public Deck () 
    {
        while(numCards < 52)
        {
            theDeck = new Node (new Card(numCards), theDeck);
            numCards++;
        }
    }

    public void shuffleDeck()
    {           
        int rNum;
        int count = 0;
        Node current = theDeck;
        Card tCard;
        int range = 0;  

        while(count != 51)
        {   
            // Store whatever is inside the current node in a temp variable
               tCard = current.getItem();

            // Generate a random number between 0 -51      
                rNum = (int)(Math.random()* 51);

            // Send current on a loop a random amount of times
               for (int i=0; i < rNum; i ++)
                current = current.getNext();   ******<-- (Btw this is the line I'm getting my error, i sort of know why but idk how to stop it.)

            // So wherever current landed get that item stored in that node and store it in the first on
            theDeck.setItem(current.getItem());

            // Now make use of the temp variable at the beginning and store it where current landed
            current.setItem(tCard);

            // Send current back to the beginning of the deck
            current = theDeck;

            // I've created a counter for another loop i want to do     
            count++;

            // Send current a "count" amount of times for a loop so that it doesn't shuffle the cards that have been already shuffled.   
            for(int i=0; i<count; i++)
             current = current.getNext();  ****<-- Not to sure about this last loop because if i don't shuffle the cards that i've already shuffled it will not count as a legitimate shuffle? i think? ****Also this is where i sometimes get a nullpointerexception****

        }

    }

}

现在,当我调用此方法时,会出现不同类型的错误:

  • 它有时只会洗 2 张牌,但有时会洗 3 - 5 张牌,然后给我一个 NullPointerException。我已经在上面的代码中用星号指出了它在哪里给了我这个错误

  • 有一次我让它洗了 13 张牌,但每次洗牌时都没有以正确的方式洗牌。一张牌一直在重复。

  • 在另一点上,我让所有 52 张卡片都通过了 while 循环,但它又多次重复了一张卡片。

所以我真的需要一些关于我做错了什么的意见。在我的代码结束时,我认为我的逻辑完全错误,但我似乎无法找到解决方法。

4

4 回答 4

2

似乎很啰嗦。

我会选择以下内容:

public void shuffleDeck() {
    for(int i=0; i<52; i++) {
        int card = (int) (Math.random() * (52-i));
        deck.addLast(deck.remove(card));
    }
}

因此,每张卡都会以随机顺序移动到牌组的后面。

于 2013-12-14T15:56:06.030 回答
0

如果您被授权使用二级数据结构,一种方法是简单地计算剩余卡片数量内的随机数,选择该卡片,将其移动到二级结构的末尾直到为空,然后用二级替换您的列表列表。

于 2013-03-25T07:55:47.453 回答
0

我的实现使用分治算法对链表进行洗牌

public class LinkedListShuffle
{
    public static DataStructures.Linear.LinkedListNode<T> Shuffle<T>(DataStructures.Linear.LinkedListNode<T> firstNode) where T : IComparable<T>
    {
        if (firstNode == null)
            throw new ArgumentNullException();

        if (firstNode.Next == null)
            return firstNode;

        var middle = GetMiddle(firstNode);
        var rightNode = middle.Next;
        middle.Next = null;

        var mergedResult = ShuffledMerge(Shuffle(firstNode), Shuffle(rightNode));
        return mergedResult;
    }

    private static DataStructures.Linear.LinkedListNode<T> ShuffledMerge<T>(DataStructures.Linear.LinkedListNode<T> leftNode, DataStructures.Linear.LinkedListNode<T> rightNode) where T : IComparable<T>
    {
        var dummyHead = new DataStructures.Linear.LinkedListNode<T>();
        DataStructures.Linear.LinkedListNode<T> curNode = dummyHead;

        var rnd = new Random((int)DateTime.Now.Ticks);
        while (leftNode != null || rightNode != null)
        {
            var rndRes =  rnd.Next(0, 2);
            if (rndRes == 0)
            {
                if (leftNode != null)
                {
                    curNode.Next = leftNode;
                    leftNode = leftNode.Next;
                }
                else
                {
                    curNode.Next = rightNode;
                    rightNode = rightNode.Next;
                }
            }
            else
            {
                if (rightNode != null)
                {
                    curNode.Next = rightNode;
                    rightNode = rightNode.Next;
                }
                else
                {
                    curNode.Next = leftNode;
                    leftNode = leftNode.Next;
                }
            }

            curNode = curNode.Next;                     
        }
        return dummyHead.Next;
    }

    private static DataStructures.Linear.LinkedListNode<T> GetMiddle<T>(DataStructures.Linear.LinkedListNode<T> firstNode) where T : IComparable<T>
    {
        if (firstNode.Next == null)
            return firstNode;

        DataStructures.Linear.LinkedListNode<T> fast, slow;
        fast = slow = firstNode;
        while (fast.Next != null && fast.Next.Next != null)
        {
            slow = slow.Next;
            fast = fast.Next.Next;
        }
        return slow;
    }
}
于 2015-01-15T12:29:29.683 回答
0

刚刚遇到这个并决定发布一个更简洁的解决方案,它允许您指定要进行多少洗牌。

出于答案的目的,您有一个包含 PlayingCard 对象的链接列表;

LinkedList<PlayingCard> deck = new LinkedList<PlayingCard>();

并使用这样的东西来洗牌;

public void shuffle(Integer swaps) {    
    for (int i=0; i < swaps; i++) {
        deck.add(deck.remove((int)(Math.random() * deck.size())));
    }                       
}

你做的交换越多,列表就越随机。

于 2017-11-02T15:20:18.467 回答