Create a deck of cards in R without using and double looping

I am creating a blackjack simulator in R. The code below manages to create a deck (s) of cards that I want. (For those who play, I will deal with the value of Ace later).

My question is, is there a better way to create a deck that does not include a while loop plus a double loop? I have more problem with double for loop. The while loop is probably inevitable, since the number of decks created is variable.

I also initialize an empty data frame, which, as I know, is not the best way, but the data set is so small in this case that it will not affect performance.

And finally, is there an i ++ equivalent in R? I also programmed in java and got used to it.

Thank.

createDeck <- function(totalNumOfDecks = 2)
{
  suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
  cards <- c("Ace", "Deuce", "Three", "Four","Five", 
             "Six", "Seven", "Eight", "Nine", "Ten", 
             "Jack", "Queen", "King")
  values <- c(0,2,3,4,5,
              6,7,8,9,10,
              10,10,10)

  deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))

  numOfDecks = 1

  while (numOfDecks <= totalNumOfDecks){
    for (i in suits){
      for (j in cards){
        deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
      }
    }
    numOfDecks = numOfDecks + 1
  }

  print(deck)
}
+4
4

expand.grid :

# Define suits, cards, values
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
values <- c(0, 2:9, rep(10, 4))
totalNumOfDecks <- 2

# Build deck, replicated proper number of times
deck <- expand.grid(cards=cards, suits=suits)
deck$value <- values
deck <- deck[rep(seq(nrow(deck)), totalNumOfDecks),]

expand.grid . value value . , rep(seq(nrow(deck))) 1-52 , .

+6

( ), , , .

#Setup
suits <- c('Clubs','Diamonds','Hearts','Spades')
card <- c('Ace','Two','Three','Four','Five',
                    'Six','Seven','Eight','Nine','Ten',
                    'Jack','Queen','King')
value <- c(0,2:10,rep(10,3))
deck <- 0:51

#Full deck
suits[(deck %/% 13) + 1]
card[(deck %% 13) + 1]
value[(deck %% 13) + 1]

#Some random cards
hand <- sample(deck,5)
suits[(hand %/% 13) + 1]
card[(hand %% 13) + 1]
value[(hand %% 13) + 1]
+3
buildDeck <- function(noOfDecks=1){
    suits <- c("Clubs", "Spades", "Diamonds", "Hearts")
    cards <-c("Ace", 2:10, "Jack", "Queen", "King")
    Deck <- paste(cards, rep(suits, each=13), sep="-")
    d<-rep(Deck,noOfDecks) #Build decks
    shuffledDeck <-sample(d,length(d)) #Shuffle decks
    shuffledDeck
}
+1

paste.grid; . , levels(interaction(...)).

Just made a deck of your own, and she uses Unicode characters for costumes to make her look amazing; here is what i did:

deck <- paste0(rep(c(2:10, "J", "Q", "K", "A"), 4),  #card values
               rep(c("♠", "♥", "♦", "♣"), each = 13)) #suits
deck
#  [1] "2♠"  "3♠"  "4♠"  "5♠"  "6♠"  "7♠"  "8♠"  "9♠"  "10♠" "J♠"  "Q♠"  "K♠" 
# [13] "A♠"  "2♥"  "3♥"  "4♥"  "5♥"  "6♥"  "7♥"  "8♥"  "9♥"  "10♥" "J♥"  "Q♥" 
# [25] "K♥"  "A♥"  "2♦"  "3♦"  "4♦"  "5♦"  "6♦"  "7♦"  "8♦"  "9♦"  "10♦" "J♦" 
# [37] "Q♦"  "K♦"  "A♦"  "2♣"  "3♣"  "4♣"  "5♣"  "6♣"  "7♣"  "8♣"  "9♣"  "10♣"
# [49] "J♣"  "Q♣"  "K♣"  "A♣"
+1
source

Source: https://habr.com/ru/post/1547976/


All Articles