Created
April 20, 2017 15:53
-
-
Save HakubJozak/fc69adf26179511c55f133c11ce4864f to your computer and use it in GitHub Desktop.
animals
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
| # Vytvorte tridu Dog. | |
| # Bude mit jednu instancni promennou @energy a dve metody. | |
| # | |
| # #eat! - zvysi energie o 10 | |
| # #walk! - snizi hodnotu energie o 1 | |
| # #bark! - snizi hodnotu energie o 5 | |
| # | |
| # alive? - true, kdyz energie je vetsi nez 0 | |
| class Dog | |
| def initialize(name) | |
| @name = name | |
| @energy = 10 | |
| end | |
| def bark! | |
| puts 'HAF!' | |
| @energy -= 5 | |
| end | |
| def walk! | |
| @energy -= 1 | |
| end | |
| def alive? | |
| @energy > 0 | |
| end | |
| def dead? | |
| !alive? | |
| end | |
| def eat!(food = :granule) | |
| case food | |
| when :ham | |
| @energy += 10 | |
| when :granule | |
| @energy += 0.3 | |
| when :sausage | |
| @energy += 4 | |
| when :soya | |
| @energy -= 1 | |
| else | |
| bark! | |
| end | |
| @energy = @energy.round(2) | |
| end | |
| def energy | |
| @energy | |
| end | |
| end | |
| dog = Dog.new('Azor') | |
| loop do | |
| puts "---------------------" | |
| puts " ENERGY: #{dog.energy}" | |
| puts "--------------------" | |
| print "Enter dog command: " | |
| case gets.chomp | |
| when 'e' | |
| puts 'What food do you want to feed your dog with?' | |
| puts "[h]am, ..." | |
| case gets.chomp | |
| when 'h' then dog.eat!(:ham) | |
| when 's' then dog.eat!(:soya) | |
| else | |
| dog.eat! | |
| end | |
| when 'w' then dog.walk! | |
| when 'b' then dog.bark! | |
| else | |
| dog.bark! | |
| end | |
| if dog.dead? | |
| puts 'RIP' | |
| exit | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment