0

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
R Curti
  • 31
  • 5
  • you should probably tag this with the programming language used, and clean up the formatting to make it readable. – Herb Oct 22 '16 at 01:42

1 Answers1

0

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • This works well to determine if there was a straight or not. Is there a simple way to return the value of the highest card in the straight without using the above modular arithmetic? – R Curti Nov 03 '16 at 20:49
  • Sounds like another question. After all you were the one that wanted it done with modulo-arithmetic. – IRTFM Nov 03 '16 at 21:10