拆包Exponent factors表将打包后的值乘以原始值



我有一个扑克游戏,其中可能有13张牌存储为[0到12]。每只手持有5张牌,其中有13个可能的牌组。

最终值是从指数13开始的标识符⁵(5的幂)。

它存储了它是什么样的获胜手牌。然后剩余的5次方13被用于存储5张牌中的每一张。

更不用说并不是所有的5张牌都一直保存着,这只是为了赢得高牌,需要4名踢球者。

我的问题是,只使用最终值,我如何才能打开每一张卡,以及这是一张什么样的获胜牌。

/** 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

正在尝试从该357451值中解包这些值。开始尝试计算这里的数学。

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

我想我已经想通了,但我不明白这些价值观。可能也只适用于这种情况,会打破任何其他情况。

不知道2, 18, 2115, 9165的值是从哪里产生的,可能是我编造的一些垃圾。

我该如何正确地做到这一点?我不认为我可以使用移位,因为这不是逐位的。

那么就这样结束了?

(357451 / 371293)          = 0
(357451 / 28561)           = 12
(357451 % 28561) / 2197    = 6
(357451 % 2197) / 169      = 9
(357451 % 169) / 13        = 1
(357451 % 13)              = 3

通过这一部分你是正确的。。

(357451 / 28561) = 12                     rankings[1] = 12

但这不好。。。

(357451 / 2197) / ((13*2)+1) = 6          rankings[2] = 6

你需要把结果12乘以28561,然后从357451中减去,看看剩下的是什么。在这种情况下是14719。

现在您可以继续使用该号码,而不是357451。所以14719/2197=6。

继续这个模式(14719-(2197*6)),直到你有了你的5个数字。

如果你想这样做的话,(357451%28561)也会得到剩余的钱。

我的"解码"代码。。。

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]);
}
}

最新更新