我有一個算法來計算玩家的手是否在德州撲克中持有直線。它工作正常,但我不知道是否有做一個簡單的方法,不涉及陣列/串轉換等有沒有比這更簡單的方法來計算直撲撲克?
這裏是什麼,我有一個簡化版本。假設玩家處理的是一個卡片值爲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
A 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);
這裏的訣竅是對各種套裝進行OR處理,所以只有一個2對ACE表示法。我錯在認爲必須有一個更簡單的方法來做到這一點?
看起來對我來說非常簡單,你認爲它可以更簡單嗎?爲了檢測輪子,我只需在陣列前面插入每個套裝陣列的最後一個元素的副本,然後將它們放在一起。然後你可以搜索'11111'的字符串結果。 – 2010-10-23 16:21:12
它可以更快地使用反向while/for循環for(var i = 3; i - ;){...}' – vol7ron 2010-10-23 16:36:46
@Larry Lustig:這是我認爲可能更簡單的parseInt行。你知道,在「也許如果我在這更好,我不會轉換爲數組=>字符串=> int,然後回到一個字符串」... – Robusto 2010-10-24 00:08:52