Card Game Simulation

In this lab, we will simulate how to create a deck of cards, as well as what a hand will look like.

Create Deck

First, we need to create the deck.

cardArray <- array(1:52)
for (i in 1:52){
  cardArray[i]=i
}
print(cardArray)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50 51 52

This code will produce a deck of 52 cards.

Shuffle Deck

Now, we need to shuffle the deck so that all of the cards are in random places.

for (j in cardArray){
  randomNumber <- sample(1:52, 1)
  temp = cardArray[j]
  cardArray[j]=cardArray[randomNumber]
  cardArray[randomNumber]=temp
}
print(cardArray)
##  [1] 14  9  5 24 37 23 47  4  8 19  3 49 42 41 25 52 17  6  2 22 11 26 48
## [24] 16 31 18 20 36 28 46 32 51 40 44 50 10 34 13 43 33 35 45 21 38 12 39
## [47]  1 27 30 29  7 15

Deal Hand

Now, our deck is shuffled, so we can deal out a hand.

hand <- array(1:4)
for (k in 1:5){
  hand[k]=cardArray[k]
}
print(hand)
## [1] 14  9  5 24 37