# H = 1, P = 2, C = 3, W = 4
rand <- function(a, b) {
  floor(runif(1, a, b+1))
}

shuffle <- function(cards) {
 for(i in length(cards):1) {
    r = rand(1, i) 
    cardsi = cards[i]
    cards[i] = cards[r]
    cards[r] = cardsi
 }
  cards
}

checkHand <- function(cards) {
  if(length(which(cards==1)) == 3 || length(which(cards==2)) == 3 || length(which(cards==3)) == 3 ||
       (length(which(cards==1)) >= 1 && length(which(cards==2)) >= 1 && length(which(cards==3)) >= 1) ||
       (length(which(cards==1)) >= 1 && length(which(cards==2)) >= 1 && length(which(cards==4)) >= 1) ||
       (length(which(cards==1)) >= 1 && length(which(cards==3)) >= 1 && length(which(cards==4)) >= 1) ||
       (length(which(cards==2)) >= 1 && length(which(cards==3)) >= 1 && length(which(cards==4)) >= 1) ||
       (length(which(cards==4)) == 2)) {
    return(1) # winning hand
  } else {
    return(0) # losing hand
  }
}

runTrial <- function(numCards=3, cards=c(rep(1,14), rep(2,14), rep(3,14), rep(4,2))) {
  cards = shuffle(cards)
  hands <- numeric(0)
  
  total = 0
  for(i in 1:floor(44/numCards)) {
    hand = cards[((i-1)*numCards+1):(i*numCards)]
    total = total + checkHand(hand)
  }
  
  return(total/floor(44/numCards))
}

n = 500

total = 0
for(i in 1:n) total = total + runTrial(numCards=3) # ~ 0.3815
cat("prob set with 3 cards from", n*14, "trials = ", total/n, "\n")
## prob set with 3 cards from 7000 trials =  0.3686
total = 0
for(i in 1:n) total = total + runTrial(numCards=4) # ~ 0.7945
cat("prob set with 3 cards from", n*13, "trials = ", total/n, "\n")
## prob set with 3 cards from 6500 trials =  0.8044