View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Mapping Poker-Starting Hands to 0..1325

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1.chicken  Members

Posted 26 August 2014 - 05:03 AM

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.

Posted 26 August 2014 - 05:45 AM

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, 26 August 2014 - 05:48 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3Bacterius  Members

Posted 26 August 2014 - 05:46 AM

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

And:

> 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, 26 August 2014 - 05:48 AM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #4.chicken  Members

Posted 26 August 2014 - 07:03 AM

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, 26 August 2014 - 07:05 AM.

### #5Brother Bob  Moderators

Posted 26 August 2014 - 07:34 AM

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.

### #6Bacterius  Members

Posted 26 August 2014 - 08:00 AM

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #7.chicken  Members

Posted 26 August 2014 - 08:37 AM

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

array[52][52][52][52]

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

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, 26 August 2014 - 08:42 AM.

### #8Glass_Knife  Moderators

Posted 26 August 2014 - 09:25 AM

This may give you some inspiration:

http://www.suffecool.net/poker/evaluator.html

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #9Álvaro  Members

Posted 26 August 2014 - 06:32 PM

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';
}



### #10.chicken  Members

Posted 27 August 2014 - 11:05 AM

Ok, thanks everyone, I found a quick enough solution now

Thanks so much!

### #11Álvaro  Members

Posted 27 August 2014 - 11:13 AM

Ok, thanks everyone, I found a quick enough solution now

So what is it?

### #12.chicken  Members

Posted 27 August 2014 - 08:26 PM

Well,I guess this is what you meant by precomputing the arrays?

    static std::vector<std::vector<startingHand>> fillCardsToHand() {
std::vector<std::vector<startingHand>> result;
for (int i = 0; i < kNumCards; i++) {
result.push_back(std::vector<startingHand>());
for (int j = 0; j < kNumCards; j++) {
result[i].push_back(i < j ? (i*(103 - i) / 2 + (j - i) - 1) : (j*(103 - j) / 2 + (i - j) - 1));
}
}
return result;
}
static std::vector<std::pair<card, card>> fillHandsToCard() {
std::vector<std::pair<card, card>> result;
for (int i = 0; i < kNumHands; i++) {
for (int k = 0; k < kNumCards; k++) {
if ((k*(103 - k) / 2) > i) {
result.push_back(std::make_pair(k - 1, i - ((k - 1) * (103 - (k - 1)) / 2 - (k - 1) - 1)));
break;
}
}
}
return result;
}
static std::vector<std::vector<startingHand>> cardsToHandArray = fillCardsToHand();
static std::vector<std::pair<card,card>> handsToCardArray = fillHandsToCard();

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.