|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Trap |
| 4 | +# Steve Ullman, 1972-08-01 |
| 5 | +# Ruby version Glenn Vanderburg, 2022-03-04 |
| 6 | + |
| 7 | +# Change these values to make the game easier or harder. |
| 8 | +GUESSES_PER_GAME = 6 |
| 9 | +NUMBER_UPPER_BOUND = 100 |
| 10 | + |
| 11 | +# Put everything in methods for order of presentation; we |
| 12 | +# want to be able to refer to methods before declaring them, |
| 13 | +# so the code reads nicely from top to bottom. |
| 14 | +def main |
| 15 | + print_banner_and_instructions |
| 16 | + |
| 17 | + loop do |
| 18 | + play_a_game |
| 19 | + break unless yes?("Try again?") |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +def yes?(prompt) |
| 24 | + print "\n#{prompt} " |
| 25 | + answer = gets |
| 26 | + return answer.downcase.start_with?("y") |
| 27 | +end |
| 28 | + |
| 29 | +def print_banner_and_instructions |
| 30 | + banner = "Creative Computing -- Morristown, New Jersey" |
| 31 | + |
| 32 | + puts "Trap!".center(banner.size) |
| 33 | + puts banner |
| 34 | + 2.times { puts } |
| 35 | + |
| 36 | + return unless yes?("Instructions?") |
| 37 | + |
| 38 | + puts <<~"END" |
| 39 | + I am thinking of a number between 1 and #{NUMBER_UPPER_BOUND}. |
| 40 | + Try to guess my number. On each guess, |
| 41 | + you are to enter 2 numbers, trying to trap |
| 42 | + my number between the two numbers. I will |
| 43 | + tell you if you have trapped my number, if my |
| 44 | + number is larger than your two numbers, or if |
| 45 | + my number is smaller than your two numbers. |
| 46 | + If you want to guess one single number, type |
| 47 | + your guess for both your trap numbers. |
| 48 | + You get #{GUESSES_PER_GAME} guesses to get my number. |
| 49 | + END |
| 50 | +end |
| 51 | + |
| 52 | +def play_a_game |
| 53 | + n = choose_number |
| 54 | + |
| 55 | + GUESSES_PER_GAME.times do |i| |
| 56 | + lower, upper = get_guesses("Guess ##{i+1}?") |
| 57 | + |
| 58 | + case |
| 59 | + when n < lower |
| 60 | + puts "My number is smaller than your trap numbers." |
| 61 | + when n > upper |
| 62 | + puts "My number is larger than your trap numbers." |
| 63 | + when lower != upper |
| 64 | + puts "You have trapped my number." |
| 65 | + else |
| 66 | + puts "You got it!!!" |
| 67 | + return |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + puts "Sorry, that's #{GUESSES_PER_GAME} guesses. Number was #{n}" |
| 72 | +end |
| 73 | + |
| 74 | +def choose_number |
| 75 | + rand(NUMBER_UPPER_BOUND) + 1 |
| 76 | +end |
| 77 | + |
| 78 | +def get_guesses(prompt) |
| 79 | + loop do |
| 80 | + print "\n#{prompt} " |
| 81 | + |
| 82 | + # This is forgiving of input format; it ignores spaces and |
| 83 | + # punctuation, returning only the strings of consecutive |
| 84 | + # digits in the input line. |
| 85 | + guesses = gets.scan(/\d+/) |
| 86 | + |
| 87 | + if guesses.size != 2 |
| 88 | + puts "Please enter two numbers for each guess." |
| 89 | + else |
| 90 | + # convert the strings of digits to integers: |
| 91 | + numbers = guesses.map(&:to_i) |
| 92 | + # and return them, lowest number first: |
| 93 | + return numbers.sort |
| 94 | + end |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +main |
0 commit comments