Ruby Programming Tutorial - Lesson 20 - Creating a Betting bot

in #ruby7 years ago (edited)

rub2.jpg

We are creating a ruby betting program

You can take this code, extend it to work with steemit platform ..
but we are just learning to program. it will not be actual betting bot .. but rather just a fun programming example.

Create a betting bot, that takes a betting amount and toss a coin probably
if person wins give him 1.8x amount back.
e.g person bets 1 SBD and wins give him 1.8 SBD back.

We will start by creating a Class Called Betting

Create a folder called Bettingbot and create a file bettingbot.rb inside the folder

class Betting
    def initialize(*properties)
        @bettingAmount = properties[0]
        @bettingPerson = properties[1]
        @bettingMemo   = properties[2]
    end

    def betting_result 
        ## Generate a random number between 1 and 10,000
        _random = Random.new.rand(1..10000)
        
        ## Check if random number is bigger than 5000 and Betting was high 
        ## Declare him a winner
        if _random >= 5000 && @bettingMemo == "high"
            $betting_Amount = @bettingAmount.to_f * 1.8
            puts $betting_Amount
            return "WIN", _random, $betting_Amount
        elsif _random <= 4999 && @bettingMemo == "low"
            $betting_Amount = @bettingAmount.to_f * 1.8
            puts $betting_Amount
            return "WIN", _random, $betting_Amount
        elsif @bettingMemo != "high" || @bettingMemo != "low"
            return "INVALID bid, Choose 'high' or 'low' "
        else 
            return "LOSE", _random
        end
    end
end

isn't it a beautiful program ?

Screenshot from 2018-03-13 20-16-13.png

We created an initialization method which takes variable length of arguments.
in our case we will pass three arguments to it.

  • Betting Amount e.g 0.5 SBD
  • ID of person who is betting e.g @bilal-haider
  • Memo of Betting e.g high or low
    def initialize(*properties)
        @bettingAmount = properties[0]
        @bettingPerson = properties[1]
        @bettingMemo   = properties[2]
    end

once we receive a bet, we can perform arithmetic calculations and decide whether the person who bet, won or he lost or he placed an invalid bid.

We are using random number generator method, which ruby provides us. We will generate a random number between 1 and 10,000.
if number is higher than 5000 , and user bids high .. user wins..
if number is lower than 4999, and user bids low .. user wins
otherwise user loses..

 _random = Random.new.rand(1..10000)

Storing the resulting number in our local variable _random ..

Following are couple of conditional statements, logical statements and arithmetic statements
which perform our task..

## Conditional statement, which check the random number and bid value .. both of these conditions
## must be true in order for the user to Win ..  e.g random number is bigger than 5000 and also user bid ## was 'high'
        if _random >= 5000 && @bettingMemo == "high"
            ## multiplying user's bid by 1.8 
            $winning_price = @bettingAmount.to_f * 1.8 

            ## returning, Win, random number, and winning amount
            return "WIN", _random, $winning_price           
        elsif _random <= 4999 && @bettingMemo == "low"
            ## multiplying user's bid by 1.8 
            $winning_price = @bettingAmount.to_f * 1.8

            ## returning, Win, random number, and winning amount
            return "WIN", _random, $winning_price

        elsif @bettingMemo != "high" || @bettingMemo != "low" ## handling invalid bids, 
            return "INVALID bid, Choose 'high' or 'low' "
        else 
            return "LOSE", _random
        end

after that our class is ready, we can use it now

We created a new file called "main.rb" and written following code.

require_relative 'bettingbot'

puts "Enter your betting Amount: "
$betting_amount = gets.chomp

## converting input text value to a floating point number that has 2 numbers after decimal point
## e.g 45.235234 will be converted into 45.24

$betting_amount = '%.2f' % $betting_amount.to_f


puts "Enter username: "
$betting_person = gets.chomp

puts "Enter 'high' or 'low': "
$betting_value  = gets.chomp 

betting_bot = Betting.new($betting_amount, $betting_person, $betting_value)
_result = betting_bot.betting_result();
print "#{_result} : " 

if _result[0] == "WIN"
    puts "Congratulations: Your number was #{_result[1]} "
    puts "You won #{_result[2]} SBD "
elsif _result[0] == "LOSE"
    puts "Unfortunately  : Your number was #{_result[1]} "
    puts "Try again"
else 
    puts "Wierd: Error"
end

Screenshot from 2018-03-13 20-15-50.png

First line of the program

require_relative 'bettingbot'
You can create a class, in a seperate file. and include the file where you need it..
Just like we did, We created Betting Class in a seperate file. and included in main.rb

puts "Enter your betting Amount: "
$betting_amount = gets.chomp

## converting input text value to a floating point number that has 2 numbers after decimal point
## e.g 45.235234 will be converted into 45.24
$betting_amount = '%.2f' % $betting_amount.to_f

Asking user to input his betting amount. e.g 0.1
following lines of code ask user to insert his username and his bid "high" or "low"

puts "Enter username: "
$betting_person = gets.chomp

puts "Enter 'high' or 'low': "
$betting_value  = gets.chomp 

Once we get the inputs from the user, we can then use them to create a "Betting" object.
we can then call our method to get the "betting result"
which we are storing in a local variable "_result"

betting_bot = Betting.new($betting_amount, $betting_person, $betting_value)
_result = betting_bot.betting_result();
print "#{_result} : " 

remember that "betting_result" is a method of our Betting class, which performs calculations
and send us result ... the result is an array .. which contains three values in case of winning,
and two values in case of losing .

  • "WIN"
  • _random .. which is the random number which we generated
  • and winning amount .. which is 1.8 Times the betting amount ..

finally We check the result variable .. if the result is a WIN or LOSE
we display message to user accordingly ...

if _result[0] == "WIN"
    puts "Congratulations: Your number was #{_result[1]} "
    puts "You won #{_result[2]} SBD "
elsif _result[0] == "LOSE"
    puts "Unfortunately  : Your number was #{_result[1]} "
    puts "Try again"
else 
    puts "Wierd: Error"
end

Congratulations on creating a Betting bot

Its time for you to think about an idea, which you want to convert into a ruby program.
You can think about something, which people would love to use.
and convert that idea into a ruby program.

When you create something share with me :)

join me on Discord
https://discord.gg/UhQCYeb

Sort:  

Great tutorial. You got an upvote from @nextvote.

Thank you :) bro

Great tutorial You got an upvote from @murderface

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.029
BTC 61497.04
ETH 2478.29
USDT 1.00
SBD 2.66