R语言 老虎机项目
老虎机是一个游戏,它包括两个步骤。从7个符号中随机生成3个符号;对于生成的3个符号,计算分数。
算法
- 老虎机中使用的符号是。
- Diamond
- Seven
- 3Bars
- 2Bars
- 1Bar
- cherry
- Zero
- 我们可以采取任何符号。这可能取决于个人的选择。
- 从上面的符号中,随机产生三个符号。
- 我们可以使用R编程中的sample()函数获得随机符号。
- 根据这些符号,会有一些奖金。
- 奖金分配给生成的符号也取决于个人。
So, for easy understanding let us consider the below amount
of money as prize for this project.
Diamond Diamond Diamond -------------------> 1000
Seven Seven Seven --------------------> 800
3Bars 3Bars 3Bars ------------------> 600
2Bars 2Bars 2Bars --------------------> 400
1Bar 1Bar 1Bar ---------------------> 250
Cherry Cherry Cherry ----------------------> 100
If three symbols are generated with the combination of all
types of bars prize money will be 50 .
If three symbols are generated with the combination of two
Cs and remaining one empty or 0 prize money will be 50.
0 Cherry Cherry------------> 50
Cherry 0 Cherry ------------> 50
Cherry Cherry 0 -------------> 50
- 如果3个符号中产生一个樱桃的组合,其余两个是空的或0,奖金将是20。
- 如果这三个符号是零,奖金显然是0。
- 如果我们得到7-7-钻石,钻石将被认为是7。如果钻石出现,奖金将增加一倍,因此,奖金将是800 * 2 = 1600。
- 如果我们得到七-钻石-钻石,这里钻石出现了两次,所以奖金将翻倍。奖金将是(800*2)2。
- 如果 “钻石 “与任何其他字符一起出现,那么它就被称为野牌字符。就像我们所说的扑克牌中的小丑。
- 如果我们得到了所有的钻石,奖金将翻倍,这将被视为 “大奖”。
- 符号和奖金可能不同,但概念是相同的。
- 让我们实现一个没有通配符的老虎机项目。
程序1: 这段代码将采用替换和概率作为参数
gsy <- function() {
s <- c("Diamond","Seven","3Bars","2Bars","1Bar","Cherry","Zero")
sample(s,size=3,replace=TRUE,prob=c(0.02,0.04,0.05,0.12,0.15,0.04,0.5))
}
gs<- function(s) {
# checking for same symbols
s1<- s[1] == s[2] && s[2] == s[3]
b <- s %in% c("1Bar","2Bars","3bars")
if (s1) {
cash <- c("Diamond"=1000,"7"=800,"3Bars"=600,"2Bars"=400,
"1Bar"=250,"Cherry"=100,"Zero"=0)
cash_p <- unname(cash[s[1]])
}
# checking for all bars
else if (all(b)) {
cash_p <- 5
}
# checking for cherries
else {
cherry1 <- sum(s == "Cherry")
cash_p <- c(0,2,5)[cherry1+1]
}
# checking for diamonds
d1 <- sum(s == "Diamond")
cash_p * 2 ^ d1
}
# run function for calling gsy function
run <- function() {
s <- gsy()
print(s)
gs(s)
}
run()
输出
测试用例!
测试用例2 :
测试用例3
程序2: 在这段代码中,我们没有传递概率参数。
gsy <- function() {
s <- c("Diamond","Seven","3Bars","2Bars","1Bar","Cherry","Zero")
sample(s,size=3,replace=TRUE)
}
gs<- function(s) {
# checking for same symbols
s1<- s[1] == s[2] && s[2] == s[3]
b <- s %in% c("1Bar","2Bars","3bars")
if (s1) {
cash <- c("Diamond"=1000,"7"=800,"3Bars"=600,"2Bars"=400,
"1Bar"=250,"Cherry"=100,"Zero"=0)
cash_p <- unname(cash[s[1]])
}
# checking for all bars
else if (all(b)) {
cash_p <- 5
} else {
# checking for cherries
cherry1 <- sum(s == "Cherry")
cash_p <- c(0,2,5)[cherry1+1]
}
# checking for diamonds
d1 <- sum(s == "Diamond")
cash_p * 2 ^ d1
}
run <- function() {
s <- gsy()
print(s)
gs(s)
}
run()
输出
测试用例1
测试用例 2 :
测试用例 3 :
极客教程