|
6 | 6 | max = 100
|
7 | 7 | min = 1
|
8 | 8 | remaining_guesses = 6
|
| 9 | +first_guess = rand(100) + 1 |
9 | 10 |
|
10 | 11 | while remaining_guesses > 1
|
11 | 12 | remaining_guesses -= 1
|
12 |
| - guess = (min + max) / 2 |
13 |
| - puts "I will guess the number #{guess}." |
14 |
| - puts "Did I get the right number? Or should my next guess bigger or smaller?" |
15 |
| - |
16 |
| - # If the guess is too big, the user should type smaller - or bigger, if it is too small |
17 |
| - user_response = gets.chomp |
18 |
| - |
19 |
| - # The computer uses the response to guess its next number |
20 |
| - if user_response == "smaller" |
21 |
| - puts "The number you are looking for is smaller than your guess." |
22 |
| - max = guess |
23 |
| - elsif user_response == "bigger" |
24 |
| - puts "The number you are looking for is bigger than your guess." |
25 |
| - min = guess |
| 13 | + guess = first_guess |
| 14 | + |
| 15 | + if remaining_guesses == 5 |
| 16 | + puts "I will guess the number #{first_guess}." |
| 17 | + |
| 18 | + puts "Did I get the right number? Or should my next guess bigger or smaller?" |
| 19 | + |
| 20 | + # If the guess is too big, the user should type smaller - or bigger, if it is too small |
| 21 | + user_response = gets.chomp |
| 22 | + |
| 23 | + # The computer uses the response to guess its next number |
| 24 | + if user_response == "smaller" |
| 25 | + puts "The number you are looking for is smaller than your guess." |
| 26 | + max = guess |
| 27 | + elsif user_response == "bigger" |
| 28 | + puts "The number you are looking for is bigger than your guess." |
| 29 | + min = guess |
| 30 | + else |
| 31 | + puts "Whew, I got it on the first try!" |
| 32 | + exit 0 |
| 33 | + end |
| 34 | + |
26 | 35 | else
|
27 |
| - puts "Bwahahahahaa, I win!" |
28 |
| - exit 0 |
| 36 | + guess = (min + max) / 2 |
| 37 | + puts "I will guess the number #{guess}." |
| 38 | + puts "Did I get the right number? Or should my next guess bigger or smaller?" |
| 39 | + |
| 40 | + # If the guess is too big, the user should type smaller - or bigger, if it is too small |
| 41 | + user_response = gets.chomp |
| 42 | + |
| 43 | + # The computer uses the response to guess its next number |
| 44 | + if user_response == "smaller" |
| 45 | + puts "The number you are looking for is smaller than your guess." |
| 46 | + max = guess |
| 47 | + elsif user_response == "bigger" |
| 48 | + puts "The number you are looking for is bigger than your guess." |
| 49 | + min = guess |
| 50 | + else |
| 51 | + puts "Bwahahahahaa, I win!" |
| 52 | + exit 0 |
| 53 | + end |
29 | 54 | end
|
30 | 55 | end
|
31 | 56 |
|
|
0 commit comments