Created
November 27, 2010 23:27
-
-
Save willluongo/718383 to your computer and use it in GitHub Desktop.
Guessing Game implemented in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class GuessingGame | |
| def initialize | |
| @upper_limit=100 | |
| @lower_limit=1 | |
| @guess=50 | |
| @quit = false | |
| until self.quit_playing | |
| self.comp_guess | |
| self.input = gets.chomp.to_i | |
| end | |
| end | |
| def guess_number | |
| ((@upper_limit + @lower_limit)/2) | |
| end | |
| def too_low | |
| @lower_limit = @guess | |
| end | |
| def too_high | |
| @upper_limit = @guess | |
| end | |
| def input=(player_input) | |
| case player_input | |
| when 1 | |
| self.too_low | |
| when 2 | |
| self.too_high | |
| when 3 | |
| self.win | |
| else | |
| puts "Please pick option 1, 2, or 3." | |
| end | |
| @guess = self.guess_number | |
| end | |
| def win | |
| @quit = true | |
| puts "\n\nYou win!!\nThanks for playing!\n\n" | |
| end | |
| def comp_guess | |
| puts "\n\nPlease select one of the following:\n(1) Too Low\n(2) Too High\n(3) That's it!\n\nI guess #{@guess}" | |
| end | |
| def quit_playing | |
| @quit | |
| end | |
| end | |
| This_Game = GuessingGame.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment