2012-03-11 25 views
1

我在寫一些代碼時遇到了一些麻煩。基本上,我試圖像一副撲克牌一樣「洗牌」堆棧集合,但由於某種原因,我使用的臨時堆棧中的一個不會完全清空,這會在下一次運行時導致一個空集合異常周圍。我手動追蹤代碼和輸出,並將元素留在臨時堆棧1中(代碼如下)。我真的不知道爲什麼會發生這種情況!如果你對此有所瞭解,這將會非常有幫助。當Fisher-Yates洗牌時Java堆棧中剩下的元素?

這是解決問題的方法鏈接:http://pastebin.com/cxJCmemZ

public void shuffleCards(LinkedStack<UnoCard> deck) { 
     int tempIndex; 
     LinkedStack<UnoCard> tempCardStack1 = new LinkedStack<UnoCard>(); 
     LinkedStack<UnoCard> tempCardStack2 = new LinkedStack<UnoCard>(); 

     //Fisher-Yates shuffle 
     for (int i = (deck.size() - 1); i >= 0; i--) { 
      tempIndex = ((int)(i * Math.random())); 

      System.out.println("i is: " + i); 
      System.out.println("tempIndex is: " + tempIndex); 

      //swap if cards are different 
      if (tempIndex != i) { 
       //pop face down cards up to first card onto temporary stack 
       System.out.println("Popping up to first card"); 
       for(int j = 0; j <= tempIndex; j++) { 
        UnoCard tempCard = faceDownCards.pop(); 
        System.out.println(tempCard.toString()); 
        tempCardStack1.push(tempCard); 
       } 

       //pop face down cards up to second card onto temporary stack 
       System.out.println("Popping up to second card"); 
       for(int j = (tempIndex + 1); j <= i; j++) { 
        UnoCard tempCard = faceDownCards.pop(); 
        System.out.println(tempCard.toString()); 
        tempCardStack2.push(tempCard); 
       } 

       //replace first card in second card position 
       System.out.println("Replacing first card"); 
       UnoCard tempCard = tempCardStack1.pop(); 
       System.out.println(tempCard.toString()); 
       faceDownCards.push(tempCard); 

       //place second card in temporary stack 
       System.out.println("Transferring second card"); 
       tempCard = tempCardStack2.pop(); 
       System.out.println(tempCard.toString()); 
       tempCardStack1.push(tempCard); 

       //replace temporary stack 
       System.out.println("Replacing second stack"); 
       for(int j = 0; j < tempCardStack2.size(); j++) { 
        tempCard = tempCardStack2.pop(); 
        System.out.println(tempCard.toString()); 
        faceDownCards.push(tempCard); 
       } 

       //replace second card in first card position 
       System.out.println("Replacing second card"); 
       tempCard = tempCardStack1.pop(); 
       System.out.println(tempCard.toString()); 
       faceDownCards.push(tempCard); 

       //replace temporary stack 
       System.out.println("Replacing first stack"); 
       for(int j = 0; j < tempCardStack1.size(); j++) { 
        tempCard = tempCardStack1.pop(); 
        System.out.println(tempCard.toString()); 
        faceDownCards.push(tempCard); 
       } 
      } 
     } 
    } 
+0

下一次你應該只是添加代碼。並不是那麼多。通常人們不希望遵循代碼鏈接(特別是因爲該鏈接可能會在未來消失,從而使問題和答案的價值較低)。 – AHungerArtist 2012-03-12 00:03:38

+0

對不起,我是新來的。謝謝你糾正這個! – lollercopter 2012-03-12 00:18:58

回答

2

如果你彈出堆棧大小減小,因此for循環只會爲大小/ 2次運行

因此結束循環應真的是

while(!tempCardStack1.isEmpty()){ 
    tempCard = tempCardStack1.pop(); 
    System.out.println(tempCard.toString()); 
    faceDownCards.push(tempCard); 
}