c - How we can iterate cards combinations efficiently? -




there huge number of poker hands possible. decrease removing similar hands can iterate through each individual possible combination.

in order so, possible combinations denoted strings of length 52 '0' signifies card not present , '1' signifies card present.

so hands of 2,3,4,5,6 of hearts be: '0000000000000000000000000000000000000000000000011111'

2,4,10,a of hearts , 3 of clubs : '0000000000000000000000000000000000000101000100000101'

and on...

here first 13 bits signify a,k,q,...4,3,2 of spades, next 13 bits diamonds next 13 clubs , last 13 hearts

now need is, decimal value of binary string, reduce similar poker hand's binary string , return decimal value

so mean when similar hands:- - a,q,7,6,2 of spades = aq762 of clubs = aq762 of hearts = aq762 of diamonds each of binary representation can reduced binary representation of aq762 of hearts, smallest - similarly, hand of 2,3,4,5 of spades , of diamonds equivalent of hearts , 2,3,4,5 of clubs, former can reduced latter.

asksqsjsts = 1111100000000000000000000000000000000000000000000000 ackcqcjctc = 0000000000000000000000000011111000000000000000000000 adkdqdjdtd = 0000000000000111110000000000000000000000000000000000 ahkhqhjhth = 0000000000000000000000000000000000000001111100000000  ac5s4s3s2s = 0000000001111000000000000010000000000000000000000000 ah5c4c3c2c = 0000000000000000000000000000000000011111000000000000 ac5d4d3d2d = 0000000000000000000000111100000000000001000000000000 

but know:

akqjts == akqjtc == akqjtd == ... same, because combination have same strength in poker, can simplify suits ac5432s == ah5432c == ah5432d == ... same 

i think can use abstraction suits: [1], [2], [3], [4]

[1] - new first suit, suit of highest card [2] - second suit if going down highest card   65432[1] == 65432s == 65432c == 65432d == .. 6[1]5432[2] == 6c5432s == 6h5432c == 6c5432d == .. 

the first suit of highest card [1] , next new suit [2]....

and masks be:

akqjts=akqjtc=akqjtd=akqjth = 0000000000000000000000000000000000000001111100000000 = 7936  ah5432c=ac5432d= ... = 0000000000000000000000000000000000011111000000000000 = 126976 

how can make mask conversion efficiently?

like - simplyfymask(4362862139015168) = 7936 simplyfymask(1040187392) = 126976

ps: if use mask: aces - 0,1,2,3, kings - 4,5,6,7, jacks - 8,9,10,11 - bits?

in example provided: simplyfymask(1040187392) = 126976 can furthur reduce this:
-0000000000000000000000000000000000011111000000000000
- - -0000000000000000000000000000000000000011000000001110
have equal ranks poker hand

here python code (hope helps, not c programmer, didn't answer in c language)

def replacebyindex(string, index, value) :     li = list(string)     li[index] = value     return ''.join(li)  def checkflush(string) :     suit in range(4) :         count = 0         # print(suit,'-th suit check flush')         card in range(13) :             if string[suit*13 + card] != '0' :                 count += 1         if count == 0 :             continue         elif count == 5 :             # print('flushed')             return true         else :             # print(count,'cards counted for',suit,'-th suit')             return false  def unflushify(string) :     if checkflush(string) :         card in range(13) :             index = 51 - card             if string[index] == '2' :                 string = replacebyindex(string,index-13,'2')                 string = replacebyindex(string,index,'0')                 break     return string  def  simplifymask(num) :     num = bin(num)     num = num[2:]     size = len(num)     num = '0'*(52-size) + num     # print(num, len(num))     count = 0     = 0     if not checkflush(num) :         while i<52 , count<5 :             if num[i] == '1' :                 count += 1                 tgt_index = 39 + i%13                 updated = false                 while tgt_index>i , num[tgt_index] != '0' :                     tgt_index -=13                 if tgt_index <= :                     num = replacebyindex(num,i,'2')                 else :                     num = replacebyindex(num,i,'0')                     num = replacebyindex(num,tgt_index,'2')             i+=1         num = unflushify(num)     else :         while i<52 , count<5 :             if num[i] == '1' :                 count+=1                 num = replacebyindex(num,i,'0')                 num = replacebyindex(num,39+i%13,'2')     return num.replace('2','1')   num = int(input()) number = simplifymask(num) print(int(number,2),number) 

i consider myself beginner in coding, might not efficient piece of code, should give idea of how proceed. :)





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -