Open In App

Slot machine project using R programming

The slot machine is a game, and it comprises two steps: Generating three symbols randomly from 7 symbols and For the generated three symbols, computing score.

Algorithm:

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

Program1: This code will take replace and probabilities as parameters






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()

Output:

Testcase !:



Test Case 2:

Test case 3:

Program 2: In this code, we are not passing probabilities parameters. 




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()

Output:

 Test case 1:

Test case 2:

Test case 3:


Article Tags :