R/exists_two_pair.R

Defines functions exists_two_pair

Documented in exists_two_pair

#' Detects and return the existence of two pairs
#'
#' @param cards A list of objects from the S4 class 'card'.
#'
#' @return list containing a logical representing if there are two pairs,
#' and the pairs themselves if they exist.
#' @export
#'
exists_two_pair <- function(cards){
  pair <- exists_n_kind(cards,2)
  if (!pair$logical) return(list("logical" = FALSE))
  pair <- pair$cards
  cards <- remove_cards(cards,pair)
  pair2 <- exists_n_kind(cards,2)
  if(!pair2$logical) return(list("logical" = FALSE))
  pair2 <- pair2$cards
  cards <- remove_cards(cards,pair2)
  values <- c()
  for ( i in 1:length(cards)){
    values <- c(values,cards[[i]]@value)
  }
  names(values) <- paste(1:length(cards))
  values <- sort(values,decreasing=TRUE)
  top_card <- names(values)[1] %>% as.numeric() %>% cards[.]
  cards <- c(pair,pair2,top_card)
  return(list("logical" = FALSE,"cards" = cards))
}
dfcorbin/pokersim2 documentation built on Jan. 15, 2020, 12:20 a.m.