May 1, 2019

556 words 3 mins read

How to make a deck of cards in R

I want to make a deck of cards. Here is how I do it.

First we need to understand the elements of the deck of cards. There are 4 different suits, 13 different values, and 13 different faces.

  1. Make the suits in order by naming the four suits and listing them 13 times so that all 3 lists are the same length.

  2. Make the faces by listing out all 13 and we can duplicate it from there.

faces <- c('king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'two', 'ace')
face <- rep(faces, 4)
  1. Make the values of the cards by listing out the values and then duplicating that 4xs
value <- rep(13:1, 4)
  1. Make the deck by combining the 3 different lists.
deck <- data.frame(face, suit, value)
tail(deck)
##     face   suit value
## 47   six hearts     6
## 48  five hearts     5
## 49  four hearts     4
## 50 three hearts     3
## 51   two hearts     2
## 52   ace hearts     1

Now we need to create the deal function

deal <- function(x) {
  sample_n(deck, x, replace = F)
}

hand<-deal(7)

Let’s play a game of blackjack

The biggest problem we are going to have is dealing cards from a single deck witout having it reshuffle using another function call. The remaining items in the deck must be saved as the new deck.

We will need to remove the items from the dealers hand and save it as a list item. Then we need to deal out the 2 cards for the player. If the user wants another card then he will need to ask for a new card while supplying the dealer hand and the user hand so that these rows are not included in the deck when asked.

Let’s edit the deal function to return all 3 elements if needed.

deal <- function(x, set = NULL){
  hand <- set$myhand
  dealerhand <- set$dealerhand
  myhand = list()
  #remove the existing hand from the deck if it is included
  if(!is.null(hand)) {
    deck <- deck %>% anti_join(hand)
  }
  #remove the existing dealer's hand from the deck if it is included
  if(!is.null(dealerhand)) {
    deck <- deck %>% anti_join(dealerhand)
  }
  #if there is not dealer hand included then we need to get him 2 cards
  if(is.null(dealerhand)) {
    dealerhand <- sample_n(deck, 2, replace = F)
    #once this is done we need to remove those 2 cards from the deck
    deck <- deck %>% anti_join(dealerhand)
  }
  #the new cards need to be drawn from the deck
  newhand <- sample_n(deck, x, replace = F)
  #if the hand is not null then 
  if(!is.null(hand)) {
          myhand <- rbind(hand, newhand) 
        } else {
          myhand <- newhand
          }
  hand <- list(myhand = myhand, dealerhand = dealerhand)
  return(hand)
  
}

#Deal the cards and get your set
set <- deal(2)
## Joining, by = c("face", "suit", "value")
set$myhand
##    face     suit value
## 1 seven   spades     7
## 2  jack diamonds    11
set$dealerhand
##   face   suit value
## 1 four spades     4
## 2 nine spades     9
#play on if you feel lucky!
set <- deal(1, set)
## Joining, by = c("face", "suit", "value")
## Joining, by = c("face", "suit", "value")
set$myhand
##    face     suit value
## 1 seven   spades     7
## 2  jack diamonds    11
## 3  king diamonds    13
set$dealerhand
##   face   suit value
## 1 four spades     4
## 2 nine spades     9