Hey guys, I know this is a big ask but would anyone be able to help me convert this snippet of code to C# for use in Unity? I found it on this thread while looking for bit-wise poker hand evaluators, which is based on this article.
EDIT: Ignore the rest of this comment and skip to my latest reply, this just turned into a mess of me trying to bumble my way through this problem.
function evaluateHand(cs, ss) {
var pokerHands = ["4 of a Kind",
"Straight Flush",
"Straight",
"Flush",
"High Card",
"1 Pair",
"2 Pair",
"Royal Flush",
"3 of a Kind",
"Full House"];
var v,i,o,s = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
for (i = -1, v = o = 0; i < 5; i++, o = Math.pow(2, cs[i] * 4)) {
v += o * ((v / o & 15) + 1);
}
v = v % 15 - ((s / (s & -s) == 31) || (s == 0x403c) ? 3 : 1);
v -= (ss[0] == (ss[1] | ss[2] | ss[3] | ss[4])) * ((s == 0x7c00) ? -5 : 1);
return pokerHands[v];
}
I tried my best at the conversion attempting to fix any errors in Unity’s console log, these are the steps I took but I had no luck in the end :(:
First error; implicit variables v, i, o, s can’t have multiple declarators (I actually have no idea what the typing is on these variables). So I changed this:
var v,i,o,s = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
To this:
var v = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
var i = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
var o = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
var s = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
Which looks horrid, but the errors are gone now. Then next I cast: Mathf.Pow(2, cs[i] * 4) -in the for loop to an int. But the last error I can’t seem to figure out (on line 19):
Operator ‘*’ cannot be applied to operands of type ‘bool’ and ‘int’
This means that:
(ss[0] == (ss[1] | ss[2] | ss[3] | ss[4]))
-is a boolean right? I’ve never even seen these ‘|’ operators so I’m not sure how to go about fixing this. Any help would be appreciated! <3
EDIT: With a bit more searching, I’ve learned that ‘|’ is a bit-wise or operator in JavaScript, which seems different to C# from the Googling I’ve done:
JS: “Sets each bit to 1 if one of two bits is 1”
C#: “Copies a bit if it exists in either operand”
I’ve never even heard of bit-wise operators before this so I don’t know how to approach this.
Also those implicit variables are actually integers so I collapsed them back into 1 line.
EDIT2: Seems like the 2 operators are equivalent in both languages. I tried removing the comparison of ss[0] on line 19 which got rid of the error, but now there’s a divide by zero error on line 16 in the for loop. I’m unsure if this is caused by the removal, but I don’t know why it’s happening.
