Question

J'ai un algorithme de calcul si la main d'un joueur est titulaire d'un droit au Texas Hold'em. Il fonctionne très bien, mais je me demande s'il y a un moyen plus simple de le faire qui n'implique pas des conversions tableau / string, etc.

Voici une version simplifiée de ce que j'ai. Dites le joueur reçoit une main qui est un tableau de 52 éléments de valeurs de cartes:

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 représente une carte dans cette fente de valeur. La main ci-dessus présente un 2-clubs, aucun diamant, à 3 coeurs, 4-coeurs, et le 6-coeurs, un 5-bêches et à 10 pique. Maintenant, je regarde pour trouver une ligne droite.

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

L'astuce est ici pour OU les différents costumes donc il n'y a qu'un seul 2 à Ace représentation. Suis-je à ce faire tort de penser doit y avoir un moyen plus simple?

Était-ce utile?

La solution

La quasi-totalité du travail que votre code est de conversion de type. Si vous venez d'avoir la main stocké au format binaire pour commencer (besoins> 32 type de bits), vous pouvez faire quelque chose comme:

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;

L'équivalent en utilisant une boucle d'une ligne serait:

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

Ceci est beaucoup plus courte et plus facile à comprendre.

Conversion vers et à partir d'une représentation sage bit prend plus de code et de temps CPU que vous enregistrez en utilisant le bit à bit opérateur OR.

Autres conseils

Eh bien, un droit doit inclure un 5 ou un 10, vous pouvez commencer par jeter la main si elle ne dispose pas d'un ou l'autre:

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
}

Vous pouvez utiliser une valeur entière comme un champ de bits pour les valeurs de cartes, as obtient deux points bas et haut. Ensuite, vous comparez avec la fin de dix contre bitwise lignes droites possibles.

Ou utiliser une boucle for et vérifiez cinq numéros consécutifs -. Efficacement est tout de même

Cette question m'a intéressé. J'ai fini par aller beaucoup trop loin. Et a écrit une page Web pour calculer une main. Son sans doute pas le plus efficace, mais il ne fonctionne pas. Je l'ai fait avec juste JavaScript (jQuery No). Voici une démo http://jsbin.com/izuto4/2/

Voici le 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>&nbsp;A</td>
            <td>&nbsp;2</td>
            <td>&nbsp;3</td>
            <td>&nbsp;4</td>
            <td>&nbsp;5</td>
            <td>&nbsp;6</td>
            <td>&nbsp;7</td>
            <td>&nbsp;8</td>
            <td>&nbsp;9</td>
            <td>10</td>
            <td>&nbsp;J</td>
            <td>&nbsp;Q</td>
            <td>&nbsp;K</td>
            <td>&nbsp;</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>

Non, c'est à peu près aussi simple que cela. Je regardais dans l'évaluation de la main de poker il y a un certain temps, et je pense que la façon la plus rapide utilise une approche comme la vôtre. Voir le premier résultat ce site . Il utilise les opérations de bits pour calculer les mains.

EDIT: En premier résultat, je veux dire "PokerSource Poker-Eval Evaluator"

.

Pourquoi ne pas trier les cartes par rang, puis vérifier que chaque rang est l'un de plus que le précédent. "Rangs" est un tableau de longueur 5 d'ACE = 1, 2 = 2, ..., J = 11, Q = 12, K = 13. Je pense que cela est plus simple que les autres méthodes présentées ici.

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

Vous pouvez utiliser 7 cartes de SpecialK et les évaluateurs de 5 cartes posté et demander de classer les mains. Si le rang qui est retourné se situe entre 5854 et 5863 (inclusivement) ou entre 7453 et 7462 (tous deux inclus) puis votre main, que ce soit de 5 ou 7 cartes, respectivement est ou contient un droit.

Notez l'as de pique est désigné par 0, l'as de coeurs par 1, en passant par les deux clubs qui est représenté par 51. L'algorithme « ajouter » vos cartes et regarder le rang dans une petite table , prenant 9 Mo de RAM. Il fera aussi beaucoup plus, mais bon ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top