#' Determine if a straight 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 straight itself if there is one.
#' @export
#'
#' @examples
exists_straight <- function(cards){
n <- length(cards)
#Retreive the vector of values from the cards.
values <- c()
for ( i in 1:n){
values <- c(values,cards[[i]]@value)
}
#add the original indexes as names so we can keep
#track of which value relates to which card.
names(values) <- paste(1:n)
values <- sort(values,decreasing = TRUE)
#We will account for the case where an ace can
#be used as both 1 and 14.
if (1 %in% values){
values <- c(14,values) #Assign the same name
#to the duplicate ace so correct card returned.
names(values)[1] <- names(values[n+1])
}
unique_vals <- values %>% unique()
#Find the values which make up the straight
for(i in 1:(n-4)){
check <- unique_vals[i:(i+4)]
compare <- unique_vals[i]:(unique_vals[i]-4)
if (setequal(check,compare)){
#Retrieve the indexes from values
index <- match(check,values)
#Retrieve the original index relating to which card to use.
index <- names(values[index])
return(list("logical" = TRUE,"cards" = cards[as.numeric(index)]))
}
}
return(list("logical" = FALSE))
}
set.seed(102)
cards <- new_cards(15,new_deck())[-16]
cards <- exists_straight(cards)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.