Merge pull request #586 from rtlechow/47-hi-lo-ruby

47_Hi-Lo Ruby
This commit is contained in:
Jeff Atwood
2022-02-24 08:35:51 -06:00
committed by GitHub
2 changed files with 60 additions and 1 deletions

View File

@@ -1,3 +1,3 @@
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
Conversion to [Ruby](https://www.ruby-lang.org/en/)
Conversion to [Ruby](https://www.ruby-lang.org/en/) by [R.T. Lechow](https://github.com/rtlechow)

59
47_Hi-Lo/ruby/hi_lo.rb Normal file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/env ruby
MAX_TRIES = 6
RANGE = (1..100)
def intro
puts <<~END_OF_INTRO
#{'HI LO'.center(74)}
#{"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n".center(76)}
THIS IS THE GAME OF HI LO.\n
YOU WILL HAVE #{MAX_TRIES} TRIES TO GUESS THE AMOUNT OF MONEY IN THE
HI LO JACKPOT, WHICH IS BETWEEN #{RANGE.min} AND #{RANGE.max} DOLLARS. IF YOU
GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!
THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,
IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.\n\n
END_OF_INTRO
end
def make_guess
puts 'YOUR GUESS?'
@guess = gets.to_i
end
def check_guess
if @guess == @number
@guessed_correctly = true
@total_winnings += @number
puts <<~END_OF_WIN_TEXT
GOT IT!!!!!!!!!! YOU WIN #{@number} DOLLARS.
YOUR TOTAL WINNINGS ARE NOW #{@total_winnings} DOLLARS.
END_OF_WIN_TEXT
else
puts "YOUR GUESS IS TOO #{@guess > @number ? 'HIGH' : 'LOW'}.\n\n"
end
end
def blew_it
@total_winnings = 0
puts "YOU BLEW IT...TOO BAD...THE NUMBER WAS #{@number}"
end
def outro
puts "\nSO LONG. HOPE YOU ENJOYED YOURSELF!!!"
end
intro
@total_winnings = 0
loop do
@guessed_correctly = false
@number = rand(RANGE)
MAX_TRIES.times do
make_guess
check_guess
break if @guessed_correctly
end
blew_it unless @guessed_correctly
puts "\nPLAY AGAIN (YES OR NO)?"
break if gets.start_with?(/n/i)
end
outro