1

Java2クラスのプロジェクトを開始したばかりで、完全に停止しました。私はこの方法に頭を悩ませることができません。特に、割り当てで他のDATASTRUCTUREメソッドやJavaのシャッフルメソッドをまったく使用できない場合。

したがって、52枚のカードを保持する52個のノードを含むリンクリストをすでに作成したDeck.classがあります。

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枚のカードをシャッフルするようになりましたが、それが行われるたびに、それらを正しい方法でシャッフルすることはできませんでした。1枚のカードが常に繰り返されました。

  • 別の時点で、52枚のカードすべてにwhileループを通過させましたが、1枚のカードを何度も繰り返しました。

だから私は本当に私が間違っていることについていくつかの入力が必要です。コードの終わりに向かって、私のロジックは完全に間違っていると思いますが、それを回避する方法を理解できないようです。

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

私の実装では、分割統治アルゴリズムを使用してリンクされたリストをシャッフルします

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

二次データ構造の使用が許可されている場合、残りのカードの数内で乱数を計算し、そのカードを選択し、空になるまで二次構造の最後に移動してから、リストを二次構造で置き換えるだけです。リスト。

于 2013-03-25T07:55:47.453 に答える
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 に答える