In this lab, we will simulate how to create a deck of cards, as well as what a hand will look like.
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.
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
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
Now, we have our hand dealt out, so let’s see what it looks like.
for (ii in hand){
if (((hand[ii]%%13)==0)||((hand[ii]%%13)==1)||((hand[ii]%%13)==2)||((hand[ii]%%13)==3)||((hand[ii]%%13)==4)||((hand[ii]%%13)==5)||((hand[ii]%%13)==6)||((hand[ii]%%13)==7)||((hand[ii]%%13)==8)){
print((hand[ii]%%13)+2)
}
else if(hand[ii]%%13==9){
print("Jack")
}
else if(hand[ii]%%13==10){
print("Queen")
}
else if(hand[ii]%%13==11){
print("King")
}
else if(hand[ii]%%13==12){
print("Ace")
}
else{
print("Error")
}
}
print("of")
for (iii in hand){
if (hand[iii]/13==0){
print("Hearts")
}
else if(hand[iii]/13==1){
print("Diamonds")
}
else if(hand[iii]/13==2){
print("Spades")
}
else if(hand[iii]){
print("Clubs")
}
else{
print("Error")
}
}
I couldn’t get this to work however. I modelled everything after 13 numbers, as that’s how many different cards there are. I tried using conditional statements, along with modulus and regular division, to try to demonstrate what a single card in the hand would be. However, there appears to be an error that I do not know how to solve involving the modulus math and the if statements.