假设x是一个holdem手牌(棋盘+底牌),其中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
答案 0 :(得分:0)
我认为这个问题需要澄清一下需要什么:
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 ..或者更少依赖于模运算。