0

我是 actionscript 3.0 的新手,我正在尝试让一副牌洗牌,我已经成功了,但我的问题是我的牌被重复了,所以洗牌后我在 52 张牌的牌组中有同一张牌的重复。我正在尝试创建一个德州扑克游戏。

我发现这个讨论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];
        }
    }
}
4

2 回答 2

5

错误说明:

var shuffledArray:Array = [allCards.length];

使用 shuffledArray[0] = allCards.length 的单个元素创建一个数组。事实上你不需要预先分配它只是说:

var shuffledArray: Array = []; 

这是经典的 Fisher-Yates 版本:

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
}
于 2013-03-23T22:01:47.890 回答
0

假设你的洗牌代码没问题,我认为你看到重复牌的一个原因是在你的 getRandomCard() 方法中,你没有考虑到已经抽出的牌。您随机生成一个索引并在数组中返回卡片......但该卡片仍在数组中,并且可能再次随机生成相同的索引,导致返回相同的卡片。

于 2013-03-23T22:29:30.033 回答