使用R编程的老虎机项目
老虎机是一个游戏,它包括两个步骤:从 7 个符号中随机生成三个符号,并对生成的三个符号计算分数。
算法:
- 老虎机中使用的符号是:
- 钻石
- 七
- 3个酒吧
- 2条
- 1巴
- 樱桃
- 零
- 我们可以使用任何符号。这可能取决于个人的选择。
- 从上述符号中,随机生成三个符号。
- 我们可以使用 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 个符号由 1 个 Cherry 组合生成,其余两个为空或 0,则奖金为 20。
- 如果三个符号都是零,奖金显然是 0。
- 如果我们得到七七钻石,钻石将被视为七。如果钻石赠送奖金将翻倍,奖金将是800 * 2 = 1600。
- 如果我们得到七钻-钻石,这里钻石出现了两次,所以奖金将翻倍。奖金为(800*2) 2 。
- 如果钻石与任何其它的字符出现,那么它被称为字符。就像我们在卡片中称小丑一样。
- 如果我们得到所有钻石,奖金将翻倍,将被视为“累积奖金”。
- 符号和奖金可能不同,但概念是相同的。
- 让我们实现无字符老虎机项目。
程序1:此代码将替换和概率作为参数
R
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()
R
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()
输出:
测试用例 !:
测试案例 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: