• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Mapping Poker-Starting Hands to 0..1325

11 posts in this topic

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
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[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

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

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++) {
            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)));
        return result;
    static std::vector<std::vector<startingHand>> cardsToHandArray = fillCardsToHand();
    static std::vector<std::pair<card,card>> handsToCardArray = fillHandsToCard();

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0