我是 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];
}
}
}