2013-03-23 22 views
0

我是新來的動作3.0,我試圖做一副牌洗牌,我已經成功了這個,但我的問題是,我的牌正在重複,所以我有52張牌的同一張牌的副本洗牌。我正在嘗試創建一個德州撲克遊戲。actionscript 3.0如何將算法合併到我的代碼中?

我發現這個討論Randomize or shuffle an array但它並沒有告訴我如何將Fisher-Yates算法合併到我的代碼中。我已經嘗試了幾種不同的方法,這裏提出了一些建議,以及在網絡上的什麼地方,沒有任何工作(認爲這個問題肯定是我缺乏經驗)。

有人可以請給我一個如何將其納入我的代碼或鏈接到某個地方,將解釋如何正確地做到這一點的例子。

在此先感謝。

保羅

package src.CardDeck 
{ 
    public class CardDeck 
    { 
     public var allCards:Array = []; 
     public var cardNames:Array; 
     public var cardValues:Array; 
     public var gameType:String; 
     public var drawnCards:uint = 0; 

     public function CardDeck(game:String) 
     { 
      gameType = game; 
      cardNames = ["Ace","Two","Three", 
         "Four","Five","Six", 
         "Seven","Eight","Nine", 
         "Ten","Jack","Queen","King"]; 
      if(gameType == "texasholdem") 
      { 
       cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10]; 
      } 
      makeSuit("Spade"); 
      makeSuit("Heart"); 
      makeSuit("Diamond"); 
      makeSuit("Club"); 
     } 

     private function makeSuit(suitString:String):void 
     { 
      var card:Object; 

      for(var i:uint = 0; i < cardNames.length; i++) 
      { 
       card = {}; 
       card.cardType = suitString; 
       card.cardName = cardNames[i]; 
       card.cardValue = cardValues[i]; 
       card.isDrawn = false; 
       allCards.push(card); 
      } 
     } 

     public function shuffle():Array 
     { 
      var shuffledArray:Array = [allCards.length]; 
      var randomCard:Object; 
      do 
      { 
       randomCard = getRandomCard(); 
       if(shuffledArray.indexOf(randomCard) == -1) 
       { 
        shuffledArray.push(randomCard); 
       } 
      } 
      while(shuffledArray.length < allCards.length) 
       return shuffledArray; 
     } 

     private function getRandomCard():Object 
     { 
      var randomIndex:int = Math.floor(Math.random()* allCards.length); 
      return allCards[randomIndex]; 
     } 
    } 
} 
+0

你的問題必須有數組,洗牌術語? – 2013-03-28 04:41:22

回答

5

錯誤注:

var shuffledArray:Array = [allCards.length]; 

使具有單個元件的陣列,其shuffledArray [0] = allCards.length。 其實你並不需要預先分配它只是說:

var shuffledArray: Array = []; 

這是經典的費雪耶茨版本:

public function shuffleFisherYates():Array { 
var shuffledArray:Array = []; 
var randomCardIndex: int; 
    do { 
     randomCardIndex = Math.floor(Math.random()* allCards.length); 
     shuffledArray.push(allCards[randomCardIndex]); // add to mix 
     allCards.splice(randomCardIndex,1); // remove from deck 
    }while(allCards.length); // Meaning while allCards.length != 0 
    return shuffledArray; 
} 

這裏是Durstenfeld的(代替)版本:

public function shuffleDurstenfeld():Array { 
var swap:Object; 
var countdown:int = allCards.length-1; 
var randomCardIndex: int; 
    for(i = countdown; i > 0; i--){ 
     randomCardIndex = Math.floor(Math.random()* countdown); 
     swap = allCards[countdown]; 
     allCards[countdown] = allCards[randomCardIndex]; 
     allCards[randomCardIndex]= swap; 
    } 
    return allCards; // shuffled in place 
} 
+0

+很好。很好的示範Fisheryates和Durstenfeld – Baba 2013-04-25 10:16:06

0

假設你的洗牌代碼沒問題,我認爲你看到重複卡片的原因是,在你的getRandomCard()方法中,你沒有考慮擁有被繪製。你隨機生成一個索引並將數組返回到數組中......但是該數組仍然在數組中,並且可能會再次隨機生成相同的索引,從而導致返回相同的卡。

+0

我有很長的路要走,去學習面向對象。非常感謝lhsan現在編譯沒有錯誤,但你可以快速瀏覽http://stackoverflow.com/questions/15593171/1120-access-of-undefined-property-shuffledarray。感謝mitim洗牌沒有錯誤,但我仍然在學習,所以你可以分解我的感謝。 – user2203250 2013-03-23 23:14:53

+0

你可以不用重複畫卡片嗎?還是原來的問題還存在? – mitim 2013-03-23 23:34:25

+0

在上面鏈接的其他問題中查看您的代碼,看起來您正在洗牌工作(我自己快速查看了它)。我的原始答案意思是說你有一副牌,你想通過從1到52之間選擇一個隨機數來繪製一張牌。你這樣做,但你只是在你的getRandomCard中檢查那個牌的牌( ) 方法。你實際上並沒有移除那張牌,以任何方式表明它已被抽到甲板上。因此,如果您碰巧再次選擇相同的數字(可以通過Math.random()),您將得到相同的卡片結果。 – mitim 2013-03-23 23:47:57

相關問題