diff --git a/Gemfile b/Gemfile index 05a14d1..f469884 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ ruby '2.3.1' gem 'rspec' gem 'rubocop' +gem 'colorize' diff --git a/Gemfile.lock b/Gemfile.lock index 4533f5e..fba409e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ GEM ast (2.1.0) astrolabe (1.3.1) parser (~> 2.2) + colorize (0.8.1) diff-lcs (1.2.5) parser (2.2.3.0) ast (>= 1.1, < 3.0) @@ -34,8 +35,12 @@ PLATFORMS ruby DEPENDENCIES + colorize rspec rubocop +RUBY VERSION + ruby 2.3.1p112 + BUNDLED WITH - 1.10.6 + 1.16.6 diff --git a/bin/magic_8_ball.rb b/bin/magic_8_ball.rb new file mode 100644 index 0000000..ce40a15 --- /dev/null +++ b/bin/magic_8_ball.rb @@ -0,0 +1,5 @@ +require 'colorize' +require_relative '../lib/ball' + +ball = Ball.new +ball.shake diff --git a/bin/play.rb b/bin/play.rb new file mode 100644 index 0000000..c012df3 --- /dev/null +++ b/bin/play.rb @@ -0,0 +1,6 @@ +require_relative '../lib/parrot' +require_relative '../lib/game' +require 'colorize' + +game = Game.new +game.start diff --git a/lib/ball.rb b/lib/ball.rb new file mode 100644 index 0000000..ad1ad61 --- /dev/null +++ b/lib/ball.rb @@ -0,0 +1,54 @@ +class Ball + POSITIVE = [ + 'It is certain', + 'It is decidedly so', + 'Without a doubt', + 'Yes — definitely', + 'You may rely on it' + ] + WEAK = [ + 'As I see it, yes', + 'Most likely', + 'Outlook good', + 'Signs point to yes', + 'Yes' + ] + NEUTRAL = [ + 'Reply hazy, try again', + 'Ask again later', + 'Better not tell you now', + 'Cannot predict now', + 'Concentrate and ask again' + ] + NEGATIVE = [ + 'Don’t count on it', + 'My reply is no', + 'My sources say no', + 'Outlook not so good', + 'Very doubtful' + ] + + def shake + all_ansvers = [POSITIVE, WEAK, NEUTRAL, NEGATIVE] + random_emotion = all_ansvers[rand(all_ansvers.size)] + ansver = random_emotion[rand(random_emotion.size)] + puts "\n" + answer_message = give_color(random_emotion, ansver) + puts answer_message + puts "\n" + + answer_message + end + + def give_color(str, ansv) + if str == POSITIVE + ansv.to_s.light_blue + elsif str == WEAK + ansv.to_s.light_green + elsif str == NEUTRAL + ansv.to_s.light_yellow + else + ansv.to_s.light_red + end + end +end diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000..01be882 --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,137 @@ +class Game + def start + puts 'enter name of your parrot, please: '.light_green + @name = gets.chomp + @parrot = Parrot.new(@name) + display_parrot + loop do + @parrot.check_death + todo = ask_user + if @parrot.wants_to_sleep && todo != 10 && todo != 7 + puts 'turn lights on first'.light_red + next + end + case todo + when 1 then give_food + when 2 then give_water + when 3 then give_banana + when 4 then teach_new_word + when 5 then open_jail + when 6 then return_to_jail + when 7 then turn_lights_on + when 8 then turn_lights_off + when 9 then speak_with_parrot + when 10 then exit + else puts "I don't know the command".light_red + end + @parrot.life_time + display_parrot + end + end + + def display_parrot + @parrot.how_are_u + # puts @parrot.inspect + end + + def give_food + puts "#{@name} has eaten the food".light_green + @parrot.food_in_jeil += 12 + @parrot.food_in_stomach = 4 + @parrot.wants_to_fly = true + end + + def give_water + puts "#{@name} has got the water".light_green + @parrot.clean_water_count = 10 + end + + def open_jail + if @parrot.in_jeil + @parrot.in_jeil = false + puts "#{@name} has left the jail".light_green + @parrot.mood = 'happy' + @parrot.wants_to_fly = false + else + puts 'You already opened the jeil '.light_red + end + end + + def return_to_jail + if ! @parrot.in_jeil + @parrot.in_jeil = true + puts "#{@name} return to the jeil".light_green + @parrot.mood = 'good' + else + puts 'you have not open jail'.light_red + end + end + + def teach_new_word + puts "what word do you want to teach #{@name}?".light_green + word = gets.chomp + @parrot.learned_words << word + puts "#{@name} learned the word '#{word}'. it knows such words: #{@parrot.learned_words.join(', ')}".light_green + @parrot.wants_banana = true + end + + def turn_lights_off + if ! @parrot.wants_to_sleep + @parrot.wants_to_sleep = true + puts "#{@name} sleep".light_green + else + puts 'you have not turn lights on'.light_red + end + end + + def turn_lights_on + if @parrot.wants_to_sleep + @parrot.wants_to_sleep = false + puts "#{@name} wake up".light_green + @parrot.mood = 'norm' + else + puts 'you have not turn lights off'.light_red + end + end + + def give_banana + if @parrot.wants_banana + puts "#{@name} is eating the banana".light_green + @parrot.mood = 'happy' + @parrot.food_in_jeil += 4 + @parrot.food_in_stomach = 4 + @parrot.wants_banana = false + else + puts "#{@name} doesn't want a banana" + end + end + + def speak_with_parrot + puts "What word do you want to ask #{@name}?".light_green + word = gets.chomp + if @parrot.learned_words.include?(word) + puts word.light_blue + else + puts "I don't know this word".light_yellow + end + @parrot.wants_banana = true + end + + private + + def ask_user + puts "\n\n\n" + puts 'What do you want to do? pick a command'.light_green + puts 'give food >> type 1'.light_green + puts 'give water >> type 2'.light_green + puts 'give banana >> type 3'.light_green + puts 'teach new word >> type 4'.light_green + puts 'open jeil >> type 5'.light_green + puts 'return to jail >> type 6'.light_green + puts 'turn lights on >> type 7'.light_green + puts 'turn lights off >> type 8'.light_green + puts 'speak with parrot >> type 9'.light_green + puts 'exit >> type 10'.red + todo = gets.chomp.to_i + end +end diff --git a/lib/parrot.rb b/lib/parrot.rb new file mode 100644 index 0000000..06587a3 --- /dev/null +++ b/lib/parrot.rb @@ -0,0 +1,141 @@ +class Parrot + attr_accessor :clean_water_count, :food_in_stomach, :food_in_jeil, + :wants_to_sleep, :wants_banana, :learned_words, :wants_to_fly, + :in_jeil, :mood + + def initialize(name) + @name = name + @lifes = 3 + @mood = "good" + @food_in_stomach = 4 + @food_in_jeil = 12 + @clean_water_count = 10 + @in_jeil = true + @wants_to_sleeputs= false + @wants_to_fly = true + @wants_banana = false + @learned_words = [] + puts "\n\n\n" + puts "Parrot #{@name} was born.".light_yellow + + x = <<~parrot + __,---. + /__|o\ ) + `-\ / / + ,) (, + // \\ + {( )} + =======""===""=============== + ||||| + ||| + | + + parrot + puts x.light_cyan + end + + def how_are_u + print "Hi, my name is #{@name}. ".colorize(:light_cyan) + print "My mood is #{mood}. ".colorize(:light_cyan) + if has_food? + print "I have enough food. ".colorize(:light_cyan) + else + print "I have no food! ".colorize(:red) + print "I am hungry! ".colorize(:red) if hungry? + end + if has_water? + print "I have enough water. ".colorize(:light_cyan) + else + print "I have no water! ".colorize(:red) + end + print "I want banana! ".colorize(:light_yellow) if wants_banana + print "I want to fly! ".colorize(:light_yellow) if wants_to_fly + print "I sleep now! ".colorize(:light_yellow) if wants_to_sleep + print "\n" + talk + end + + def check_death + if dead? + y = <<~parrot + __,---. + /__|x\ ) + `-\ / / + ,) (, + // \\ + {( )} + =======""===""=============== + ||||| + ||| + | + parrot + puts y.light_red + puts "#{@name} is dead. his had no food neither water and no lifes".red + exit + end + end + + def life_time + if hungry? + if has_food? + @food_in_jeil -= 4 + @food_in_stomach +=4 + else + loose_one_life + life_lost = true + puts "#{@name} screams loudly!".red + end + end + if has_water? + @clean_water_count -= 2 + else + puts "#{@name} screams loudly!".red + loose_one_life if ! life_lost + end + poops + end + + + + private + + def hungry? + @food_in_stomach <= 4 + end + + def has_food? + @food_in_jeil > 0 + end + + def has_water? + @clean_water_count >= 2 + end + + def poops + @food_in_stomach -= 4 if @food_in_stomach >=4 + + end + + def loose_one_life + if @food_in_stomach <= 0 || @clean_water_count <= 0 + @lifes -=1 + puts "#{@name} screaming loadly".red + puts "you forgot give you parrot water or food, so it lost one life.".red + puts "the number of lifes = #{@lifes}".red + end + end + + def dead? + @lifes == 0 + end + + def talk + k = rand(6) + puts "#{@name} says: ".light_green + if @learned_words.empty? + k.times{puts 'krya-krya'.light_yellow} + else + k.times{puts @learned_words[rand(@learned_words.size)].light_yellow } + end + end +end diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index 5083084..c192962 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,18 +1,31 @@ -require 'yaml' -require_relative '../ball' +require 'colorize' +require_relative '../lib/ball' RSpec.describe Ball do - let(:answers) { YAML.load_file(File.join(__dir__, '../answers.yml')) } + before { Ball.any_instance.stub(:puts) } - it { expect(answers).to include(subject.shake) } - it { expect(answers).to eql(Ball::ANSWERS) } + let(:ball) { Ball.new } - describe '#shake' do - before { stub_const('Ball::ANSWERS', ['ANSWER']) } + subject { ball.shake } - it 'prints colorized answer' do - expect(STDOUT).to receive(:puts).with("\e[31mANSWER\e[0m") - subject.shake + it 'returns string' do + is_expected.to be_a(String) + end + + context 'colors' do + let(:colors) { ["\e[0;91;49m", "\e[0;93;49m", "\e[0;94;49m", "\e[0;92;49m"] } + + it 'has color' do + has_any_color = colors.any? { |el| subject.include?(el) } + expect(has_any_color).to eq(true) + end + + context 'all_colors' do + subject { (1..100).map { Ball.new.shake } } + it 'has all colors' do + has_every_color = colors.all? { |el| subject.join.include?(el) } + expect(has_every_color).to eq(true) + end end end end