montecarlo - R- Monte Carlo Poker Hand Counter -
i'm trying throw project practice. i'm trying count poker hands in monte carlo simulation using r.
i'm getting stuck on how handle straights, straight flushes, , royal flushes. code straights , straight flushes not included, did attempt "pseudo-code" royal flush. i'm hoping if can direction on royal flush, can figure out how handle straight flushes , straights. code ran intended before added code royalflush. code below:
poker.sim <- function (msim=10000,n=5) { # function simulate poker draws standard card deck # using grand loop approach # # create card deck denom = rep(c("a",2:10,"j","q","k"),4) suit = rep(c("s","h","d","c"),each=13) carddeck = data.frame(denom,suit) # initialize twosuit, onepair, twopair, threeofakind, flush, fullhouse, , fourofakind counters , storage later... count.twosuit = 0 count.onepair = 0 count.twopair = 0 count.threeofakind = 0 count.flush = 0 count.fullhouse = 0 count.fourofakind = 0 count.royalflush = 0 # begin grand loop for(i in 1:msim) { # determine card numbers hand select = sample(nrow(carddeck),n) # select rows card deck hand hand = carddeck[select,] # check twosuit , increment counter if twosuit occurs # statement counting every instance in 2 suits occur in five-card hand. if(length(unique(hand[,2]))==2) count.twosuit = count.twosuit+1 # check onepair , increment counter if onepair occurs # loop doing setting length of hand 4 possible cards. # first, second, , third cards can cards occurring once. # last 2 cards must same. tab = sort(table(as.vector(hand[,1]))) if(length(tab) ==4) { if(all(tab == c(1,1,1,2))) count.onepair = count.onepair+1 } # check twopair , increment counter if twopair occurs # loop doing setting length of hand 3 possible cards. # first card occur once. # last 2 cards occur twice each. if(length(tab) ==3) { if(all(tab == c(1,2,2))) count.twopair = count.twopair+1 } # check threeofakind , increment counter if threeofakind occurs. # function doing setting length of hand 3 possible cards. # first , second cards occur once each , can card. # last 3 cards must same. if(length(tab) ==3) { if(all(tab == c(1,1,3))) count.threeofakind = count.threeofakind+1 } # check flush , increment counter if flush occurs. # statement counting every instance in 1 suit occurs in five-card hand. if (length(unique(hand[,2]))==1) count.flush = count.flush+1 #check fullhouse , increment counter if fullhouse occurs # loop doing setting length of hand 2 possible cards. # first occurring twice, , second occurring 3 times. or vice versa. if(length(tab) ==2) { if(all(tab == c(2,3))) count.fullhouse = count.fullhouse+1 } # check fourofakind , increment counter if fourofakind occurs. # loop doing setting length of hand 2 possible cards. # first occurring once, , second occurring 4 times. if(length(tab) ==2) { if(all(tab == c(1,4))) count.fourofakind = count.fourofakind+1 } # check royalflush , increment counter if royalflush occurs. # restricted 1 suit , when run of 10,j,q,k,a occurs. if(length(unique(hand[,2]))==1) { if(tab == c(10,j,q,k,a) count.royalflush = count.royalflush+1 } } # close grand loop # then, count twosuits, onepairs, twopairs, threeofakinds, flushes, fullhouses, , fourofakinds # , divide them number of iterations of simulation respective probabilities. p.twosuit = count.twosuit/msim p.onepair = count.onepair/msim p.twopair = count.twopair/msim p.threeofakind = count.threeofakind/msim p.flush = count.flush/msim p.fullhouse = count.fullhouse/msim p.fourofakind = count.fourofakind/msim p.royalflush = count.royalflush/msim # output results out = list(msim,p.twosuit,p.onepair,p.twopair,p.threeofakind,p.flush,p.fullhouse,p.fourofakind,p.royalflush) names(out) = c("msim","p.twosuit","p.onepair","p.twopair","p.threeofakind","p.flush","p.fullhouse","p.fourofakind","p.royalflush") out }
focusing on royal flush part, think logic of checking if cards 10,j,q,k,a doesn't work. instead use setqual
check if elements of hand[, 1]
same vector c("10","j","q","k","a")
. have:
if(length(unique(hand[ , 2])) == 1) { if(setequal(hand[ , 1], c("10","j","q","k","a"))) count.royalflush = count.royalflush + 1 }
for checking if have straight, we'll need bit more complex condition. we'll create function checks straight
is_straight <- function(hand) { # first want encode "j", "q", "k" 11, 12, 13, can use diff later # take denominators hand character vector hand_denom <- as.character(hand[ , 1]) # , map acording values hand_denom[hand_denom == "j"] <- 11 hand_denom[hand_denom == "q"] <- 12 hand_denom[hand_denom == "k"] <- 13 # need check whether treat ace high or low , encode accordingly # if of cards two, ace treated 1 if it's straight # otherwise, we'll treat 14 hand_denom[hand_denom == "a"] <- ifelse(any(hand_denom == "2"), 1, 14) # check if numeric values successive (i.e. diff 1) all(diff(sort(as.numeric(hand_denom))) == 1) }
and use in code if (is_straight(hand)) <straight handling>
.
Comments
Post a Comment