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
#'
#' @examples
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))
}


set.seed(101)
cards <- new_cards(7,new_deck())[-8]
for(i in 1:7){
  cards[[i]]@suit <- "Hearts"
}
flush <- exists_flush(cards)
dfcorbin/pokersim documentation built on Nov. 13, 2019, 4:21 p.m.