diff --git a/Gemfile b/Gemfile index 05a14d1..cb4d5fb 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' -ruby '2.3.1' +ruby '2.5.1' gem 'rspec' gem 'rubocop' diff --git a/Gemfile.lock b/Gemfile.lock index 4533f5e..b0df5f6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,5 +37,8 @@ DEPENDENCIES rspec rubocop +RUBY VERSION + ruby 2.5.1p57 + BUNDLED WITH - 1.10.6 + 1.16.6 diff --git a/ball.rb b/ball.rb new file mode 100644 index 0000000..10f7cc8 --- /dev/null +++ b/ball.rb @@ -0,0 +1,42 @@ +require 'yaml' + +ANSWERS = YAML.load_file(File.join(__dir__, './answers.yml')) + +class String + def colorize(color_code) + "\e[#{color_code}m#{self}\e[0m" + end + + def red + colorize(31) + end + + def green + colorize(32) + end + + def yellow + colorize(33) + end + + def blue + colorize(34) + end +end + +class Ball + def shake + shake = rand(ANSWERS.size) + case shake + when 0..4 + puts ANSWERS[shake].red + when 5..9 + puts ANSWERS[shake].green + when 10..14 + puts ANSWERS[shake].yellow + else puts ANSWERS[shake].blue + end + end +end + +Ball.new.shake diff --git a/ball_version_1.rb b/ball_version_1.rb new file mode 100644 index 0000000..b990d7f --- /dev/null +++ b/ball_version_1.rb @@ -0,0 +1,56 @@ +class String + def colorize(color_code) + "\e[#{color_code}m#{self}\e[0m" + end + + def red + colorize(31) + end + + def green + colorize(32) + end + + def yellow + colorize(33) + end + + def blue + colorize(34) + end +end + +class Ball + def shake + red_answers = ["Don't count on it", + 'My reply is no', + 'My sources say no', + 'Outlook not so good', + 'Very doubtful'] + + green_answers = ['As I see it, yes', + 'Most likely', + 'Outlook good', + 'Signs point to yes'] + + yellow_answers = ['Reply hazy, try again', + 'Ask again later', + 'Better not tell you now', + 'Cannot predict now', + 'Concentrate and ask again'] + + blue_answers = ['It is certain', + 'It is decidedly so', + 'Without a doubt', + 'Yes - definitely', + 'You may rely on it'] + + shake = [red_answers[rand(red_answers.size)].red, + green_answers[rand(green_answers.size)].green, + yellow_answers[rand(yellow_answers.size)].yellow, + blue_answers[rand(blue_answers.size)].blue] + + puts shake[rand(shake.size)].to_s + end +end +Ball.new.shake diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index 5083084..120baab 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -5,10 +5,10 @@ let(:answers) { YAML.load_file(File.join(__dir__, '../answers.yml')) } it { expect(answers).to include(subject.shake) } - it { expect(answers).to eql(Ball::ANSWERS) } + it { expect(answers).to eql(ANSWERS) } describe '#shake' do - before { stub_const('Ball::ANSWERS', ['ANSWER']) } + before { stub_const('ANSWERS', ['ANSWER']) } it 'prints colorized answer' do expect(STDOUT).to receive(:puts).with("\e[31mANSWER\e[0m") diff --git a/tamagochi.rb b/tamagochi.rb new file mode 100644 index 0000000..5412cdd --- /dev/null +++ b/tamagochi.rb @@ -0,0 +1,121 @@ +class Pet + def initialize(name, age = 0, lifes = 1, mood = 3, hungry = 5, + sleep = 5, death = false) + @name = name.capitalize + @age = age + @lifes = lifes + @mood = mood + @hungry = hungry + @sleep = sleep + @death = death + + puts "Pet #{@name} has been created." + end + + def eat + eat = %w(apple banana meat potato fish honey milk nut) + puts "#{@name} has eaten #{eat[rand(eat.size)]}" + @hungry += 3 + @mood += 1 + plus_one_hour + end + + def sleep + puts "#{@name} has slept" + @sleep += 3 + @hungry -= 1 + plus_one_hour + end + + def play + puts "#{@name} has play" + @mood += 3 + @hungry -= 1 + plus_one_hour + end + + def age + puts "#{@name} has #{@age} year old." + end + + def mood + case @mood + when 0..2 + puts 'Not good' + when 3..5 + puts 'Good' + when 6..15 + puts 'Very very good' + end + end + + def status + puts 'At this moment your pet has next parameters:' + puts "Name: #{@name}" + puts "Age: #{@age}" + puts "Hungry: #{@hungry}" + puts "Mood: #{@mood}" + puts "Sleep: #{@sleep}" + puts "Death: #{@death}" + end + + def hungry? + puts @hungry <= 3 ? 'Pet wants to eat' : "Pet don't want to eat" + end + + def help + puts 'Select ' + puts " 'eat' if you want to feed your pet" + puts " 'sleep' if you want to put your pet in bed" + puts " 'play' if you want to play with your pet" + puts " 'age' if you want to see age of your pet" + puts " 'mood' if you want to see mood of your pet" + puts " 'hungry?' if you want to see hunger level of your pet" + puts " 'status' if you want to see current perameters" + end + + private + + def plus_one_hour + @hungry -= 1 + @mood -= 1 + @sleep -= 1 + + death if @hungry < 0 + end + + def death + @lifes -= 1 + @death = true + puts 'Your pet is dead. Sorry :( ' + exit + end +end + +puts 'Type name of your new pet' +pet = Pet.new(gets.chomp) + +loop do + command = gets.chomp + case command + when 'eat' + pet.eat + when 'sleep' + pet.sleep + when 'play' + pet.play + when 'mood' + pet.mood + when 'age' + pet.age + when 'hungry?' + pet.hungry? + when 'status' + pet.status + when 'help' + pet.help + when 'exit' + exit + else puts 'Something wrong.' + end +end