From 45d2ddc904edb85486780a9379b7b811dc000982 Mon Sep 17 00:00:00 2001 From: arutinn <7arutin@gmail.com> Date: Wed, 24 Oct 2018 21:17:51 +0300 Subject: [PATCH 1/4] add magic 8 ball (first version, rspec false) --- ball_version_1.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 ball_version_1.rb diff --git a/ball_version_1.rb b/ball_version_1.rb new file mode 100644 index 0000000..c2cafdc --- /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(5)].red, + green_answers[rand(4)].green, + yellow_answers[rand(5)].yellow, + blue_answers[rand(5)].blue] + + puts shake[rand(4)].to_s + end +end +Ball.new.shake From 0bcf1bf38169dfa014ab3887f5b05907f336f36b Mon Sep 17 00:00:00 2001 From: arutinn <7arutin@gmail.com> Date: Wed, 24 Oct 2018 21:47:25 +0300 Subject: [PATCH 2/4] add pet game --- Gemfile | 2 +- Gemfile.lock | 5 ++- tamagochi.rb | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tamagochi.rb 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/tamagochi.rb b/tamagochi.rb new file mode 100644 index 0000000..cb1e4d4 --- /dev/null +++ b/tamagochi.rb @@ -0,0 +1,120 @@ +class Pet + def initialize(name, age = 0, lifes = 1, mood = 3, hungry = 5, + sleep = 5, death = false) + @name = name + @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.length)]}" + @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 + +pet = Pet.new('Bobik') + +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 From 1b5355e9e5cceee6934948670e93d559ae9a906a Mon Sep 17 00:00:00 2001 From: arutinn <7arutin@gmail.com> Date: Thu, 25 Oct 2018 21:56:58 +0300 Subject: [PATCH 3/4] small refactoring --- ball_version_1.rb | 10 +++++----- tamagochi.rb | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ball_version_1.rb b/ball_version_1.rb index c2cafdc..b990d7f 100644 --- a/ball_version_1.rb +++ b/ball_version_1.rb @@ -45,12 +45,12 @@ def shake 'Yes - definitely', 'You may rely on it'] - shake = [red_answers[rand(5)].red, - green_answers[rand(4)].green, - yellow_answers[rand(5)].yellow, - blue_answers[rand(5)].blue] + 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(4)].to_s + puts shake[rand(shake.size)].to_s end end Ball.new.shake diff --git a/tamagochi.rb b/tamagochi.rb index cb1e4d4..5412cdd 100644 --- a/tamagochi.rb +++ b/tamagochi.rb @@ -1,7 +1,7 @@ class Pet def initialize(name, age = 0, lifes = 1, mood = 3, hungry = 5, sleep = 5, death = false) - @name = name + @name = name.capitalize @age = age @lifes = lifes @mood = mood @@ -14,7 +14,7 @@ def initialize(name, age = 0, lifes = 1, mood = 3, hungry = 5, def eat eat = %w(apple banana meat potato fish honey milk nut) - puts "#{@name} has eaten #{eat[rand(eat.length)]}" + puts "#{@name} has eaten #{eat[rand(eat.size)]}" @hungry += 3 @mood += 1 plus_one_hour @@ -92,7 +92,8 @@ def death end end -pet = Pet.new('Bobik') +puts 'Type name of your new pet' +pet = Pet.new(gets.chomp) loop do command = gets.chomp From ead9ef824b6bccf601df2cea4d0e11ccfda289eb Mon Sep 17 00:00:00 2001 From: arutinn <7arutin@gmail.com> Date: Sun, 28 Oct 2018 18:49:29 +0200 Subject: [PATCH 4/4] add new ball.rb --- ball.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ spec/ball_spec.rb | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 ball.rb 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/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")