Skip to content

Instantly share code, notes, and snippets.

@willluongo
Created November 27, 2010 23:27
Show Gist options
  • Select an option

  • Save willluongo/718383 to your computer and use it in GitHub Desktop.

Select an option

Save willluongo/718383 to your computer and use it in GitHub Desktop.

Revisions

  1. willluongo revised this gist Nov 27, 2010. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions GuessingGame.rb
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@ def initialize
    @lower_limit=1
    @guess=50
    @quit = false
    puts self.comp_guess
    until self.quit_playing
    self.comp_guess
    self.input = gets.chomp.to_i
    end
    end
    @@ -35,15 +35,15 @@ def input=(player_input)
    end

    @guess = self.guess_number
    puts self.comp_guess
    end

    def win
    @quit = true
    puts "\n\nYou win!!\nThanks for playing!\n\n"
    end

    def comp_guess
    @quit == true ? "You win!!\nThanks for playing!\n\n" : "Please select one of the following:\n(1) Too Low\n(2) Too High\n(3) That's it!\n\nI guess #{@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
  2. willluongo created this gist Nov 27, 2010.
    54 changes: 54 additions & 0 deletions GuessingGame.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    class GuessingGame
    def initialize
    @upper_limit=100
    @lower_limit=1
    @guess=50
    @quit = false
    puts self.comp_guess
    until self.quit_playing
    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
    puts self.comp_guess
    end

    def win
    @quit = true
    end

    def comp_guess
    @quit == true ? "You win!!\nThanks for playing!\n\n" : "Please 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