Jump to content
  • 0

Bitwise Operators / Binary values


Bake Mono

Question


  • Group:  Members
  • Topic Count:  8
  • Topics Per Day:  0.00
  • Content Count:  26
  • Reputation:   1
  • Joined:  11/15/11
  • Last Seen:  

I was working on a card game (poker) and ran into a slight problem. I started with an array that gave all cards a certain value. The following shows how the values were given.

2, 3, 4, 5 ... King, Ace

1, 2, 4, 8 ... 2048, 4096

No matter what suit they had, a straight (ace high) would have a value of 7,936. This made it easy to determine the hands and who won. However the problem started when ever there were multiple values of some cards. The following hand is an example:

Note: 2 cards in hand + 5 on table = 7 cards total

Card: 2, 2, 3, 3, 4, 5, 6

Val : 1, 1, 2, 2, 4, 8, 16

Total value of 34

With the total value of 34, it reads it as 32 + 2. So esseitially I can not check for that value, because if someone had a 7 and 3 card, they would have the same value.

Is anyone able to think of an alternative to this, or possibly a way that I can use the bit values given to the card to accept and/or search for a pair, two pair, three of a kind, full house, four of a kind? I've been trying to think of alternatives for close to a week and it's just giving me a headache.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  130
  • Reputation:   43
  • Joined:  12/11/11
  • Last Seen:  

Note that there are many ways to do this and I haven't worked anything with cards before so this could be far from the most efficient way.

There are a total of 52 cards in a deck, an integer can at most store 32 different bits so you can ultimately get away with using only 2 integers for each hand (or table).

The first integer could hold the 26 black cards and the second integer the 26 red cards. This however is just a storage solution.

When you calculate the hands, you can just OR (you can also XOR if you only play with 1 deck) the tables two variables with the hands two variables to combine them when matching. You then check for combinations, starting with the highest score one and going down.

Example:

set .@black, .@tableblack | .@handblack;

Make sure that you set high card, both on the hand and on the rest.

Say they both had pair of 10, but the high card was Ace vs 2.

I'll give an example of matching Three of a kind using two different approaches:

First some info on how we store the cards in the variables.

8191: the first 13 bits set to 1

67100672: bits 14 -> 26 set to 1

Spades are (.@black & 8191)

Clubs are (.@black & 67100672)

Hearts are (.@red & 8191)

Diamonds are (.@red & 67100672)

1) Counting amount of hits, counting Ace as 13th bit, 1 as first bit

for(set .@i, 12; .@i >= 0; set .@i, .@i-1){
 set .@j, .@i+13;
 if( ((.@black >> .@i) & 1) + ((.@black >> .@j) & 1) + ((.@red >> .@i) & 1) + ((.@red >> .@j) & 1) == 3 ){
break;
 }
}
set .@i, .@i+1;
if(.@i != 0) mes "We have a match for card value " + .@i;

2) Using some bitwise operands and a karnaughdiagram:

S is Spades, C is Clubs, H is Hearts and D is Diamonds

First expression:

S & C & H | S & C & D | S & H & D | C & H & D

Optimising:

S & ( C & H | C & D | H & D ) | C & H & D
S & ( C & ( H | D ) | H & D ) | C & H & D
HD = H & D

Result:

S & ( C & ( H | D ) | HD ) | C & HD

Which gives us the code without loops:

// This part is put before all matching
set .@S, (.@black & 8191);
set .@C, (.@black & 67100672);
set .@H, (.@red & 8191);
set .@D, (.@red & 67100672);
set .@HD, .@H & .@D;

// this is the match for three of a kind
set .@R, .@S & ( .@C & ( .@H | .@D ) | .@HD ) | .@C & .@HD;

Then after that you can check the highest bit set with a trick found in Hacker's Delight:

set .@R, .@R | (.@R >> 1);
set .@R, .@R | (.@R >> 2);
set .@R, .@R | (.@R >> 4);
set .@R, .@R | (.@R >> 8);
set .@R, .@R | (.@R >> 16);
set .@R, .@R - (.@R >> 1);

if(.@R != 0) mes "We have a match for card value " + .@R;

After we have found a match with one of the two methods, you can get the high card easily by excluding the match from the hand by:

  1. Merging the four colors
  2. Excluding the match with XOR
  3. Checking highest bit set

Note that you won't know the color by this, so if their high cards match you will have to check which color the user has on it's high card.

Edited by plankt
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...