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)
}