Books / Ruby for Beginners / Chapter 17

Code Example - Slot Machine in Ruby

We’ll practice a bit and sum up everything we know so far, and implement simple one-armed bandit game. We will put some money in the bank, pull the handle and see what happens.

Let’s outline our plan. Variable balance will represent the amount of money we have in the bank. We’ll have three reels (positions, or slots) and few images. Traditionally, slot machines have fruit images, cherry, bell, and number “7”. To simplify our task we’ll be just using numbers from 0 to 5 instead of mentioned images - we can always come back to it and make improvements later.

Variables x, y, z will represent the reels. Each reel can hold only one value (from 0 to 5) at a time. This value will be defined by random number generator.

Next step is to define what “win” and “lose” means: when all three variables x, y, z are equal, we’ll be enforcing following rules:

  • If all numbers are zeroes, we lose everything
  • If all variables are ones, we add $10 to our balance
  • If all variables are twos, we add $20 to our balance
  • Otherwise we charge 50 cents for attempt

Program should work until we have money on our balance. Elementary age check will be helpful:

print "What's your age: "
age = gets.to_i
if age < 18
  puts 'Sorry, but you should be at least 18 to play'
  exit
end

balance variable keeps the balance (initialized with $20), also define the infinite loop:

balance = 20
loop do
  # ..
end

Wait for Enter key inside of the loop:

  puts 'Press Enter to pull the handle...'
  gets

Initialize variables x, y, z:

  x = rand(0..5)
  y = rand(0..5)
  z = rand(0..5)

Print result of a single draw:

  puts "Result: #{x} #{y} #{z}"

Check first condition “If all numbers are zeroes, we lose everything”:

  if x == 0 && y == 0 && z == 0
    balance = 0
    puts 'You lost your money'
  end 

Check second condition “If all variables are ones, we add $10 to our balance”:

  elsif x == 1 && y == 1 && z == 1
    balance += 10
    puts 'You won $10'
  end

Add third condition “If all variables are twos, we add $20 to our balance” and also the last case “Otherwise we charge 50 cents for attempt”. Here is the complete code for all conditions:

  if x == 0 && y == 0 && z == 0
    balance = 0
    puts 'You lost your money'
  elsif x == 1 && y == 1 && z == 1
    balance += 10
    puts 'You won $10'
  elsif x == 2 && y == 2 && z == 2
    balance += 20
    puts 'You won $20'
  else
    balance -= 0.5
    puts 'You lost 50 cents'
  end

At the end show the final result:

  puts "Your balance is #{balance} dollars"  

Complete program:

print "What's your age: "
age = gets.to_i
if age < 18
  puts 'Sorry, but you should be at least 18 to play'
  exit
end

balance = 20
loop do
  puts 'Press Enter to pull the handle...'
  gets
  
  x = rand(0..5)
  y = rand(0..5)
  z = rand(0..5)
  
  puts "Result: #{x} #{y} #{z}"
  
  if x == 0 && y == 0 && z == 0
    balance = 0
    puts 'You lost your money'
  elsif x == 1 && y == 1 && z == 1
    balance += 10
    puts 'You won $10'
  elsif x == 2 && y == 2 && z == 2
    balance += 20
    puts 'You won $20'
  else
    balance -= 0.5
    puts 'You lost 50 cents'
  end

  puts "Your balance is #{balance} dollars"  
end

Result:

What's your age: 20
Press Enter to pull the handle...

Result: 5 5 3
You lost 50 cents
Your balance is 19.5 dollars
Press Enter to pull the handle...

...

Result: 1 1 1
You won $10
Your balance is 29.5 dollars
Press Enter to pull the handle...

You must agree that there is nothing complex here and program works. With knowledge we gained in last chapters we can build simple text games, do some basic calculations, make other useful applications. Ruby programs are small, elegant, and very easy to read and understand. And this is exactly what makes programming fun!

In next chapters we’ll cover essential data structures, will find out what classes and objects are, will get familiar with some tools, we’ll have foundation for absolutely amazing things you can do with Ruby programming!

Exercise 1 Define method that will return random number. Make sure program works with this method. After that apply animation to variables x, y, and z.

Exercise 2 Add more conditions to the game, like 333, 444, and so on. Use your imagination.


Licenses and Attributions


Speak Your Mind

-->