actionscript 3.0如何将算法合并到我的代码中?

时间:2013-03-23 21:33:26

标签: actionscript-3

我是动作脚本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];
        }
    }
}

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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