我有一个算法来计算玩家的手在德州扑克中是否持有顺子。它工作正常,但我想知道是否有一种更简单的方法可以做到这一点,不涉及数组/字符串转换等。
这是我所拥有的简化版本。假设玩家拿到一手牌,它是一个 52 元素的卡片值数组:
var rawHand = [1,0,0,0,0,0,0,0,0,0,0,0,0, //clubs
0,0,0,0,0,0,0,0,0,0,0,0,0, //diamonds
0,1,1,0,1,0,0,0,0,0,0,0,0, //hearts
0,0,0,1,0,0,0,0,1,0,0,0,0];//spades
1 代表该值槽中的一张卡。上面的手有 2 个梅花,没有方块,有 3 个红心、4 个红心和 6 个红心,一个 5-黑桃和一个 10-黑桃。现在我看着它找到一条直线。
var suits = []; //array to hold representations of each suit
for (var i=0; i<4; i++) {
var index = i*13;
// commenting this line as I removed the rest of its use to simplifyy example
//var hasAce = (rawHand[i+13]);
//get a "suited" slice of the rawHand, convert it to a string representation
//of a binary number, then parse the result as an integer and assign it to
//an element of the "suits" array
suits[i] = parseInt(rawHand.slice(index,index+13).join(""),2);
}
// OR the suits
var result = suits[0] | suits[1] | suits[2] | suits[3];
// Store the result in a string for later iteration to determine
// whether straight exists and return the top value of that straight
// if it exists; we will need to determine if there is an ace in the hand
// for purposes of reporting a "low ace" straight (i.e., a "wheel"),
// but that is left out in this example
var resultString = result.toString(2);
//Show the result for the purposes of this example
alert("Result: " + resultString);
这里的诀窍是对各种花色进行或运算,因此只有一个 2 对 A 表示。我认为必须有一种更简单的方法来做到这一点是错误的吗?