这里有第一个问题...... 我正在编写一个简单的3张牌扑克手评估器,并且在从一组值中查找/提取多个“直线”(连续值序列)时遇到问题。
我需要提取并尽可能直接返回数组。这是一个例子:
(假设数组首先按数字递增排序)
myArray = [1h,2h,3c,3h,4c]
可能的三值序列是: [1H,2H,3C] [1H,2H,3H] [2H,3C,4C] [2H,3H,4C]
这是我的原始代码,用于查找序列3,其中数组包含带有.value和.suit的卡对象。为简单起见,这个问题我只是把“2h”等放在这里:
private var _pokerHand = [1h,2h,3c,3h,4c];
private function getAllStraights(): Array
{
var foundStraights:Array = new Array();
for (var i: int = 0; i < (_handLength - 2); i++)
{
if ((_pokerHand[i].value - _pokerHand[i + 1].value) == 1 && (_pokerHand[i + 1].value - _pokerHand[i + 2].value) == 1)
{
trace("found a straight!");
foundStraights.push(new Array(_pokerHand[i], _pokerHand[i + 1], _pokerHand[i + 2]));
}
}
return foundStraights;
}
但当有重复值时(例如上面的3),它当然会失败。我不能丢弃重复项,因为它们可能是不同的诉讼。我需要一切可能,如上例所示。这允许我通过“Flush”功能运行直道以找到“同花顺”。
我缺少什么数组迭代技术?
答案 0 :(得分:1)
这是一个有趣的问题。考虑到扑克游戏(和Flash)的流行,我确信之前已经解决了很多次,但我在网上找不到一个例子。以下是我将如何处理它:
这似乎符合您的要求(Card
对象.value
为int
}:
private function getAllStraights(cards:Vector.<Card>, straightLength:uint = 3):Vector.<Vector.<Card>> {
var foundStraights:Vector.<Vector.<Card>> = new <Vector.<Card>>[];
var possibleStraights:Vector.<Vector.<Card>> = new <Vector.<Card>>[];
for each (var startingCard:Card in cards) {
possibleStraights.push(new <Card>[startingCard]);
}
while (possibleStraights.length) {
var possibleStraight:Vector.<Card> = possibleStraights.shift();
var lastCard:Card = possibleStraight[possibleStraight.length - 1];
var possibleNextCards:Vector.<Card> = new <Card>[];
for (var i:int = cards.indexOf(lastCard) + 1; i < cards.length; i++) {
var nextCard:Card = cards[i];
if (nextCard.value == lastCard.value)
continue;
if (nextCard.value == lastCard.value + 1)
possibleNextCards.push(nextCard);
else
break;
}
for each (var possibleNextCard:Card in possibleNextCards) {
var possibleNextStraight:Vector.<Card> = possibleStraight.slice().concat(new <Card>[possibleNextCard]);
if (possibleNextStraight.length == straightLength)
foundStraights.push(possibleNextStraight);
else
possibleStraights.push(possibleNextStraight);
}
}
return foundStraights;
}
鉴于[1♥,2♥,3♣,3♥,4♣]
,您得到:[1♥,2♥,3♣], [1♥,2♥,3♥], [2♥,3♣,4♣], [2♥,3♥,4♣]
当你有很多重复项时,它会变得非常有趣,比如[1♥,1♣,1♦,1♠,2♥,2♣,3♦,3♠,4♣,4♦,4♥]
。这给你:
[1♥,2♥,3♦], [1♥,2♥,3♠], [1♥,2♣,3♦], [1♥,2♣,3♠], [1♣,2♥,3♦], [1♣,2♥,3♠], [1♣,2♣,3♦], [1♣,2♣,3♠], [1♦,2♥,3♦], [1♦,2♥,3♠], [1♦,2♣,3♦], [1♦,2♣,3♠], [1♠,2♥,3♦], [1♠,2♥,3♠], [1♠,2♣,3♦], [1♠,2♣,3♠], [2♥,3♦,4♣], [2♥,3♦,4♦], [2♥,3♦,4♥], [2♥,3♠,4♣], [2♥,3♠,4♦], [2♥,3♠,4♥], [2♣,3♦,4♣], [2♣,3♦,4♦], [2♣,3♦,4♥], [2♣,3♠,4♣], [2♣,3♠,4♦], [2♣,3♠,4♥]
我没有仔细检查过,但它看起来一目了然。