0

I am attempting to decipher this code: https://github.com/tangentforks/TwoPlusTwoHandEvaluator/blob/master/generate_table.cpp

The code is used to generate a massive lookup table that is then used to look up hand ranks of 7 card hands. I am having difficulty deciphering exactly why the main method is written the way it is. From what I can make of it, the first for loop bruteforces every single combination of cards, substituting the appropriate suit abstractions when necessary, but I am not sure why that is repeated in the next section, or why it is scaled by a factor of 53 and then added 53 to. Could anyone shed any light on this?

This is the code in question::

  printf("\nGetting Card IDs!\n");

  // Jmd: Okay, this loop is going to fill up the IDs[] array which has
  // 612,967 slots. as this loops through and find new combinations it
  // adds them to the end. I need this list to be stable when I set the
  // handranks (next set)  (I do the insertion sort on new IDs these)
  // so I had to get the IDs first and then set the handranks
  for (IDnum = 0; IDs[IDnum] || IDnum == 0; IDnum++) {
    // start at 1 so I have a zero catching entry (just in case)
    for (card = 1; card < 53; card++) {
      // the ids above contain cards upto the current card.  Now add a new card
      ID = MakeID(IDs[IDnum], card);   // get the new ID for it
      // and save it in the list if I am not on the 7th card
      if (numcards < 7) holdid = SaveID(ID);
    }
    printf("\rID - %d", IDnum);   // show progress -- this counts up to 612976
  }

// main()
  printf("\nSetting HandRanks!\n");

  // this is as above, but will not add anything to the ID list, so it is stable
  for (IDnum = 0; IDs[IDnum] || IDnum == 0; IDnum++) {
    // start at 1 so I have a zero catching entry (just in case)
    for (card = 1; card < 53; card++) {
      ID = MakeID(IDs[IDnum], card);

      if (numcards < 7) {
    // when in the index mode (< 7 cards) get the id to save
    IDslot = SaveID(ID) * 53 + 53;
      } else {
    // if I am at the 7th card, get the equivalence class ("hand rank") to save
    IDslot = DoEval(ID);
      }

      maxHR = IDnum * 53 + card + 53;   // find where to put it
      HR[maxHR] = IDslot; // and save the pointer to the next card or the handrank
    }

    if (numcards == 6 || numcards == 7) {
      // an extra, If you want to know what the handrank when there is 5 or 6 cards
      // you can just do HR[u3] or HR[u4] from below code for Handrank of the 5 or
      // 6 card hand
      // this puts the above handrank into the array
      HR[IDnum * 53 + 53] = DoEval(IDs[IDnum]);
    }

    printf("\rID - %d", IDnum); // show the progress -- counts to 612976 again
} 
cubesnyc
  • 1,385
  • 2
  • 15
  • 32
  • The link is 456 lines of code and has dozens of loops. You could be a little more precise about which loop you are asking about. – brian beuning Aug 24 '17 at 03:15
  • I referenced it in the question, the two main for loops in the main method. – cubesnyc Aug 24 '17 at 03:38
  • When converting a base 10 number in string form to a binary form, the code multiples by 10 for each digit. Since there are 52 cards and I assume 1 extra value to mean last card, this code multiplies by 53 to pack multiple cards into one integer. Since log(2^64) / log(53) = 11.22, they can pack 11 cards into one 64-bit int without overflow. – brian beuning Aug 24 '17 at 11:40

1 Answers1

0

Doing the same currently. It is shifted by 53 because there are 52 cards. Once you point to an invalid state, you transition to 0. From there, any card that you pick must still result in an invalid state, so the first 53 entries 0 + 52 ranks point to 0. The first meaningful entry comes at 53..(53+53).

Try to understand the concept first. The table is a huge directed graph, where each node has 52 childnodes and one „evaluate-current-handrank-node“ at index 0. poker-ai.org and the original thread provide lots of useful information. Also look at CactusKevs idea to obtain handrank-order on cards by using prime numbers. Genius. It all goes into this.

Benj
  • 889
  • 1
  • 14
  • 31