library(purrr)
rankings <- 10:1
names(rankings) <- c("High Card","Pair", "Two Pair", "Three of a kind", "Straight", "Flush",
"Full House", "Four of a kind ", "Straight Flush")
suit <- 1:4
names(suit) <- c("Spades", "Clubs", "Hearts", "Diamonds")
library(methods)
setClass("card", slots = c(
suit = "character",
value = "numeric"
)
)
setClass("deck", slots = c(
withdrawn = "list"
)
)
setClass("player", slots = c(
hand="list",
chips = "numeric",
community="list"
)
)
#' Generate an object of class "deck"
#'
#' @return An new object of the S4 class "deck"
#' @export
#'
#' @examples
new_deck <- function(){
deck <- new("deck", withdrawn = list())
return(deck)
}
#' Generate a new card
#'
#' @return A new object of the S4 class "card"
#' @export
#' @importFrom purrr rdunif
#' @examples
new_card <- function(){
new("card",
suit = names(suit)[rdunif(1,1,4)],
value = rdunif(1,1,13)
)
}
#' Draw cards from a given deck.
#'
#' @param n The number of cards to be drawn from the deck
#' @param deck An object of the S4 class "deck" which
#' may have had cards drawn from it previously.
#'
#' @return A list of n+1 elements with the generated cards, as
#' well as the deck contained in the last element.
#' @export
#' @examples
new_cards <- function(n,deck){
if (n > 52-length(deck@withdrawn)) stop("Not enough cards remaining in the deck")
cards <- c()
count <- 0
while (count<n){
newcard <- new_card()
if (exists(paste(newcard@value,newcard@suit),where = deck@withdrawn)){
next
}
count = count +1
cards <- c(cards, newcard)
names(cards)[count] <- paste(newcard@value,newcard@suit)
deck@withdrawn <- c(deck@withdrawn, newcard)
names(deck@withdrawn)[length(deck@withdrawn)] <- paste(newcard@value,newcard@suit)
}
return(c(cards,deck))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.