# The following function calculates the probabilities of drawing a pair or a tris in a Texas Hold'em game with 2 players and the dealer pairs_and_tris <- function(n) { # This function searches for all the pairs in 100 hands and returns the number of pairs found find_pairs <- function() { pair = 0 for(i in 1:100){ if((player1[i,1] == player1[i,2]) | (is.element(player1[i,1],dealer[i,])) | (is.element(player1[i,2],dealer[i,]))) { pair = pair + 1 } } return(pair) } # This function searches for all the tris in 100 hands and returns the number of tris found find_tris <- function() { tris = 0 for(i in 1:100) { test = 0 if((is.element(player1[i,1],dealer[i,]) | is.element(player1[i,2],dealer[i,])) && (player1[i,1] == player1[i,2])) { tris = tris + 1 } for(b in 2 : 5){ if(dealer[i,b] == dealer[i,b-1]) { dealer_pair = T test = dealer[i,b] if(is.element(test,player1[i,]) && dealer_pair) { tris = tris + 1 } } } } return(tris) } # This function searches for all the pokers in 100 hands and returns the number of pokers found find_poker <- function() { poker = 0 for(i in 1:100) { test = 0 dealer_pair = 0 test2 = 0 dealer_tris = 0 for(b in 2 : 5) { test = 0 if(dealer[i,b] == dealer[i,b-1]) { dealer_pair = T test = dealer[i,b] }else { dealer_pair = F } if((player1[i,1] == player1[i,2]) && (dealer_pair) && (player1[i,1] == test)) { poker = poker + 1 #------------------- print(player1[i,]) print(dealer[i,]) } } for(b in 3 : 5) { test2 = 0 dealer_tris = 0 if((dealer[i,b] == dealer[i,b-1]) && (dealer[i,b-1] == dealer[i,b-2])) { dealer_tris = T test2 = dealer[i,b] }else { dealer_tris = F } if(dealer_tris && ((player1[i,1] == test2) | (player1[i,2] == test2))) { poker = poker + 1 #------------------- print(player1[i,]) print(dealer[i,]) } } } return(poker) } # The code below calculates probabilties of pairs and tris for each of the n 100 texas holdem hands simulation # The vector help_vector is used to build the matrix risultati_p1 where all the pairs and tris will be stored help_vector <- c(0,0,0) results_p1 <- matrix(help_vector,ncol=3) colnames(results_p1) = c("Pairs per 100 hands","Tris per 100 hands","Poker per 100 hands") for(i in 1:n) { player1Hands <- c(0,0) player1 <- matrix(player1Hands,ncol=2) player2Hands <- c(0,0) player2 <- matrix(player2Hands,ncol=2) dealerst <- c(0,0,0,0,0) dealer <- matrix(dealerst,ncol=5) # The code below simulates 100 texas hold'em hands for(i in 1:100) { first_round <- hand() p1_h <- c(first_round[[1]][1],first_round[[1]][2]) player1 <- rbind(player1,p1_h) p2_h <- c(first_round[[2]][1],first_round[[2]][2]) player2 <- rbind(player2,p2_h) deal_h <- c(first_round[[3]][1],first_round[[3]][2],first_round[[3]][3],first_round[[3]][4],first_round[[3]][5]) dealer <- rbind(dealer,deal_h) } player1 <- player1[-1,] player2 <- player2[-1,] dealer <- dealer[-1,] pairs <- find_pairs() tris <- find_tris() #---------------- poker <- find_poker() #---------------- results__p1 <- c(pairs,tris,poker) results_p1 <- rbind(results_p1,results__p1) } results_p1 <- results_p1[-1,] # Results for each 100 hand are shown View(results_p1) # Final result is printed string_to_print <- paste("Given a Texas Hold'em game and two players, here are probabilities for p1 in",n*100,"hands:",sep=" ") print(string_to_print) # Please note that probabilites are calculated as average of pairs and tris found mean_scores = c(mean(results_p1[,1]),mean(results_p1[,2]),mean(results_p1[,3])) names(mean_scores) = c("Probability of a pair,","Probability of a tris","Probabilty of a poker") return(mean_scores) }