deck.N<- data.frame(
face=c("king","queen","jack","ten","nine","eight","seven","six","five","four","three","two","ace",
"king","queen","jack","ten","nine","eight","seven","six","five","four","three","two","ace",
"king","queen","jack","ten","nine","eight","seven","six","five","four","three","two","ace",
"king","queen","jack","ten","nine","eight","seven","six","five","four","three","two","ace"),
suits=c("spade","spade","spade","spade","spade","spade","spade","spade","spade","spade","spade","spade","spade",
"clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs","clubs",
"diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond","diamond",
"hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts","hearts"),
value=c(13:1,13:1,13:1,13:1),
stringsAsFactors = F
)
deck.N
## face suits value
## 1 king spade 13
## 2 queen spade 12
## 3 jack spade 11
## 4 ten spade 10
## 5 nine spade 9
## 6 eight spade 8
## 7 seven spade 7
## 8 six spade 6
## 9 five spade 5
## 10 four spade 4
## 11 three spade 3
## 12 two spade 2
## 13 ace spade 1
## 14 king clubs 13
## 15 queen clubs 12
## 16 jack clubs 11
## 17 ten clubs 10
## 18 nine clubs 9
## 19 eight clubs 8
## 20 seven clubs 7
## 21 six clubs 6
## 22 five clubs 5
## 23 four clubs 4
## 24 three clubs 3
## 25 two clubs 2
## 26 ace clubs 1
## 27 king diamond 13
## 28 queen diamond 12
## 29 jack diamond 11
## 30 ten diamond 10
## 31 nine diamond 9
## 32 eight diamond 8
## 33 seven diamond 7
## 34 six diamond 6
## 35 five diamond 5
## 36 four diamond 4
## 37 three diamond 3
## 38 two diamond 2
## 39 ace diamond 1
## 40 king hearts 13
## 41 queen hearts 12
## 42 jack hearts 11
## 43 ten hearts 10
## 44 nine hearts 9
## 45 eight hearts 8
## 46 seven hearts 7
## 47 six hearts 6
## 48 five hearts 5
## 49 four hearts 4
## 50 three hearts 3
## 51 two hearts 2
## 52 ace hearts 1
write.csv(deck.N, file = "cards.csv", row.names = FALSE)
deck<-read.csv("cards.csv")
Back up data deck into child environment before playing directly on global environment (pg 109) note: Assign deck to DECK 1st (DECK will remember the value of deck at the time assigning, no matter what deck change after that
#Shuffle and deal cards ##1st game
setup<-function(deck){
DECK<-deck
DEAL<-function(){ # neu dung assign thay the cho <-,
card<-deck[1,] # minh se chon dc noi function chay
assign("deck",deck[-1,],envir=parent.env(environment())) #(truc tiep tren original deck tren global,
card # ko default chay a copy of deck tren runtime env nua)
}
SHUFFLE<-function(){
random<-sample(1:52,52)
assign("deck",DECK[random, ],envir = parent.env(environment()))
}
list(deal=DEAL,shuffle=SHUFFLE) #Let's ask setup to return DEAL and SHUFFLE so we can use them
}
cards<-setup(deck)
deal <- cards$deal
shuffle <- cards$shuffle
environment(deal)
## <environment: 0x00000000130f60b8>
environment(shuffle)
## <environment: 0x00000000130f60b8>
rm(deck)
shuffle()
The 1st deal is ‘jack, spade, 11’
The 2 deal is ‘nine, diamond, 9’
The 3 deal is ‘eight, hearts, 8’
The 4 deal is ‘ace, hearts, 1’
The 5 deal is ‘queen, clubs, 12’
The 6 deal is ‘jack, clubs, 11’
The 7 deal is ‘ten, spade, 10’
The 8 deal is ‘three, hearts, 3’
The 9 deal is ‘jack, diamond, 11’
The 10 deal is ‘two, spade, 2’
The deal is ‘ace, spade, 1’
The deal is ‘six, spade, 6’
The deal is ‘nine, spade, 9’
The deal is ‘king, diamond, 13’
The deal is ‘two, diamond, 2’
The deal is ‘five, hearts, 5’
The deal is ‘four, clubs, 4’
The deal is ‘seven, hearts, 7’
The deal is ‘eight, spade, 8’
The deal is ‘nine, hearts, 9’
The deal is ‘ten, clubs, 10’
The deal is ‘seven, spade, 7’
The deal is ‘eight, diamond, 8’
The deal is ‘two, clubs, 2’
The deal is ‘four, diamond, 4’
The deal is ‘five, diamond, 5’
The deal is ‘jack, hearts, 11’
The deal is ‘queen, diamond, 12’
The deal is ‘six, clubs, 6’
The deal is ‘five, spade, 5’
The deal is ‘seven, diamond, 7’
The deal is ‘four, hearts, 4’
The deal is ‘ten, hearts, 10’
The deal is ‘king, hearts, 13’
The deal is ‘queen, hearts, 12’
The deal is ‘king, spade, 13’
The deal is ‘ace, clubs, 1’
The deal is ‘two, hearts, 2’
The deal is ‘four, spade, 4’
The deal is ‘eight, clubs, 8’
The deal is ‘three, diamond, 3’
The deal is ‘ace, diamond, 1’
The deal is ‘three, clubs, 3’
The deal is ‘king, clubs, 13’
The deal is ‘nine, clubs, 9’
The deal is ‘three, spade, 3’
The deal is ‘seven, clubs, 7’
The deal is ‘ten, diamond, 10’
The deal is ‘six, diamond, 6’
The deal is ‘queen, spade, 12’
The 51 deal is ‘six, hearts, 6’
The 52 deal is ‘five, clubs, 5’
The 53 deal is ‘NA, NA, NA’
The 54 deal is ‘NA, NA, NA’
#END