有没有比这更简单的方法来计算扑克中的顺子?

发布于 2024-09-29 02:48:05 字数 1551 浏览 0 评论 0原文

我有一个算法来计算德州扑克中玩家的牌是否为顺子。它工作得很好,但我想知道是否有一种更简单的方法来做到这一点,不涉及数组/字符串转换等。

这是我所拥有的简化版本。假设玩家拿到的手牌是由 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 的表示形式。我认为必须有一种更简单的方法来做到这一点是错误的吗?

I have an algorithm for calculating whether a player's hand holds a straight in Texas Hold'em. It works fine, but I wonder if there is a simpler way to do it that does not involve array/string conversions, etc.

Here's a simplified version of what I have. Say the player is dealt a hand that is a 52-element array of card values:

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 represents a card in that value slot. The above hand has a 2-clubs, no diamonds, a 3-hearts, 4-hearts, and 6-hearts, a 5-spades and a 10-spades. Now I look at it to find a straight.

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);

The trick here is to OR the various suits so there is just one 2-to-Ace representation. Am I wrong in thinking there must be a simpler way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尽揽少女心 2024-10-06 02:48:06

好吧,顺子必须包含 5 或 10,因此如果这手牌没有其中之一或其他,您可以先将其扔掉:

if (rawHand[3] || rawHand[16] || rawHand[29] || rawHand[42] ||
    rawHand[8] || rawHand[21] || rawHand[34] || rawHand[47]) {
  // do some more checks
} else {
  // not a straight
}

Well, a straight must include a 5 or a 10, so you can start by throwing out the hand if it doesn't have one or other:

if (rawHand[3] || rawHand[16] || rawHand[29] || rawHand[42] ||
    rawHand[8] || rawHand[21] || rawHand[34] || rawHand[47]) {
  // do some more checks
} else {
  // not a straight
}
a√萤火虫的光℡ 2024-10-06 02:48:06

您可以使用整数值作为牌值的位字段,A 获得低位和高位两个位置。然后,您将按位结束与十个可能的直线进行比较。

或者使用 for 循环并检查五个连续的数字 - 实际上它们都是相同的。

You can use an integer value as a bitfield for the card values, ace gets two spots low and high. Then you compare with bitwise end against the ten possible straights.

Or use a for-loop and check for five consecutive numbers - effectively it's all the same.

栖迟 2024-10-06 02:48:06

这个问题引起了我的兴趣。我最终太过分了。并写了一个网页来计算任意手牌。它可能不是最有效的,但它确实有效。我仅使用 JavaScript(无 jQuery)完成此操作。这是一个 演示 http://jsbin.com/izuto4/2/

下面是代码:

<html>
<head>
<script>
   // var myrawHand = [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,0,0,0,0,0];//spades

    function getCardsInHand(rawHand) {
        var cardsInHand = new Array();
        var counter = 0;
        for (var i = 0; i < rawHand.length; i ++) {
            if (rawHand[i]) {
                cardsInHand[counter] = i;
                counter ++;
            }
        }
        return cardsInHand;
    }

    function cardsfiltered(rawHand) {
        var cards = getCardsInHand(rawHand)

        var cardsfiltered = new Array();
        for (var j = 0; j < cards.length; j ++){
            cardsfiltered[j] = cards[j] - (parseInt(cards[j] / 13) * 13);
        }
        cardsfiltered.sort();
        return {cards : cards, cardsfiltered : cardsfiltered};
    }

    function whatIsMyHand(rawHand) {
        var cardObject = cardsfiltered(rawHand);
        if (((cardObject.cards[0] == 0 && cardObject.cards[1] == 9)
                || (cardObject.cards[0] == 13 && cardObject.cards[1] == 22)
                || (cardObject.cards[0] == 26 && cardObject.cards[1] == 35)
                || (cardObject.cards[0] == 39 && cardObject.cards[1] == 48))
                && cardObject.cards[4] == cardObject.cards[3] + 1 &&
                cardObject.cards[3] == cardObject.cards[2] + 1 &&
                cardObject.cards[2] == cardObject.cards[1] + 1) {
            return "Royal Flush";
        }
        else if (cardObject.cards[4] == cardObject.cards[3] + 1 &&
                cardObject.cards[3] == cardObject.cards[2] + 1 &&
                cardObject.cards[2] == cardObject.cards[1] + 1 &&
                cardObject.cards[1] == cardObject.cards[0] + 1) {
            return "Straight Flush";
        }
        else if ((cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3])
                && (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Four of a Kind";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])
                || (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Full House";
        }
        else if (parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[1] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[2] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[3] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[4] / 13)) {
            return "Flush";
        }
        else if ((cardObject.cardsfiltered[4] == cardObject.cardsfiltered[3] + 1
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[2] + 1
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[1] + 1
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[0] + 1)
                || (cardObject.cardsfiltered[0] == 0
                && cardObject.cardsfiltered[1] == 10
                && cardObject.cardsfiltered[2] == 11
                && cardObject.cardsfiltered[3] == 12
                && cardObject.cardsfiltered[4] == 9)) {
            return "Straight";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2])
                || (cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3])
                || (cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Three of a Kind";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && (cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4]))
                || (cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Two Pair"
        }
        else if (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                || cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                || cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4]) {
            return "Pair";
        }
        else {
            return "High Card";
        }
    }
    var CardCheckCount = 0;
    function MaxCardCheck(element) {
        if (element.checked) {
            if (CardCheckCount < 5) {
                CardCheckCount++;
                return true;
            }
        }
        else {
            CardCheckCount--;
            return true;
        }
        element.checked = !element.checked;
        alert("You can only pick 5 cards.");
        return false;
    }

    function calculateHand() {
        var checkboxes = document.getElementsByTagName("input");
        var myrawHand = new Array();
        for (var i = 0, element; element = checkboxes[i]; i++) {
          myrawHand[parseInt(element.name)] = element.checked ? element.value : 0;
        }
        alert(whatIsMyHand(myrawHand));
    }
</script>
</head>
<body>
<table>
    <thead>
        <tr>
            <td> A</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td> 5</td>
            <td> 6</td>
            <td> 7</td>
            <td> 8</td>
            <td> 9</td>
            <td>10</td>
            <td> J</td>
            <td> Q</td>
            <td> K</td>
            <td> </td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input name="0" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="1" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="2" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="3" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="4" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="5" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="6" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="7" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="8" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="9" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="10" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="11" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="12" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Clubs</td>
        </tr>
        <tr>
            <td><input name="13" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="14" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="15" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="16" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="17" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="18" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="19" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="20" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="21" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="22" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="23" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="24" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="25" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Diamonds</td>
        </tr>
        <tr>
            <td><input name="26" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="27" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="28" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="29" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="30" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="31" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="32" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="33" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="34" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="35" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="36" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="37" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="38" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Hearts</td>
        </tr>
        <tr>
            <td><input name="39" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="40" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="41" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="42" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="43" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="44" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="45" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="46" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="47" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="48" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="49" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="50" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="51" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Spades</td>
        </tr>
    </tbody>
</table>
<button onclick="calculateHand()">Calculate Hand</button>
</body>
</html>

This question got me interested. I ended up going way overboard. And wrote a web page to calculate any hand. Its probably not the most efficient but it does work. I did this with just JavaScript (No jQuery). Here is a demo http://jsbin.com/izuto4/2/

Below is the code:

<html>
<head>
<script>
   // var myrawHand = [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,0,0,0,0,0];//spades

    function getCardsInHand(rawHand) {
        var cardsInHand = new Array();
        var counter = 0;
        for (var i = 0; i < rawHand.length; i ++) {
            if (rawHand[i]) {
                cardsInHand[counter] = i;
                counter ++;
            }
        }
        return cardsInHand;
    }

    function cardsfiltered(rawHand) {
        var cards = getCardsInHand(rawHand)

        var cardsfiltered = new Array();
        for (var j = 0; j < cards.length; j ++){
            cardsfiltered[j] = cards[j] - (parseInt(cards[j] / 13) * 13);
        }
        cardsfiltered.sort();
        return {cards : cards, cardsfiltered : cardsfiltered};
    }

    function whatIsMyHand(rawHand) {
        var cardObject = cardsfiltered(rawHand);
        if (((cardObject.cards[0] == 0 && cardObject.cards[1] == 9)
                || (cardObject.cards[0] == 13 && cardObject.cards[1] == 22)
                || (cardObject.cards[0] == 26 && cardObject.cards[1] == 35)
                || (cardObject.cards[0] == 39 && cardObject.cards[1] == 48))
                && cardObject.cards[4] == cardObject.cards[3] + 1 &&
                cardObject.cards[3] == cardObject.cards[2] + 1 &&
                cardObject.cards[2] == cardObject.cards[1] + 1) {
            return "Royal Flush";
        }
        else if (cardObject.cards[4] == cardObject.cards[3] + 1 &&
                cardObject.cards[3] == cardObject.cards[2] + 1 &&
                cardObject.cards[2] == cardObject.cards[1] + 1 &&
                cardObject.cards[1] == cardObject.cards[0] + 1) {
            return "Straight Flush";
        }
        else if ((cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3])
                && (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Four of a Kind";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])
                || (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Full House";
        }
        else if (parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[1] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[2] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[3] / 13)
                && parseInt(cardObject.cards[0] / 13) == parseInt(cardObject.cards[4] / 13)) {
            return "Flush";
        }
        else if ((cardObject.cardsfiltered[4] == cardObject.cardsfiltered[3] + 1
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[2] + 1
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[1] + 1
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[0] + 1)
                || (cardObject.cardsfiltered[0] == 0
                && cardObject.cardsfiltered[1] == 10
                && cardObject.cardsfiltered[2] == 11
                && cardObject.cardsfiltered[3] == 12
                && cardObject.cardsfiltered[4] == 9)) {
            return "Straight";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2])
                || (cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3])
                || (cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Three of a Kind";
        }
        else if ((cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                && (cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4]))
                || (cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                && cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4])) {
            return "Two Pair"
        }
        else if (cardObject.cardsfiltered[0] == cardObject.cardsfiltered[1]
                || cardObject.cardsfiltered[1] == cardObject.cardsfiltered[2]
                || cardObject.cardsfiltered[2] == cardObject.cardsfiltered[3]
                || cardObject.cardsfiltered[3] == cardObject.cardsfiltered[4]) {
            return "Pair";
        }
        else {
            return "High Card";
        }
    }
    var CardCheckCount = 0;
    function MaxCardCheck(element) {
        if (element.checked) {
            if (CardCheckCount < 5) {
                CardCheckCount++;
                return true;
            }
        }
        else {
            CardCheckCount--;
            return true;
        }
        element.checked = !element.checked;
        alert("You can only pick 5 cards.");
        return false;
    }

    function calculateHand() {
        var checkboxes = document.getElementsByTagName("input");
        var myrawHand = new Array();
        for (var i = 0, element; element = checkboxes[i]; i++) {
          myrawHand[parseInt(element.name)] = element.checked ? element.value : 0;
        }
        alert(whatIsMyHand(myrawHand));
    }
</script>
</head>
<body>
<table>
    <thead>
        <tr>
            <td> A</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td> 5</td>
            <td> 6</td>
            <td> 7</td>
            <td> 8</td>
            <td> 9</td>
            <td>10</td>
            <td> J</td>
            <td> Q</td>
            <td> K</td>
            <td> </td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input name="0" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="1" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="2" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="3" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="4" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="5" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="6" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="7" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="8" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="9" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="10" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="11" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="12" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Clubs</td>
        </tr>
        <tr>
            <td><input name="13" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="14" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="15" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="16" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="17" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="18" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="19" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="20" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="21" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="22" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="23" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="24" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="25" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Diamonds</td>
        </tr>
        <tr>
            <td><input name="26" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="27" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="28" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="29" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="30" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="31" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="32" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="33" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="34" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="35" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="36" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="37" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="38" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Hearts</td>
        </tr>
        <tr>
            <td><input name="39" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="40" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="41" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="42" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="43" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="44" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="45" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="46" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="47" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="48" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="49" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="50" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td><input name="51" type="checkbox" value="1" onclick="MaxCardCheck(this);"/></td>
            <td>Spades</td>
        </tr>
    </tbody>
</table>
<button onclick="calculateHand()">Calculate Hand</button>
</body>
</html>
久伴你 2024-10-06 02:48:06

不,这就是最简单的事情。我不久前研究过扑克手牌评估,我认为最快的方法是使用像你这样的方法。请参阅此网站中的第一个结果。它使用按位运算来计算手。

编辑:第一个结果,我的意思是“Pokersource Poker-Eval Evaluator”。

No, that's about as simple as it gets. I looked into poker hand evaluation a while ago, and I think the fastest way uses an approach like yours. See the first result in this site. It uses bitwise operations to compute hands.

EDIT: By first result, I mean "Pokersource Poker-Eval Evaluator".

旧城空念 2024-10-06 02:48:06

为什么不按等级对卡片进行排序,然后检查每个等级是否比前一个等级多一级。 “ranks”是一个长度为 5 的数组,其中 ACE = 1、2 = 2、... J = 11、Q = 12、K = 13。
我认为这比这里介绍的其他方法更简单。

function isStraight( ranks) {
  ranks.sort();
  return (ranks[0] + 1 == ranks[1] || (ranks[0] == 1 && ranks[4] == 13)) &&
         (ranks[1] + 1 == ranks[2]) &&
         (ranks[2] + 1 == ranks[3]) &&
         (ranks[3] + 1 == ranks[4]);
}

Why not sort the cards by rank, and then check that each rank is one more than the previous. "ranks" is an array of length 5 with ACE = 1, 2 = 2, ... J = 11, Q = 12, K = 13.
I think this is more simple than the other methods presented here.

function isStraight( ranks) {
  ranks.sort();
  return (ranks[0] + 1 == ranks[1] || (ranks[0] == 1 && ranks[4] == 13)) &&
         (ranks[1] + 1 == ranks[2]) &&
         (ranks[2] + 1 == ranks[3]) &&
         (ranks[3] + 1 == ranks[4]);
}
月亮邮递员 2024-10-06 02:48:06

您可以使用此处发布的 SpecialK 的 7 张牌和 5 张牌评估器,并要求其对牌进行排名。如果返回的等级介于 5854 和 5863(均包含)之间或介于 7453 和 7462(均包含)之间,则您的手牌(无论是 5 张牌还是 7 张牌)分别是顺子或包含顺子。

请注意,黑桃 A 表示为 0,红桃 A 表示为 1,一直到梅花二表示为 51。该算法将“添加”您的牌并在一个小表中查找排名,占用9MB 内存。它还会做更多的事情,但是嘿......

You can use SpecialK's 7-card and 5-card evaluators posted here and ask it to rank hands. If the rank that is returned is between 5854 and 5863 (both inclusive) or between 7453 and 7462 (both inclusive) then your hand, be it of 5 or 7 cards, respectively is or contains a straight.

Note the Ace of Spades is denoted by 0, the Ace of Hearts by 1, through to the Two of Clubs which is represented by 51. The algorithm will "add" up your cards and look up the rank in a small table, taking up 9MB of RAM. It will also do a lot more, but hey...

缱绻入梦 2024-10-06 02:48:05

代码所做的几乎所有工作都是类型转换。如果您一开始就以位格式存储手(需要 > 32 位类型),您可以执行以下操作:

var mask = 2^13 - 1; // this will zero out all but the low 13 bits
var suits = (rawHand | rawHand>>13 | rawHand>>26 | rawHand>>39) & mask;

使用单行循环的等效操作将是:

var suits = [];
for(var i=0; i < 13; i++) {
   suits[i] = rawHand[i] || rawHand[i+13] || rawHand[i+26] || rawHand[i+39];
}

这更短且更易于理解。

与按位表示法相互转换需要的代码和 CPU 时间比使用按位 OR 运算符节省的代码和 CPU 时间要多。

Almost all of the work your code does is type conversion. If you just had the hand stored in bit format to begin with(needs > 32 bit type), you could do something like:

var mask = 2^13 - 1; // this will zero out all but the low 13 bits
var suits = (rawHand | rawHand>>13 | rawHand>>26 | rawHand>>39) & mask;

The equivalent using a one line loop would be:

var suits = [];
for(var i=0; i < 13; i++) {
   suits[i] = rawHand[i] || rawHand[i+13] || rawHand[i+26] || rawHand[i+39];
}

This is much shorter and easier to understand.

Converting to and from a bit-wise representation takes more code and CPU time than you save by using the bit-wise OR operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文