扑克:德州扑克包straight1函数?

Poker: Holdem package straight1 function?

假设 x 是德州扑克手牌(公共牌 + 底牌),其中 J:A = 11:14 且 A 可能 = 1。花色无关紧要。你只是为了顺子过牌。

x <- c(2,5,6,7,8,9,14)

这是 holdem 包中的 straight1 函数。除了函数末尾的 for 循环,我什么都懂。有人可以解释一下这部分是如何工作的。我迷路了。

function(x){
a1 = sort(unique(x))
if (length(a1)<4.5) return(0)
a3 = 0
n = length(a1)
if(a1[n] == 14) a1 = c(1,a1) ## count ace as both 1 and 14
a2 = length(a1)
for(j in c(5:a2)){ ## j will be the potential highest card of straight
if( sum(15^c(1:5) * a1[(j-4):j]) == sum(15^c(1:5) * ((a1[j]-4):a1[j]))) a3 = a1[j]
}
a3
}   ## end of straight1

我认为这个问题需要对需要的内容进行一些澄清:

Qstraight <-function(x){
     a1 = sort(unique(x))
     if (length(a1)<4.5) return(0)
     a3 = 0
     n = length(a1)
     if(a1[n] == 14) a1 = c(1,a1) ## count ace as both 1 and 14
     a2 = length(a1)
     for(j in c(5:a2)){ 
         ## j will be the potential highest card of straight
         if( sum(15^c(1:5) * a1[(j-4):j]) == 
             sum(15^c(1:5) * ((a1[j]-4):a1[j]))) a3 = a1[j]
                        }
    a3
    }   ## end

所以结果是...

 Qstraight(x)
#[1] 9  # i.e a "nine-high straight
x2 <- c(2,5,6,7,8,10,14)
 Qstraight(x2)
#[1] 0   # i.e not a straight at all.

我可能会对唯一值进行排序,然后取最大长度 rle(diff(unique(sort(x))))$values == 1 .. 或一些不太依赖模块化算法的东西。