-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck
194 lines (143 loc) · 4.96 KB
/
deck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
library(tidyverse)
library(gridExtra)
deck <- 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"),
suit = c("spades", "spades", "spades", "spades", "spades", "spades",
"spades", "spades", "spades", "spades", "spades", "spades", "spades",
"clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs",
"clubs", "clubs", "clubs", "clubs", "clubs", "diamonds", "diamonds",
"diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds",
"diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "hearts",
"hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts",
"hearts", "hearts", "hearts", "hearts", "hearts"),
value = c(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8,
7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
)
head(deck)
str(deck)
## Uma mao de carta
hand <- deck[sample(nrow(deck), 6), ]
##Funcao para amostra
hand_s <- function(){
deck[sample(nrow(deck), 6), ] %>%
group_by(suit) %>%
mutate(total = sum(value)) %>%
ggplot(aes(x = suit, y = total, color = suit))+
geom_point()
}
## Jogadores
hand_s <- function(){
playerA <- deck[sample(nrow(deck), 6), ] %>%
arrange(suit, value)
playerB <- deck[sample(nrow(deck), 6), ] %>%
arrange(suit, value)
str_c('Player A:',playerA$suit,playerA$value,
'Player B:',playerB$suit,playerB$value, sep = ' ')
}
## Jogadores grafico
hand_s <- function(){
playerA <- deck[sample(nrow(deck), 6), ] %>%
group_by(suit)%>%
mutate(total = sum(value))
plotA <- ggplot(data = playerA, aes(x = suit, y = total, color = suit))+
geom_point()
playerB <- deck[sample(nrow(deck), 6), ] %>%
group_by(suit)%>%
mutate(total = sum(value))
plotB <- ggplot(data = playerB, aes(x = suit, y = total, color = suit))+
geom_point()
grid.arrange(plotA, plotB)
}
pontos_spades <- playerA %>%
filter(suit == 'spades')%>%
group_by(suit)
## rolled
get_symbols <- function(){
wheel <- c('DD','7','BBB','BB','B','C','0')
sample(wheel, size = 3, replace = TRUE,
prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52))
}
get_symbols()
## Versão do livro
score <- function(symbols){
identidade <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bar <- symbols %in% c('B','BB','BBB')
if (identidade){
combination <- c('DD' = 100, '7' = 80, 'BBB' = 40, 'BB' = 25, 'B' = 10, 'C' = 10, '0' = 0)
prize <- unname(combination[symbols[1]])
} else if (all(bar)) {
prize <- 5
} else {
cherries <- sum(symbols == 'C')
prize <- c(0, 2, 5)[cherries + 1]
}
diamonds <- sum(symbols == 'DD')
prize * 2 ^ diamonds
}
## Versão completa
play <- function(){
symbols <- get_symbols()
## Imprime as Amostras
cat('...ROLETA...','\n','\n',symbols,'\n')
cat('Seu Score: ','\n',score(symbols),'\n','\n')
if(score(symbols) > 0){
print('Parabéns você pontuou...')
} else {
print('Pontuação zero... Tente outra vez...')
}
}
## Versão Gustavo
pontua <- function(x){
## cria a matriz de pontuação maior
points <- data.frame(
combination = c('DD DD DD','7 7 7','BBB BBB BBB','BB BB BB','B B B','C C C'),
prize = c(100, 80, 40, 25, 10, 10)
)
barrs <- c('BBB BB B','BBB B BB','B')
cherry <- c('C C', 'C')
# testa se esta na matriz de pontuação
if (sum(str_count(points$combination, str_c(x,'$'))) > 0){
points[str_detect(points$combination, str_c(x,'$')) == TRUE, ][[2]]
} else {
##Testa se tem algun valor C
if (sum(str_count(cherry, x) > 0)){
return(5)
}
return(0)
}
}
## Calculo das probabilidades
wheel <- c('DD','7','BBB','BB','B','C','0')
combo <- expand.grid(wheel, wheel, wheel, stringsAsFactors = FALSE)
prob = c('DD' = 0.03, '7' = 0.03, 'BBB' = 0.06, 'BB' = 0.1, 'B' = 0.25,
'C' = 0.01,'0' = 0.52)
## Acrescent as probabilidades
combo$prob1 <- prob[combo$Var1]
combo$prob2 <- prob[combo$Var2]
combo$prob3 <- prob[combo$Var3]
combo$prob <- combo$prob1 * combo$prob2 * combo$prob3
head(combo)
## loops
chars <- vector(length = 4)
words <- c('My','fourth','for','loop')
for (i in 1:4){
chars[i] <- words[i]
}
result <- NULL
for (i in 1:nrow(combo)){
result[i] <- score(c(combo[i,1],combo[i,2],combo[i,3]))
}
## adiciona o score no combo
combo$score <- result
sum(combo$score * combo$prob)
## Executa 100 vezes o play
pontuacao <- vector('numeric', length = 1000)
for (i in 1000){
pontuacao[i] <- play()
}