R/run_round.R

Defines functions run_round

Documented in run_round

#' Run round of poker.
#'
#' @param n The number of players who will be dealt to.
#' @param chips The number of chips each player will start with.
#' @param start The index of the player who will make the first
#' action.
#' @param human A logical value which takes the value TRUE if the
#' actions are to be inputted in the console.
#' @return A list of elements from the S4 class "player".
#' @export
#'
#' @examples
run_round <- function(n,start,chips,human = TRUE){
  #Deal to n players and create starting parameters
  players <- preflop_deal(n,chips)
  deck <- players[[n+1]]
  players <- players[-(n+1)]
  pot <- 0
  raise <- 0
  skip <- rep(FALSE,n)

  #PREFLOP BETS - Retrieve actions
  bet_round <- take_bets(players,start,pot=0,
                         skip,preflop=TRUE,human=TRUE)
  players <- bet_round$players
  pot <- unlist(bet_round$pot)
  skip <- unlist(bet_round$skip)
  if(sum(skip) == n-1) return(players)

  #Deal the flop and take the flop round of bets.
  flop <- deal_to_players(n,ncards=3,players,deck)
  deck <- flop[[n+1]]
  players <- flop[-(n+1)]
  bet_round <- take_bets(players,start-2,pot=pot,
                         skip,preflop=FALSE,human=TRUE)
  players <- bet_round$players
  pot <- unlist(bet_round$pot)
  skip <- unlist(bet_round$skip)
  if(sum(skip) == n-1) return(players)

  #Deal the turn and take the turn round of bets.
  turn <- deal_to_players(n,ncards=1,players,deck)
  deck <- turn[[n+1]]
  players <- turn[-(n+1)]
  bet_round <- take_bets(players,start-2,pot=pot,
                         skip,preflop=FALSE,human=TRUE)
  players <- bet_round$players
  pot <- unlist(bet_round$pot)
  skip <- unlist(bet_round$skip)
  if(sum(skip) == n-1) return(players)

  #Deal the river and take the river round of bets.
  river <- deal_to_players(n,ncards=1,players,deck)
  deck <- river[[n+1]]
  players <- river[-(n+1)]
  bet_round <- take_bets(players,start-2,pot=pot,
                         skip,preflop=FALSE,human=TRUE)
  players <- bet_round$players
  pot <- unlist(bet_round$pot)
  skip <- unlist(bet_round$skip)
  if(sum(skip) == n-1) return(players)

  #Rank the hands and determine the winner.
  rankings <- rep(0,sum(!skip))
  indexed_players <- players
  names(indexed_players) <- 1:n
  remaining_players <- indexed_players[!skip]
  for (i in 1:sum(!skip)){
    rankings[i] <- rank_hand(remaining_players[i])$rank
  }
  names(rankings) <- 1:sum(!skip)
  rankings <- sort(rankings,decreasing = FALSE)
  winner <- rankings[1] %>% names() %>% as.numeric %>% remaining_players[.]
  details <- rank_hand(winner)
  index <- names(winner) %>% as.numeric()
  cat(names(players[index]),"wins with",details$class,"\n")
  cat("Pot:",pot)
  players[[index]]@chips <- players[[index]]@chips + pot
  return(players)
}
dfcorbin/pokersim documentation built on Nov. 13, 2019, 4:21 p.m.