R/exists_flush.R

Defines functions exists_flush

Documented in exists_flush

#' Determine if a flush exists.
#'
#' @param cards A list of objects from the S4 Class 'card'.
#'
#' @return A list containing a logical representing if there is a flush,
#' and the flush itself if there is one.
#' @export
#'
exists_flush <- function(cards){
  n <- length(cards)
  suits <- c("Spades","Clubs","Hearts","Diamonds")
  #Iterate over each suit.
  for (i in suits){
    removed <- get_suit(cards,i) #Retrieve the cards from that suit.
    if (length(removed) >= 5){ #If more than 5 there exists a flush.
      values <- c()
      #Next, gather the values remaining cards in a vector.
      for ( j in 1:length(removed)){
        values <- c(values,removed[[j]]@value)
      }
      #Assign names to the vector representing the
      #original order of the elements.
      names(values) <- paste(1:length(removed))
      values <- sort(values,decreasing = TRUE)
      flush <- values[1:5] #Take the top 5 cards.
      index <- as.numeric(names(flush)) #Get their indexes from the
      #original order and retrieve the cards from removed.
      return(list("logical" = TRUE, "cards" = removed[index]))
    }
  }
  return(list("logical" = FALSE))
}
dfcorbin/pokersim2 documentation built on Jan. 15, 2020, 12:20 a.m.