• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

help with bits and bytes

hi, I starting learning delphi 2 months ago and it was a great joy. I created a program to evaluate poker hand in holdem games at online poker rooms with it.
I was stuck at last trying to find a way to implement a rating system that gives a numeric rank to any given 5 cards. I used 64bit integer values to assign ranks.
for example 231221211201191 would translate to AceC KingC QueenC JackC TenC
Ace has value of 23 and king 22. The 231 stands for Ace+Club(which is represented by the 1).
Then I multiply the resultnig rank with 10000000000000000 and add 9 if flush and 8 if straight etc...
This way I am both encoding the cards and rating the hands they constitute.
The program works fine. But I found a poker library here
using a different kind of rating by manipulating bits! this puzzled me as I have never dealt with these.

The comment in the library read:

   TcaaHandRating - this type is the core type of the rating system. It is
    implemented as a 32 bit Integer. Information about the hand is mapped as
    described below:

       rank           card5  card4  card3  card2  card1
 332222222222   1111   1111   1100   0000   0000
 109876543210   9876   5432   1098   7654   3210

    The most significant 12 bits (20..31) the contain hand rank.

    The lowest 20 bits (0..19) are divided into five nibbles which contain
    the ordinal values of the cards. The order the cards are in are based on
    the rules for evaluating the hands of poker and are described below:

Now how can a card be encoded in a nibble which is four bits as I read somewhere? Every card in my case like 231 occupies a byte and does not fit into a nibble which explains why i had to use 64bits
Any help appreciated.
And I am ready to explain my question more if needed.

  • 4
  • 2
2 Solutions
oalawnaAuthor Commented:
There is another question about the binary representation of number but let us close this Q first.

As you noted they must be using a different rating scheme than you are. The four bits gives them up to 16 possible values which is enough to store the card value, but not necessarily the suit.

Maybe the rank contains the suits, etc.
oalawnaAuthor Commented:
i see, but there are 52 cards. then this means that they are not encoding suits also
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

oalawnaAuthor Commented:
one more question. Is there is anything i should care about using int64. I mean drawbacks or expected problems?

No there shouldn't be. That is still a pretty small chunk of memory in the scheme of things. It really won't increase your total memory needs by too much.

Delphi Help says: "Note:Most standard routines that take integer arguments truncate Int64 values to 32 bits. However, the High, Low, Succ, Pred, Inc, Dec, IntToStr, and IntToHex routines fully support Int64 arguments. Also, the Round, Trunc, StrToInt64, and StrToInt64Def functions return Int64 values. A few routines—including Ord—cannot take Int64 values at all."

So just scout out the routines you want to use and make sure they handle Int64. Unless you want to build your own :)

You could also use a variant record. That way you could access the Int64 as two int32 or as an Int64 when needed. (The variant record shares memory space). For example:

  TCard = record
    case byte of
    1: (A : Integer;
        B : Integer);
    2: (C : Int64;)

  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    { Private declarations }
    Card : TCard;
    { Public declarations }

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
  Card.A:= 23;
  Card.B:= 44;
oalawnaAuthor Commented:
thanks Jturbin.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now