I have this poker card game where a possible 13 card ranks stored as [0 to 12]. Each hand which holds 5 cards which have 13 possible card ranks.
The final value is a identifier which begins from the exponent 13⁵ (to power of 5).
Which stores what kind of winning hand it is. Then the remaining 5 powers of 13 are used to store each of the 5 cards.
Also not to mention not all 5 cards are stored at all times, that's only for the High Card win, which requires 4 kickers.
My question is using just the final value how would I be able to unpack every card and what kind of winning hand it was.
/** The ranking factors (powers of 13, the number of ranks). */
private static final int[] RANKING_FACTORS = {371293, 28561, 2197, 169, 13, 1};
rankings[0] = HIGHCARD WIN [0]
rankings[1] = 12; //Ace
rankings[2] = 6; //Eight
rankings[3] = 9; //Jack
rankings[4] = 1; //Three
rankings[5] = 3; //Five
// Calculate value.
for (int i = 0; i < NO_OF_RANKINGS; i++) {
value += rankings[i] * RANKING_FACTORS[i];
}
(0*371293) + (12*28561) + (6*2197) + (9*169) + (1*13) + (3*1) = 357451
Attempting to unpack the values from that 357451
value.
Starting trying to figure out the math here.
if 357451 < 371293 rankings[0] = 0
(357451 / 28561) = 12 rankings[1] = 12
(357451 / 2197) / ((13*2)+1) = 6 rankings[2] = 6
(357451 / 169) / ((13*18)+1) = 9 rankings[3] = 9
//Alright it seems that 18 is from answers (12+6) probably because I haven't subtracted them or something.
//So next one should be (12+6+9)= 27, but it's 2115
(357451 / 13) / ((13*2115)+1) = 1 rankings[4] = 1
(357451 / 1) / ((13*9165)+1) = 3 rankings[5] = 3
I think I figured it out but I don't understand the values Probably also only works for this case will break on any other case.
Don't know where the values 2, 18, 2115, 9165
get generated from probably some crap I made up.
How do I do this the proper way? I don't think I could use shifting since this isn't bitwise.
So its done like this then?
(357451 / 371293) = 0
(357451 / 28561) = 12
(357451 % 28561) / 2197 = 6
(357451 % 2197) / 169 = 9
(357451 % 169) / 13 = 1
(357451 % 13) = 3
You're correct through this part..
(357451 / 28561) = 12 rankings[1] = 12
But this is no good...
(357451 / 2197) / ((13*2)+1) = 6 rankings[2] = 6
You need to take your result 12 and multiply it back to 28561 then subtract that from 357451 to see what was left over. In this case it's 14719.
Now you can continue using that number instead of 357451. So 14719 / 2197 = 6.
Continue that pattern (14719 - (2197*6)) until you have your 5 numbers.
(357451 % 28561) will also get you the remainder, if you want to do it that way.
My "decode" code...
private static final int[] RANKING_FACTORS = {4826809, 371293, 28561, 2197, 169, 13, 1};
@Test
public void testDecode() {
long value = 357451;
int[] rankings = new int[6];
//System.out.println(Math.max(0,value-RANKING_FACTORS[0]));
for (int i=0; i < rankings.length; i++) {
rankings[i] = (int)(value / RANKING_FACTORS[i]);
value %= RANKING_FACTORS[i];
System.out.println(rankings[i]);
}
}