Sinisterly
[Ruby] Lottery [Updated] - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: [Ruby] Lottery [Updated] (/Thread-Ruby-Lottery-Updated)



[Ruby] Lottery [Updated] - Inori - 03-29-2015

Back again.

I threw this together when I started learning ruby and thought I'd improve it.

May update in the future to make it smaller/more compact as well as more/fewer numbers for an actually winnable lottery and maybe a raffle, while I'm at it.

Enjoy.

Code:
#rLottery by Nightc||ed, ©2014 range = (01..50) puts "[1] computer selected numbers" puts "[2] input your own" nums = gets.chomp if nums == "2" puts "enter first number (1-50)" puts "current numbers:" a1 = gets.to_i system "cls" puts "enter second number (1-50)" puts "current numbers: #{a1}" a2 = gets.to_i system "cls" puts "enter third number (1-50)" puts "current numbers: #{a1}, #{a2}" a3 = gets.to_i system "cls" puts "enter fourth number (1-50)" puts "current numbers: #{a1}, #{a2}, #{a3}" a4 = gets.to_i system "cls" puts "enter fifth number (1-50)" puts "current numbers: #{a1}, #{a2}, #{a3}, #{a4}" a5 = gets.to_i system "cls" puts "enter sixth number (1-50)" puts "current numbers: #{a1}, #{a2}, #{a3}, #{a4}, #{a5}" a6 = gets.to_i elsif nums == "1" a1 = range.to_a.sample a2 = range.to_a.sample a3 = range.to_a.sample a4 = range.to_a.sample a5 = range.to_a.sample a6 = range.to_a.sample else puts "invalid choice" sleep end system "cls" b1 = range.to_a.sample b2 = range.to_a.sample b3 = range.to_a.sample b4 = range.to_a.sample b5 = range.to_a.sample b6 = range.to_a.sample arrb = [b1, b2, b3, b4, b5, b6] arra = [a1, a2, a3, a4, a5, a6] arrb.sort arra.sort puts "current numbers: #{a1}, #{a2}, #{a3}, #{a4}, #{a5}, #{a6}" puts "Press enter to continue" cont = gets.chomp system "cls" puts "Your Numbers: #{a1}, #{a2}, #{a3}, #{a4}, #{a5}, #{a6}" puts "Winnnig Numbers: #{b1}, #{b2}, #{b3}, #{b4}, #{b5}, #{b6}" puts "odds against: 15,625,000,000:1" #I'm a programmer, not a statistician. This is probably wrong. if arra == arrb puts "Congratulations! You win!" else puts "Sorry :( please play again." end kill = gets.chomp exit



Update:
- Added more modes
- Compacted lottery function
Code:
#rLottery by Nightc||ed, ©2014 puts """ what game would you like to play? [1] Lottery [2] Raffle [3] 50/50 [4] ---- """ game = gets.chomp system "cls" case game when "1" def conf puts """ -----------Lottery config---------- """ end conf puts "enter number of slots:" numc = gets.to_i system "cls" conf puts "number of slots: #{numc}" puts "enter range of numbers:" rng = gets.to_i system "cls" rng = *(1..rng) draw = [] numc.times do draw << rng.sample end puts """ -=-=-=-=-=-=Lottery=-=-=-=-=-=-= the winning numbers are: #{draw} -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=""" sleep when "2" puts """ -----------Raffle config---------- Enter number of tickets: """ tickets = gets.to_i tickets = *(1..tickets) winner = tickets.sample system "cls" puts """ -=-=-=-=-=-=-Raffle=-=-=-=-=-=-= the winning ticket is: #{winner}! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=""" sleep when "3" puts """ -----------50/50 config---------- Enter number of tickets: """ tickets = gets.to_i system "cls" puts """ -----------50/50 config---------- Enter price per ticket ($$.¢¢; ex. 01.50): """ price = gets.to_i sum = tickets * price tickets = *(1..tickets) wticket = tickets.sample system "cls" puts """ -=-=-=-=-=-=-50/50-=-=-=-=-=-=-= ticket number #{wticket} won $#{sum}! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=""" sleep when "4" puts "Under construction" sleep end