Jump to content
  • Advertisement
Sign in to follow this  

Mapping Poker-Starting Hands to 0..1325

This topic is 1366 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I'm the new guy ;)


I'm trying to develop a poker-tool and I'm having a little tought-problem.

There are 1326 poker starting hands. I want to be able to identify each of them with a number between 0 and 1325. Then, when I have an input of f.e. 768, I want to be able to reconstruct the two holecards, that made up this hand. Holecards are numbers between 0..51 (for a deck of 52cards).


Obviously I want to be able to do it the other way around as well, which I believe is alot easier tho.


I came up with the following idea:

Let { i, j } be a pair of cards. Then I get the hand h with

i*51 + j ; i<j

I then thought I could reconstruct the cards by

h % 51 = j

h-j / 51 = i


But that doesnt seem to be the solution :\

Can anyone help me out here? Just can't figure it out.

Share this post

Link to post
Share on other sites

You need to use 52 instead of 51 (since x % 51 gives a number from 0 to 50, not 0 to 51).


You also want to use (h - j) / 52 since division has higher precedence than subtraction.


You probably don't need all combinations either the only thing you care about is card value and whether they are the same suit or off suit.


EDIT: And what Bacterius said.

Edited by Paradigm Shifter

Share this post

Link to post
Share on other sites
There are 52 cards, so you need i * 52 + j, and the requirement that i < j is unnecessary. So:

> Hand {i, j} maps to index i * 52 + j


> index h maps to hand {h / 52, h % 52}

Where / is integer division.

EDIT: ninja'd, that said with integer division (which rounds down) you don't need to subtract j to get a multiple of 52, that's one of the nice features of integer division. Edited by Bacterius

Share this post

Link to post
Share on other sites

Thanks for the quick answers first of all. The problem ist just, that if my card "i" is an Ace of spades, it maps to 51. So i*52 alone is > 1326. But I'd need all the numbers to be within the range of 0..1325, it order to easily loop over my arrays.

Is that possible somehow?


Oh and I DO need all combinations unfortunately. It's kidna complicated ;)

Edited by .chicken

Share this post

Link to post
Share on other sites

The two two solutions above assume all possible combinations of all possible cards without regards for duplication or order. That is, you have a unique identifier allocated for {i,i} which is not a possible hand, and you get two different identifiers for {i,j} and {j,i} which in fact are the same hands.

Assume a deck of 6 cards for each for easy visualization; the concept extends to any size. If you just take the above solutions, you get the following matrix of enumerated hards (x- and y-axes fo the table are the cards i and j, respectively):

     0     1     2     3     4     5
     5     6     7     8     9    10
    10    11    12    13    14    15
    15    16    17    18    19    20
    20    21    22    23    24    25
    25    26    27    28    29    30

As you can see, the diagonals have a unique value for impossible hands, and the upper-right and lower-left triangular parts also have unique values for the same hands.

What you want is the following matrix:

     -     0     2     5     9    14
     -     -     1     4     8    13
     -     -     -     3     7    12
     -     -     -     -     6    11
     -     -     -     -     -    10
     -     -     -     -     -     -

where - indicates a don't care-value because those hands are either impossible or already represented in the upper-right triangle.


Given two card indices {i,j}, the formula 0.5*i*i + 0.5*i - j - 1 gives you those values, assuming that i and j are integers and that j<i. The formula is independent on the number of cards. It should be possible to solve this for {i,j} given a hand index. It is a single equation with two unknowns, and the quadratic equation generally have two solutions as well, but I believe there is only one solution given the constraints that i and j are integers, and that 0<=j<i<52.


But given the very limited number of hands, a table with pre-coded hands may be feasible. In that case, just search the table for the combination of two cards and likewise look up the table index and read which two cards it represents.

Share this post

Link to post
Share on other sites
I believe one possible mapping formula would be (under the condition that \(i < j\)):
\[\{i, j\} \mapsto \frac{1}{2} i(103 - i) + (j - i) - 1\]
So, for instance:
\[\{0, 1\} \mapsto 0\]
\[\{1, 2\} \mapsto 51\]
\[\{41, 50\} \mapsto 1279\]
\[\{50, 51\} \mapsto 1325\]
With the decoding of an index \(m\) achieved by first finding \(i\) which is the largest such that:
\[\frac{1}{2} i(103 - i) \leq m\]
And then solving for \(j\) accordingly. Something like this (in Python, double slash is integer division):
def hand_to_index(i, j):
    return i * (103 - i) // 2 + (j - i) - 1

def index_to_hand(m):
    for test_i in range(52):
        if test_i * (103 - test_i) // 2 > m:
            i = test_i - 1
            j = m - i * (103 - i) // 2 + i + 1
            return (i, j)
There are better ways of finding \(i\), for instance just solving the quadratic equation and rounding the solution down to the nearest integer instead of iterating to find the right \(i\) (or using binary search if you really want to over-engineer) but in any case I would personally agree with Brother Bob's solution of using a precomputed table, it seems a lot less work and much less error-prone given that the search space is so small anyway.

Share this post

Link to post
Share on other sites

Ok, thank you so far. I'll explain what I need this for in a little more detail.


I'm trying to find approximations of game equilibria, when two players play against each other a game of poker with restricted rules.

Therefore I'll compute the win% of every possible starting pokerhand vs every other possible starting pokerhand on a given board and store that information in an array.


What I did previously was


then, if hand1 was {i,j} and hand2 was {a,b} i could get the win% of hand1 vs hand2 with array[j][a]


Obviously I have alot of unused memory and I kept getting stack overflow errors. Additionally I'll save the array data on the disk and load it the next time I need it, so I dont have to compute all the win%s again.


So I'm trying to find a better solution for my previous approach now, so that I can just have an array[1326][1326].

The first two priorities are, that it's easy to use and that it's fast. If it slows down everytime I try to convert the starting hand to 2 individual cards and vice versa, it won't be very useful.

So you still think a precomputed table would be the way to go?

I'd most easily do that by setting up the table in for-loops at the beginning of my program, right? I didn't even think about that...

Edit: so in that loop

i=0..51 {
j=0..51 {
  //here i'd just use the hand_to_index(i,j) function bacterius suggested, right?

Thank you so far.

Edited by .chicken

Share this post

Link to post
Share on other sites
I didn't follow the whole thread, but here's some simple C++ code to do what you want:
#include <iostream>
#include <cmath>

struct Pair {
  int x, y; // x > y                                                                                                  

  Pair(int x, int y) : x(x), y(y) {

int pair_to_int(Pair pair) {
  return pair.x * (pair.x - 1) / 2 + pair.y;

Pair int_to_pair(int i) {
  int x = int(0.5 + std::sqrt(.25+2.0*i));
  int y = i - x * (x - 1) / 2;
  return Pair(x, y);

int main() {
  Pair p = int_to_pair(768);
  std::cout << p.x << ' ' << p.y << '\n';
  std::cout << pair_to_int(p) << '\n';

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!