Search code examples
rmathpoker

Poker: Holdem package straight1 function?


say x is a holdem hand (board + hole cards) where J:A = 11:14 and A may = 1. Suit doesn't matter. You're just checking for a straight.

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

This is the straight1 function from the holdem package. I understand everything but the for loop at the end of the function. Could someone please explain how that portion is working. I'm lost.

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

Solution

  • I think the question needs some clarification about what is needed:

    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
    

    So the result with this is ...

     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.
    

    I probably would have sorted the unique values and then taken the maximum of length of rle(diff(unique(sort(x))))$values == 1 .. or something less dependent on modular arithmetic.