-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 7 code.R
294 lines (242 loc) · 6.9 KB
/
Chapter 7 code.R
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# function that generates 3 random symbols from a set
## of symbols
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()
# creating the play() using sequential steps
play <- function(){
# step one: generate symbols
symbols <- get_symbols()
# step two: display the symbols
print(symbols)
# step three: score the symbols
score(symbols)
}
# outline of the slot machine function
if ( # Case 1: all the same ) {
prize <- # look up the prize
} else if ( # Case 2: all bars ) {
prize <- # assign $5
} else {
# count cherries
prize <- # calculate a prize
}
# count diamonds
# double the prize if necessary
# outline of the score function
## the argument symbols will be the output of get_symbols()
score <- function(symbols){
# calculate a prize
prize
}
# we should not write the entire score() function in one
## go as it will have 8 different possible outcomes.
## if the function does not work it will be more
## difficult to debug.
## instead write one subtask at a time and test individually
## therefore we create a symbols vector
symbols <- c("7", "7", "7")
# exercise - write the statement as a logical test
symbols[1] == symbols[2] & symbols[2] == symbols[3]
same <- symbols[1] == symbols[2] & symbols[2] == symbols[3]
# exercise - write a Boolean test that will determine
## whether a vector name symbols contains only symbols
## of type 'bar'
symbols <- c("B", "BBB", "BB")
all(symbols %in% c("B", "BB", "BBB"))
bars <- symbols %in% c("B", "BB", "BBB")
# what our slot machine function looks like
if (same) {
prize <- # look up the prize
} else if (all(bars)) {
prize <- # assign $5
} else {
# count cherries
prize <- # calculate a prize
}
# count diamonds
# double the prize if necessary
# Next sub task - assign a prize for symbols
## we could write a series of else/if statements
# if (same) {
# symbol <- symbols[1]
# if (symbol == "DD") {
# prize <- 800
# } else if (symbol == "7") {
# prize <- 80
# } else if (symbol == "BBB") {
# prize <- 40
# } else if (symbol == "BB") {
# prize <- 5
# } else if (symbol == "B") {
# prize <- 10
# } else if (symbol == "C") {
# prize <- 10
# } else if (symbol == "0") {
# prize <- 0
# }
# }
# or we could use lookup tables
## create a vector naming each of the elements
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
payouts
payouts["DD"]
# to not print the symbols name use unname()
unname(payouts["DD"])
# we can subset payouts with another subseted vector
## this allows us an automated way to calculate
## the prize when symbols contains three of a kind
symbols <- c("7", "7", "7")
payouts[symbols[1]]
# update the code
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- # assign $5
} else {
# count cherries
prize <- # calculate a prize
}
# count diamonds
# double the prize if necessary
# case to is when the symbols are all bars
## in this case the price is five dollars
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- 5
} else {
# count cherries
prize <- # calculate a prize
}
# count diamonds
# double the prize if necessary
# exercise - How can you tell which elements of a vector
## named symbols are a C ? Devise a test and
## try it out
## my attempt
# symbols <- c('7', 'C', '7')
# count <- 0
# for (i in symbols) {
# if (i == 'C') {
# print(count)
# count <- count + 1
# } else{
# count <- count + 1
# }
# }
# how to find how may cherries are in symbols - easier
symbols <- c("C", "DD", "C")
sum(symbols == "C")
# use the same method to count diamonds
sum(symbols == "DD")
# update the code
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- 5
} else {
cherries <- sum(symbols == "C")
prize <- # calculate a prize
}
diamonds <- sum(symbols == "DD")
# double the prize if necessary
cherries <- sum(symbols == "C")
# case 3 - $5 for two cherries, $2 for one cherry
## inefficient method
if (cherries == 2) {
prize <- 5
} else if (cherries == 1) {
prize <- 2
} else {
prize <- 0
}
## more efficient method - again use lookup table
c(0, 2, 5)[cherries + 1]
# update the code
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- 5
} else {
cherries <- sum(symbols == "C")
prize <- c(0, 2, 5)[cherries + 1]
}
diamonds <- sum(symbols == "DD")
# double the prize if necessary
# exercise - write a method for adjusting prize
## based on diamonds - my attempt
double <- c(1, 2, 4, 8)
diamonds <- sum(symbols == 'DD')
prize <- prize * double[diamonds + 1]
prize
## easier method
prize * 2 ^ diamonds
# update the code - final version
## identify case
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
## get prize
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- 5
} else {
cherries <- sum(symbols == "C")
prize <- c(0, 2, 5)[cherries + 1]
}
# adjust for diamonds
diamonds <- sum(symbols == "DD")
prize * 2 ^ diamonds
# wrap code in function
score <- function(symbols){
## identify case
same <- symbols[1] == symbols[2] && symbols[2] == symbols[3]
bars <- symbols %in% c("B", "BB", "BBB")
## get prize
if (same) {
payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
"B" = 10, "C" = 10, "0" = 0)
prize <- unname(payouts[symbols[1]])
} else if (all(bars)) {
prize <- 5
} else {
cherries <- sum(symbols == "C")
prize <- c(0, 2, 5)[cherries + 1]
}
# adjust for diamonds
diamonds <- sum(symbols == "DD")
prize * 2 ^ diamonds
}
# now the play() function will work
play <- function(){
# step one: generate symbols
symbols <- get_symbols()
# step two: display the symbols
print(symbols)
# step three: score the symbols
score(symbols)
}
play()