From 6c49a584d4aa7474cf72114628892d76ff8295ef Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Mon, 22 Oct 2018 21:24:17 +0300 Subject: [PATCH 01/16] create parrot & game class --- bin/play.rb | 3 ++ lib/game.rb | 11 +++++ lib/parrot.rb | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 bin/play.rb create mode 100644 lib/game.rb create mode 100644 lib/parrot.rb diff --git a/bin/play.rb b/bin/play.rb new file mode 100644 index 0000000..405b55b --- /dev/null +++ b/bin/play.rb @@ -0,0 +1,3 @@ +require_relative "../lib/parrot" +require_relative "../lib/game" +Game.new.start diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 0000000..fac1165 --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,11 @@ +class Game + def start + p "enter name of your parrot, please: " + name = gets.chomp + parrot = Parrot.new(name) + loop do + parrot.displayParrot + sleep 5 + end + end +end \ No newline at end of file diff --git a/lib/parrot.rb b/lib/parrot.rb new file mode 100644 index 0000000..13a9005 --- /dev/null +++ b/lib/parrot.rb @@ -0,0 +1,113 @@ +class Parrot + def initialize(name) + @name = name + @lives = 3 + @mood = "good" + @foodInStomach = 4 + @foodInJeil = 10 + @cleanWaterCount = 5 + @inJeil = true + @WantsTosleep = false + @whantsToFly = true + @wantsBanana = false + @learnedWords = [] + p @name + ' was born.' + end + + def displayParrot + p inspect + end + + def giveFeed + p '#{@name} has eaten the food' + @foodInJeil = 10 + lifeTime + @whantsToFly = true + end + + def giveWater + p '#{name} has got the water' + @cleanWaterCount = 5 + lifeTime + end + + def openJail + @inJeil = false + p '#{name} has left the jail' + @mood = "happy" + lifeTime + @whantsToFly = false + end + + def returnToJail + @inJeil = true + p '#{name} return to the jeil' + @mood = "good" + lifeTime + end + + def teachNewWord(word) + @learnedWords << word + @wantsBanana = true + lifeTime + end + + def turnLightsOff + @WantsToSleep = true + p '#{name} sleep' + lifeTime + end + + def turnLightsOn + @WantsToSleep = false + p '#{name} wake up' + @mood = "norm" + lifeTime + end + + private + + def hungry? + @foodInStomach <= 1 + end + + def hasFood? + @foodInJeil >= 2 + end + + def hasWater? + @cleanWaterCount >= 2 + end + + def poops + @foodInStomach -= 4 if @foodInStomach >=4 + end + + def lifeTime + poops + if hungry? + if hasFood? + @foodInJeil -= 4 + @foodInStomach += 4 + else + '#{name} screams loudly!' + end + end + if hasWater? + cleanWaterCount -= 2 + else + '#{name} screams loudly!' + end + end + + def isTalking + if @learnedWords.empty? + p "#{name} says 'krya-krya'" + else + @learnedWords[rand(@learnedWords.size)] + end + end + + + +end \ No newline at end of file From cc5897cfe1f7695dbe7bbd1b01a9b3f3eb54d4ef Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Mon, 22 Oct 2018 21:57:03 +0300 Subject: [PATCH 02/16] use underscore --- lib/parrot.rb | 114 +++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/lib/parrot.rb b/lib/parrot.rb index 13a9005..b179eeb 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -3,108 +3,108 @@ def initialize(name) @name = name @lives = 3 @mood = "good" - @foodInStomach = 4 - @foodInJeil = 10 - @cleanWaterCount = 5 - @inJeil = true - @WantsTosleep = false - @whantsToFly = true - @wantsBanana = false - @learnedWords = [] - p @name + ' was born.' + @food_in_stomach = 4 + @food_in_jeil = 10 + @clean_water_count = 5 + @in_jeil = true + @wants_to_sleep = false + @wants_to_fly = true + @wants_banana = false + @learned_words = [] + p 'Parrot ' + @name + ' was born.' end - def displayParrot + def display_parrot p inspect end - def giveFeed - p '#{@name} has eaten the food' - @foodInJeil = 10 - lifeTime - @whantsToFly = true + def give_food + p "#{@name} has eaten the food" + @food_in_jeil = 10 + life_time + @wants_to_fly = true end - def giveWater - p '#{name} has got the water' - @cleanWaterCount = 5 - lifeTime + def give_water + p "#{name} has got the water" + @clean_water_count = 5 + life_time end - def openJail - @inJeil = false - p '#{name} has left the jail' + def open_jail + @in_jeil = false + p "#{name} has left the jail" @mood = "happy" - lifeTime - @whantsToFly = false + life_time + @wants_to_fly = false end - def returnToJail - @inJeil = true - p '#{name} return to the jeil' + def return_to_jail + @in_jeil = true + p "#{name} return to the jeil" @mood = "good" - lifeTime + life_time end - def teachNewWord(word) - @learnedWords << word - @wantsBanana = true - lifeTime + def teach_new_word(word) + @learned_words << word + @wants_banana = true + life_time end - def turnLightsOff - @WantsToSleep = true - p '#{name} sleep' - lifeTime + def turn_lights_off + @wants_to_sleep = true + p "#{name} sleep" + life_time end - def turnLightsOn - @WantsToSleep = false - p '#{name} wake up' + def turn_lights_on + @wants_to_sleep = false + p "#{name} wake up" @mood = "norm" - lifeTime + life_time end private def hungry? - @foodInStomach <= 1 + @food_in_stomach <= 1 end - def hasFood? - @foodInJeil >= 2 + def has_food? + @food_in_jeil >= 2 end - def hasWater? - @cleanWaterCount >= 2 + def has_water? + @clean_water_count >= 2 end def poops - @foodInStomach -= 4 if @foodInStomach >=4 + @food_in_stomach -= 4 if @food_in_stomach >=4 end - def lifeTime + def life_time poops if hungry? - if hasFood? - @foodInJeil -= 4 - @foodInStomach += 4 + if has_food? + @food_in_jeil -= 4 + @food_in_stomach += 4 else - '#{name} screams loudly!' + "#{name} screams loudly!" end end - if hasWater? - cleanWaterCount -= 2 + if has_water? + @clean_water_count -= 2 else - '#{name} screams loudly!' + "#{name} screams loudly!" end end - def isTalking - if @learnedWords.empty? + def is_talking? + if @learned_words.empty? p "#{name} says 'krya-krya'" else - @learnedWords[rand(@learnedWords.size)] + @learned_words[rand(@learned_words.size)] end end From 05c4cf52ca462ab2cd7e2a4983eb5baee590594d Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Tue, 23 Oct 2018 19:14:02 +0300 Subject: [PATCH 03/16] implement 8 commands --- bin/play.rb | 3 +- lib/game.rb | 103 +++++++++++++++++++++++++++++++++++++++++++++++--- lib/parrot.rb | 58 +++------------------------- 3 files changed, 106 insertions(+), 58 deletions(-) diff --git a/bin/play.rb b/bin/play.rb index 405b55b..2bc51ed 100644 --- a/bin/play.rb +++ b/bin/play.rb @@ -1,3 +1,4 @@ require_relative "../lib/parrot" require_relative "../lib/game" -Game.new.start +game = Game.new +game.start diff --git a/lib/game.rb b/lib/game.rb index fac1165..095bb42 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,11 +1,104 @@ class Game - def start + + + def start p "enter name of your parrot, please: " - name = gets.chomp - parrot = Parrot.new(name) + @name = gets.chomp + @parrot = Parrot.new(@name) + ask_user loop do - parrot.displayParrot - sleep 5 + todo = user_input + case todo + when 1 then give_food + when 2 then give_water + when 3 then give_banana + when 4 + p "what word you wanna teach #{@name}" + word = gets.chomp + teach_new_word(word) + when 5 then open_jail + when 6 then return_to_jail + when 7 then turn_lights_on + when 8 then turn_lights_off + #else p "i don't now the command" + else break + end end + end + + + def display_parrot + p inspect + end + + def give_food + p "#{@name} has eaten the food" + @parrot.food_in_jeil = 10 + @parrot.wants_to_fly = true + end + + def give_water + p "#{@name} has got the water" + @parrot.clean_water_count = 5 + end + + def open_jail + @in_jeil = false + p "#{@name} has left the jail" + @parrot.mood = "happy" + @parrot.wants_to_fly = false + end + + def return_to_jail + @parrot.in_jeil = true + p "#{@name} return to the jeil" + @parrot.mood = "good" + + end + + def teach_new_word(word) + @parrot.learned_words << word + p "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words}" + @parrot.wants_banana = true + + end + + def turn_lights_off + @parrot.wants_to_sleep = true + p "#{@name} sleep" + + end + + def turn_lights_on + @parrot.wants_to_sleep = false + p "#{@name} wake up" + @parrot.mood = "norm" + + end + + def give_banana + p "#{@name} eats banana" + @parrot.mood = "happy" + @parrot.wants_banana = false + end + + private + + def ask_user + p "What do you want to do? choose command " + p "give food >> write 1" + p "give water >> write 2" + p "give banana >> write 3" + p "teach new word >> write 4" + p "open jeil >> write 5" + p "return to jail >> write 6" + p "turn lights on >> write 7" + p "turn lights off >> write 8" + end + + def user_input + todo = gets.chomp.to_i + end + end \ No newline at end of file diff --git a/lib/parrot.rb b/lib/parrot.rb index b179eeb..c023ec5 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -1,4 +1,8 @@ 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 @lives = 3 @@ -14,61 +18,11 @@ def initialize(name) p 'Parrot ' + @name + ' was born.' end - def display_parrot - p inspect - end - - def give_food - p "#{@name} has eaten the food" - @food_in_jeil = 10 - life_time - @wants_to_fly = true - end - - def give_water - p "#{name} has got the water" - @clean_water_count = 5 - life_time - end - - def open_jail - @in_jeil = false - p "#{name} has left the jail" - @mood = "happy" - life_time - @wants_to_fly = false - end - - def return_to_jail - @in_jeil = true - p "#{name} return to the jeil" - @mood = "good" - life_time - end - - def teach_new_word(word) - @learned_words << word - @wants_banana = true - life_time - end - - def turn_lights_off - @wants_to_sleep = true - p "#{name} sleep" - life_time - end - - def turn_lights_on - @wants_to_sleep = false - p "#{name} wake up" - @mood = "norm" - life_time - end - + private def hungry? - @food_in_stomach <= 1 + @food_in_stomach <= 2 end def has_food? From 383078f8349902bea0ebf748a8066c36fc42e945 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Wed, 24 Oct 2018 15:39:05 +0300 Subject: [PATCH 04/16] it is working! --- lib/game.rb | 16 +++++++++---- lib/parrot.rb | 65 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/lib/game.rb b/lib/game.rb index 095bb42..57472ac 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -7,6 +7,9 @@ def start @parrot = Parrot.new(@name) ask_user loop do + display_parrot + p "please enter command number: " + todo = user_input case todo when 1 then give_food @@ -20,9 +23,10 @@ def start when 6 then return_to_jail when 7 then turn_lights_on when 8 then turn_lights_off - #else p "i don't now the command" - else break + when 9 then exit + else p "i don't now the command" end + @parrot.life_time end end @@ -34,13 +38,14 @@ def display_parrot def give_food p "#{@name} has eaten the food" - @parrot.food_in_jeil = 10 + @parrot.food_in_jeil = 12 + @parrot.food_in_stomach = 4 @parrot.wants_to_fly = true end def give_water p "#{@name} has got the water" - @parrot.clean_water_count = 5 + @parrot.clean_water_count = 10 end def open_jail @@ -95,8 +100,9 @@ def ask_user p "return to jail >> write 6" p "turn lights on >> write 7" p "turn lights off >> write 8" + p "exit >> write 9" end - + def user_input todo = gets.chomp.to_i end diff --git a/lib/parrot.rb b/lib/parrot.rb index c023ec5..682badd 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -5,28 +5,52 @@ class Parrot def initialize(name) @name = name - @lives = 3 + @lifes = 3 @mood = "good" @food_in_stomach = 4 - @food_in_jeil = 10 - @clean_water_count = 5 + @food_in_jeil = 12 + @clean_water_count = 6 @in_jeil = true @wants_to_sleep = false @wants_to_fly = true @wants_banana = false @learned_words = [] p 'Parrot ' + @name + ' was born.' + end + + + def life_time + if dead? + p "#{@name} is dead. his had no food neither water and no lifes" + exit + end + if hungry? + if has_food? + @food_in_jeil -= 4 + @food_in_stomach +=4 + else + loose_one_life + p "#{@name} screams loudly!" + end + end + if has_water? + @clean_water_count -= 2 + else + p "#{@name} screams loudly!" + end + poops end - + + private def hungry? - @food_in_stomach <= 2 + @food_in_stomach <= 4 end def has_food? - @food_in_jeil >= 2 + @food_in_jeil > 0 end def has_water? @@ -35,25 +59,24 @@ def has_water? def poops @food_in_stomach -= 4 if @food_in_stomach >=4 + end - def life_time - poops - if hungry? - if has_food? - @food_in_jeil -= 4 - @food_in_stomach += 4 - else - "#{name} screams loudly!" - end + def loose_one_life + if @food_in_stomach == 0 || @clean_water_count == 0 + @lifes -=1 + p "#{@name} screaming loadly" + p "you forgot give you parrot water or food, so it loose one life." + p "the number of lifes = #{@lifes}" end - if has_water? - @clean_water_count -= 2 - else - "#{name} screams loudly!" - end - end + end + + def dead? + @lifes == 0 + end + + def is_talking? if @learned_words.empty? p "#{name} says 'krya-krya'" From dff375c1be0ad1dbdba8f8d3d7ddd934a26aa32d Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Wed, 24 Oct 2018 18:27:58 +0300 Subject: [PATCH 05/16] add colors, refactoring --- Gemfile | 1 + Gemfile.lock | 7 +++++- bin/play.rb | 1 + lib/game.rb | 60 +++++++++++++++++++++++++-------------------------- lib/parrot.rb | 45 +++++++++++++++++++++++++++----------- 5 files changed, 70 insertions(+), 44 deletions(-) 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/play.rb b/bin/play.rb index 2bc51ed..0bdbbcb 100644 --- a/bin/play.rb +++ b/bin/play.rb @@ -1,4 +1,5 @@ require_relative "../lib/parrot" require_relative "../lib/game" +require 'colorize' game = Game.new game.start diff --git a/lib/game.rb b/lib/game.rb index 57472ac..2b19d40 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -2,21 +2,19 @@ class Game def start - p "enter name of your parrot, please: " + puts "enter name of your parrot, please: ".light_green @name = gets.chomp @parrot = Parrot.new(@name) - ask_user + loop do display_parrot - p "please enter command number: " - - todo = user_input + todo = ask_user case todo when 1 then give_food when 2 then give_water when 3 then give_banana when 4 - p "what word you wanna teach #{@name}" + puts "what word you wanna teach #{@name}".light_green word = gets.chomp teach_new_word(word) when 5 then open_jail @@ -24,7 +22,7 @@ def start when 7 then turn_lights_on when 8 then turn_lights_off when 9 then exit - else p "i don't now the command" + else puts "i don't now the command".light_red end @parrot.life_time end @@ -33,57 +31,57 @@ def start def display_parrot - p inspect + @parrot.how_are_u end def give_food - p "#{@name} has eaten the 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 - p "#{@name} has got the water" + puts "#{@name} has got the water".light_green @parrot.clean_water_count = 10 end def open_jail @in_jeil = false - p "#{@name} has left the jail" + puts "#{@name} has left the jail".light_green @parrot.mood = "happy" @parrot.wants_to_fly = false end def return_to_jail @parrot.in_jeil = true - p "#{@name} return to the jeil" + puts "#{@name} return to the jeil".light_green @parrot.mood = "good" end def teach_new_word(word) @parrot.learned_words << word - p "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words}" + puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words}".light_green @parrot.wants_banana = true end def turn_lights_off - @parrot.wants_to_sleep = true - p "#{@name} sleep" + @parrot.wants_to_sleeputs = true + puts "#{@name} sleep".light_green end def turn_lights_on - @parrot.wants_to_sleep = false - p "#{@name} wake up" + @parrot.wants_to_sleeputs = false + puts "#{@name} wake up".light_green @parrot.mood = "norm" end def give_banana - p "#{@name} eats banana" + puts "#{@name} eats banana".light_green @parrot.mood = "happy" @parrot.wants_banana = false end @@ -91,20 +89,20 @@ def give_banana private def ask_user - p "What do you want to do? choose command " - p "give food >> write 1" - p "give water >> write 2" - p "give banana >> write 3" - p "teach new word >> write 4" - p "open jeil >> write 5" - p "return to jail >> write 6" - p "turn lights on >> write 7" - p "turn lights off >> write 8" - p "exit >> write 9" - end - - def user_input + puts "\n\n\n" + puts "What do you want to do? choose command ".light_green + puts "give food >> write 1".light_green + puts "give water >> write 2".light_green + puts "give banana >> write 3".light_green + puts "teach new word >> write 4".light_green + puts "open jeil >> write 5".light_green + puts "return to jail >> write 6".light_green + puts "turn lights on >> write 7".light_green + puts "turn lights off >> write 8".light_green + puts "exit >> write 9".red todo = gets.chomp.to_i end + + end \ No newline at end of file diff --git a/lib/parrot.rb b/lib/parrot.rb index 682badd..cc26f9e 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -9,19 +9,40 @@ def initialize(name) @mood = "good" @food_in_stomach = 4 @food_in_jeil = 12 - @clean_water_count = 6 + @clean_water_count = 10 @in_jeil = true - @wants_to_sleep = false + @wants_to_sleeputs= false @wants_to_fly = true @wants_banana = false @learned_words = [] - p 'Parrot ' + @name + ' was born.' + puts "\n\n\n" + puts "Parrot #{@name} was born.".light_yellow 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 enought 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 enought 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 want to sleep! ".colorize(:light_yellow) if wants_to_sleep + print "\n" + is_talking + end def life_time if dead? - p "#{@name} is dead. his had no food neither water and no lifes" + puts"#{@name} is dead. his had no food neither water and no lifes" exit end if hungry? @@ -30,13 +51,13 @@ def life_time @food_in_stomach +=4 else loose_one_life - p "#{@name} screams loudly!" + puts "#{@name} screams loudly!".red end end if has_water? @clean_water_count -= 2 else - p "#{@name} screams loudly!" + puts "#{@name} screams loudly!".red end poops end @@ -65,9 +86,9 @@ def poops def loose_one_life if @food_in_stomach == 0 || @clean_water_count == 0 @lifes -=1 - p "#{@name} screaming loadly" - p "you forgot give you parrot water or food, so it loose one life." - p "the number of lifes = #{@lifes}" + puts "#{@name} screaming loadly".red + puts "you forgot give you parrot water or food, so it loose one life.".red + puts "the number of lifes = #{@lifes}".red end end @@ -77,11 +98,11 @@ def dead? - def is_talking? + def is_talking if @learned_words.empty? - p "#{name} says 'krya-krya'" + 5.times{puts 'krya-krya'.light_yellow} else - @learned_words[rand(@learned_words.size)] + 5.times{puts @learned_words[rand(@learned_words.size)].light_yellow } end end From 973299e62886ab83e803491198abe4c8bc1fd14a Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Wed, 24 Oct 2018 19:26:50 +0300 Subject: [PATCH 06/16] add pictures --- lib/parrot.rb | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/parrot.rb b/lib/parrot.rb index cc26f9e..7106219 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -17,6 +17,21 @@ def initialize(name) @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 @@ -42,7 +57,20 @@ def how_are_u def life_time if dead? - puts"#{@name} is dead. his had no food neither water and no lifes" + y = <<~parrot + __,---. + /__|x\ ) + `-\ / / + ,) (, + // \\ + {( )} + =======""===""=============== + ||||| + ||| + | + parrot + puts y.light_red + puts "#{@name} is dead. his had no food neither water and no lifes".red exit end if hungry? From dd380e4c342f70a85c1d1282febbf190f78b8439 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Wed, 24 Oct 2018 20:48:15 +0300 Subject: [PATCH 07/16] refactor jail and light methods --- lib/game.rb | 76 ++++++++++++++++++++++++++++----------------------- lib/parrot.rb | 4 +-- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/lib/game.rb b/lib/game.rb index 2b19d40..7909e9d 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,6 +1,4 @@ class Game - - def start puts "enter name of your parrot, please: ".light_green @name = gets.chomp @@ -8,7 +6,8 @@ def start loop do display_parrot - todo = ask_user + todo = ask_user + puts "turn lights on first".light_red if @parrot.wants_to_sleep case todo when 1 then give_food when 2 then give_water @@ -26,17 +25,15 @@ def start end @parrot.life_time end - end - def display_parrot @parrot.how_are_u end def give_food puts "#{@name} has eaten the food".light_green - @parrot.food_in_jeil = 12 + @parrot.food_in_jeil += 12 @parrot.food_in_stomach = 4 @parrot.wants_to_fly = true end @@ -47,42 +44,56 @@ def give_water end def open_jail - @in_jeil = false - puts "#{@name} has left the jail".light_green - @parrot.mood = "happy" - @parrot.wants_to_fly = false + 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 jeil ".light_red + end end def return_to_jail - @parrot.in_jeil = true - puts "#{@name} return to the jeil".light_green - @parrot.mood = "good" - + if ! @parrot.in_jeil + @parrot.in_jeil = true + puts "#{@name} return to the jeil".light_green + @parrot.mood = "good" + else + puts "you did not open jail".light_red + end end def teach_new_word(word) @parrot.learned_words << word puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words}".light_green @parrot.wants_banana = true - end def turn_lights_off - @parrot.wants_to_sleeputs = true - puts "#{@name} sleep".light_green - + if ! @parrot.wants_to_sleep + @parrot.wants_to_sleep = true + puts "#{@name} sleep".light_green + else + puts "you did not turn lights on".light_red + end end def turn_lights_on - @parrot.wants_to_sleeputs = false - puts "#{@name} wake up".light_green - @parrot.mood = "norm" - + if @parrot.wants_to_sleep + @parrot.wants_to_sleep = false + puts "#{@name} wake up".light_green + @parrot.mood = "norm" + else + puts "you did not turn lights off".light_red + end end def give_banana puts "#{@name} eats banana".light_green @parrot.mood = "happy" + @parrot.food_in_jeil += 4 + @parrot.food_in_stomach = 4 @parrot.wants_banana = false end @@ -91,18 +102,15 @@ def give_banana def ask_user puts "\n\n\n" puts "What do you want to do? choose command ".light_green - puts "give food >> write 1".light_green - puts "give water >> write 2".light_green - puts "give banana >> write 3".light_green - puts "teach new word >> write 4".light_green - puts "open jeil >> write 5".light_green - puts "return to jail >> write 6".light_green - puts "turn lights on >> write 7".light_green - puts "turn lights off >> write 8".light_green - puts "exit >> write 9".red + 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 "exit >> type 9".red todo = gets.chomp.to_i end - - - end \ No newline at end of file diff --git a/lib/parrot.rb b/lib/parrot.rb index 7106219..d3f7267 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -50,7 +50,7 @@ def how_are_u 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 want to sleep! ".colorize(:light_yellow) if wants_to_sleep + print "I sleep now! ".colorize(:light_yellow) if wants_to_sleep print "\n" is_talking end @@ -59,7 +59,7 @@ def life_time if dead? y = <<~parrot __,---. - /__|x\ ) + /__|x\ ) `-\ / / ,) (, // \\ From 1284b2c5221bfd2497e60e8a21f4446ecb99d925 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Wed, 24 Oct 2018 22:54:25 +0300 Subject: [PATCH 08/16] refactoring --- lib/game.rb | 55 +++++++++++++++++++++++++++++++++++---------------- lib/parrot.rb | 34 ++++++++++++++++--------------- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/lib/game.rb b/lib/game.rb index 7909e9d..d7fc1cd 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -2,33 +2,36 @@ class Game def start puts "enter name of your parrot, please: ".light_green @name = gets.chomp - @parrot = Parrot.new(@name) - + @parrot = Parrot.new(@name) + display_parrot loop do - display_parrot + @parrot.check_death todo = ask_user - puts "turn lights on first".light_red if @parrot.wants_to_sleep + 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 - puts "what word you wanna teach #{@name}".light_green - word = gets.chomp - teach_new_word(word) + 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 exit + when 9 then speak_with_parrot + when 10 then exit else puts "i don't now 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 @@ -64,9 +67,11 @@ def return_to_jail end end - def teach_new_word(word) + def teach_new_word + puts "what word you wanna teach #{@name}".light_green + word = gets.chomp @parrot.learned_words << word - puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words}".light_green + puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words.join(", ")}".light_green @parrot.wants_banana = true end @@ -90,11 +95,26 @@ def turn_lights_on end def give_banana - puts "#{@name} eats banana".light_green - @parrot.mood = "happy" - @parrot.food_in_jeil += 4 - @parrot.food_in_stomach = 4 - @parrot.wants_banana = false + if @parrot.wants_banana + puts "#{@name} eats 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 @@ -110,7 +130,8 @@ def ask_user 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 "exit >> type 9".red + puts "speak with parrot >> type 9".light_green + puts "exit >> type 10".red todo = gets.chomp.to_i end end \ No newline at end of file diff --git a/lib/parrot.rb b/lib/parrot.rb index d3f7267..06587a3 100644 --- a/lib/parrot.rb +++ b/lib/parrot.rb @@ -38,13 +38,13 @@ 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 enought food. ".colorize(:light_cyan) + 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 enought water. ".colorize(:light_cyan) + print "I have enough water. ".colorize(:light_cyan) else print "I have no water! ".colorize(:red) end @@ -52,10 +52,10 @@ def how_are_u 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" - is_talking + talk end - def life_time + def check_death if dead? y = <<~parrot __,---. @@ -73,19 +73,24 @@ def life_time 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 + @clean_water_count -= 2 else puts "#{@name} screams loudly!".red + loose_one_life if ! life_lost end poops end @@ -112,10 +117,10 @@ def poops end def loose_one_life - if @food_in_stomach == 0 || @clean_water_count == 0 + 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 loose one life.".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 @@ -124,16 +129,13 @@ def dead? @lifes == 0 end - - - def is_talking + def talk + k = rand(6) + puts "#{@name} says: ".light_green if @learned_words.empty? - 5.times{puts 'krya-krya'.light_yellow} + k.times{puts 'krya-krya'.light_yellow} else - 5.times{puts @learned_words[rand(@learned_words.size)].light_yellow } + k.times{puts @learned_words[rand(@learned_words.size)].light_yellow } end end - - - -end \ No newline at end of file +end From d00635b924e63c4cd6ab92550334d50ab562af98 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 16:10:01 +0300 Subject: [PATCH 09/16] Magic 8 ball is ready --- lib/ball.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/ball.rb diff --git a/lib/ball.rb b/lib/ball.rb new file mode 100644 index 0000000..8c71de6 --- /dev/null +++ b/lib/ball.rb @@ -0,0 +1,48 @@ +require "colorize" +class Ball + def shake + @positive = ["It is certain (Бесспорно)", + "It is decidedly so (Предрешено)", + "Without a doubt (Никаких сомнений)", + "Yes — definitely (Определённо да)", + "You may rely on it (Можешь быть уверен в этом)"] + + @hz = ["As I see it, yes (Мне кажется — «да»)", + "Most likely (Вероятнее всего)", + "Outlook good (Хорошие перспективы)", + "Signs point to yes (Знаки говорят — «да»)", + "Yes (Да)"] + @neitral = [ "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 (Весьма сомнительно)"] + all_ansvers = [@positive, @hz, @neitral, @negative] + + random_emotion = all_ansvers[rand(all_ansvers.size)] + ansver = random_emotion[rand(random_emotion.size)] + puts "\n\n" + give_color(random_emotion, ansver) + puts "\n\n" + + end + def give_color(str, ansv) + if str == @positive + puts ansv.light_blue + elsif str == @hz + puts ansv.light_green + elsif str == @neitral + puts ansv.light_yellow + else + puts ansv.light_red + end + end + +end +ball = Ball.new +ball.shake \ No newline at end of file From 70ac0c790728b8e5bee047d1afdf81fa4697ac00 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 17:26:04 +0300 Subject: [PATCH 10/16] extract constants --- bin/play.rb | 4 +- lib/ball.rb | 82 ++++++++-------- lib/game.rb | 236 +++++++++++++++++++++++----------------------- spec/ball_spec.rb | 2 +- 4 files changed, 161 insertions(+), 163 deletions(-) diff --git a/bin/play.rb b/bin/play.rb index 0bdbbcb..1ae636a 100644 --- a/bin/play.rb +++ b/bin/play.rb @@ -1,5 +1,5 @@ -require_relative "../lib/parrot" -require_relative "../lib/game" +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 index 8c71de6..a20e5f2 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -1,48 +1,46 @@ -require "colorize" +require 'colorize' class Ball - def shake - @positive = ["It is certain (Бесспорно)", - "It is decidedly so (Предрешено)", - "Without a doubt (Никаких сомнений)", - "Yes — definitely (Определённо да)", - "You may rely on it (Можешь быть уверен в этом)"] + POSITIVE = ['It is certain', + 'It is decidedly so', + 'Without a doubt', + 'Yes — definitely', + 'You may rely on it'] + HZ = ['As I see it, yes', + 'Most likely', + 'Outlook good', + 'Signs point to yes', + 'Yes'] + NEITRAL = ['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'] - @hz = ["As I see it, yes (Мне кажется — «да»)", - "Most likely (Вероятнее всего)", - "Outlook good (Хорошие перспективы)", - "Signs point to yes (Знаки говорят — «да»)", - "Yes (Да)"] - @neitral = [ "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 (Весьма сомнительно)"] - all_ansvers = [@positive, @hz, @neitral, @negative] + def shake + all_ansvers = [POSITIVE, HZ, NEITRAL, NEGATIVE] + random_emotion = all_ansvers[rand(all_ansvers.size)] + ansver = random_emotion[rand(random_emotion.size)] + # puts "\n\n" + give_color(random_emotion, ansver) + # puts "\n\n" + end - random_emotion = all_ansvers[rand(all_ansvers.size)] - ansver = random_emotion[rand(random_emotion.size)] - puts "\n\n" - give_color(random_emotion, ansver) - puts "\n\n" - - end - def give_color(str, ansv) - if str == @positive - puts ansv.light_blue - elsif str == @hz - puts ansv.light_green - elsif str == @neitral - puts ansv.light_yellow - else - puts ansv.light_red - end + def give_color(str, ansv) + if str == POSITIVE + puts ansv.to_s.light_blue + elsif str == HZ + puts ansv.to_s.light_green + elsif str == NEITRAL + puts ansv.to_s.light_yellow + else + puts ansv.to_s.light_red end - + end end ball = Ball.new -ball.shake \ No newline at end of file +ball.shake diff --git a/lib/game.rb b/lib/game.rb index d7fc1cd..a7b2937 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,137 +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 now the command".light_red - end - @parrot.life_time - display_parrot - end + 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 now the command".light_red + end + @parrot.life_time + display_parrot end + end - def display_parrot - @parrot.how_are_u - #puts @parrot.inspect - 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_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 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 jeil ".light_red - 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 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 did not open jail".light_red - 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 did not open jail'.light_red end + end - def teach_new_word - puts "what word you wanna teach #{@name}".light_green - word = gets.chomp - @parrot.learned_words << word - puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words.join(", ")}".light_green - @parrot.wants_banana = true - end + def teach_new_word + puts "what word you wanna teach #{@name}".light_green + word = gets.chomp + @parrot.learned_words << word + puts "#{@name} teach word '#{word}'. it knows this 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 did not turn lights on".light_red - end + def turn_lights_off + if ! @parrot.wants_to_sleep + @parrot.wants_to_sleep = true + puts "#{@name} sleep".light_green + else + puts 'you did 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 did not turn lights off".light_red - 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 did not turn lights off'.light_red end + end - def give_banana - if @parrot.wants_banana - puts "#{@name} eats 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 + def give_banana + if @parrot.wants_banana + puts "#{@name} eats 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 + 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? choose 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 \ No newline at end of file + private + + def ask_user + puts "\n\n\n" + puts 'What do you want to do? choose 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/spec/ball_spec.rb b/spec/ball_spec.rb index 5083084..9f2bdf3 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,5 +1,5 @@ require 'yaml' -require_relative '../ball' +require_relative '../lib/ball' RSpec.describe Ball do let(:answers) { YAML.load_file(File.join(__dir__, '../answers.yml')) } From e8eae8a158557eb6400bec6bf6f762c068d736f4 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 17:41:31 +0300 Subject: [PATCH 11/16] add executive command --- bin/magic_8_ball.rb | 5 +++++ lib/ball.rb | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 bin/magic_8_ball.rb diff --git a/bin/magic_8_ball.rb b/bin/magic_8_ball.rb new file mode 100644 index 0000000..b8c7343 --- /dev/null +++ b/bin/magic_8_ball.rb @@ -0,0 +1,5 @@ +require "colorize" +require_relative "../lib/ball" + +ball = Ball.new +ball.shake \ No newline at end of file diff --git a/lib/ball.rb b/lib/ball.rb index a20e5f2..bbc132b 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -1,4 +1,3 @@ -require 'colorize' class Ball POSITIVE = ['It is certain', 'It is decidedly so', @@ -42,5 +41,5 @@ def give_color(str, ansv) end end end -ball = Ball.new -ball.shake + + From 79370ac924e68178db1e86b4d91a286788da4aa4 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 18:20:32 +0300 Subject: [PATCH 12/16] replace test --- lib/ball.rb | 16 +++++++++------- spec/ball_spec.rb | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/ball.rb b/lib/ball.rb index bbc132b..620aa09 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -24,20 +24,22 @@ def shake all_ansvers = [POSITIVE, HZ, NEITRAL, NEGATIVE] random_emotion = all_ansvers[rand(all_ansvers.size)] ansver = random_emotion[rand(random_emotion.size)] - # puts "\n\n" - give_color(random_emotion, ansver) - # puts "\n\n" + 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 - puts ansv.to_s.light_blue + ansv.to_s.light_blue elsif str == HZ - puts ansv.to_s.light_green + ansv.to_s.light_green elsif str == NEITRAL - puts ansv.to_s.light_yellow + ansv.to_s.light_yellow else - puts ansv.to_s.light_red + ansv.to_s.light_red end end end diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index 9f2bdf3..aa77ed0 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,18 +1,18 @@ -require 'yaml' +require 'colorize' require_relative '../lib/ball' RSpec.describe Ball do - let(:answers) { YAML.load_file(File.join(__dir__, '../answers.yml')) } + let(:ball){Ball.new} + subject{ball.shake} - it { expect(answers).to include(subject.shake) } - it { expect(answers).to eql(Ball::ANSWERS) } - - describe '#shake' do - before { stub_const('Ball::ANSWERS', ['ANSWER']) } + it "returns string" do + expect(subject).to be_a(String) + end - it 'prints colorized answer' do - expect(STDOUT).to receive(:puts).with("\e[31mANSWER\e[0m") - subject.shake - end + it "has color" do + colors = ["\e[0;91;49m", "\e[0;93;49m", "\e[0;94;49m", "\e[0;92;49m"] + has_any_color = colors.any?{| el | subject.include?(el) } + expect(has_any_color).to eq(true) end + end From e1fd40ac75ffd2f4f196eb07d8baa83a7c992a15 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 18:45:58 +0300 Subject: [PATCH 13/16] refactoring tests --- spec/ball_spec.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index aa77ed0..e85b771 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -3,16 +3,27 @@ RSpec.describe Ball do let(:ball){Ball.new} + #before { $stdout.stub(:write) } subject{ball.shake} it "returns string" do - expect(subject).to be_a(String) + is_expected.to be_a(String) end - it "has color" do - colors = ["\e[0;91;49m", "\e[0;93;49m", "\e[0;94;49m", "\e[0;92;49m"] - has_any_color = colors.any?{| el | subject.include?(el) } - expect(has_any_color).to eq(true) - 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 "return_all_colors" do + has_every_color = colors.all?{| el | subject.join.include?(el) } + expect(has_every_color).to eq(true) + end + end + end end From 8f7b361d46c91f5932d331c47a3c66037b826fc5 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 18:58:08 +0300 Subject: [PATCH 14/16] rename constant --- lib/ball.rb | 17 +++++++++++------ spec/ball_spec.rb | 5 +++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/ball.rb b/lib/ball.rb index 620aa09..1b3037b 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -4,7 +4,7 @@ class Ball 'Without a doubt', 'Yes — definitely', 'You may rely on it'] - HZ = ['As I see it, yes', + WEAK = ['As I see it, yes', 'Most likely', 'Outlook good', 'Signs point to yes', @@ -21,20 +21,21 @@ class Ball 'Very doubtful'] def shake - all_ansvers = [POSITIVE, HZ, NEITRAL, NEGATIVE] + all_ansvers = [POSITIVE, WEAK, NEITRAL, NEGATIVE] random_emotion = all_ansvers[rand(all_ansvers.size)] ansver = random_emotion[rand(random_emotion.size)] - puts "\n" + log("\n") answer_message = give_color(random_emotion, ansver) - puts answer_message - puts "\n" + log(answer_message) + log("\n") + answer_message end def give_color(str, ansv) if str == POSITIVE ansv.to_s.light_blue - elsif str == HZ + elsif str == WEAK ansv.to_s.light_green elsif str == NEITRAL ansv.to_s.light_yellow @@ -42,6 +43,10 @@ def give_color(str, ansv) ansv.to_s.light_red end end + + def log(message) + puts message + end end diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index e85b771..add3244 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,9 +1,10 @@ require 'colorize' require_relative '../lib/ball' -RSpec.describe Ball do +RSpec.describe Ball do + before {allow_any_instance_of(Ball).to receive(:log){} } let(:ball){Ball.new} - #before { $stdout.stub(:write) } + subject{ball.shake} it "returns string" do From 987fd155bd54b3590d4ccf26cc239afc3313a442 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Thu, 25 Oct 2018 21:19:46 +0300 Subject: [PATCH 15/16] hide puts in test, rubocop --- bin/magic_8_ball.rb | 6 ++-- lib/ball.rb | 70 +++++++++++++++++++++++---------------------- spec/ball_spec.rb | 29 ++++++++++--------- 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/bin/magic_8_ball.rb b/bin/magic_8_ball.rb index b8c7343..ce40a15 100644 --- a/bin/magic_8_ball.rb +++ b/bin/magic_8_ball.rb @@ -1,5 +1,5 @@ -require "colorize" -require_relative "../lib/ball" +require 'colorize' +require_relative '../lib/ball' ball = Ball.new -ball.shake \ No newline at end of file +ball.shake diff --git a/lib/ball.rb b/lib/ball.rb index 1b3037b..07c7205 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -1,52 +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'] - NEITRAL = ['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'] + 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' + ] + NEITRAL = [ + '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, NEITRAL, NEGATIVE] random_emotion = all_ansvers[rand(all_ansvers.size)] ansver = random_emotion[rand(random_emotion.size)] - log("\n") + puts "\n" answer_message = give_color(random_emotion, ansver) - log(answer_message) - log("\n") - + puts answer_message + puts "\n" + answer_message end def give_color(str, ansv) if str == POSITIVE - ansv.to_s.light_blue + ansv.to_s.light_blue elsif str == WEAK - ansv.to_s.light_green + ansv.to_s.light_green elsif str == NEITRAL - ansv.to_s.light_yellow + ansv.to_s.light_yellow else - ansv.to_s.light_red + ansv.to_s.light_red end end - - def log(message) - puts message - end end - - diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index add3244..d36b506 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,28 +1,29 @@ require 'colorize' require_relative '../lib/ball' -RSpec.describe Ball do - before {allow_any_instance_of(Ball).to receive(:log){} } - let(:ball){Ball.new} - - subject{ball.shake} +RSpec.describe Ball do + before { Ball.any_instance.stub(:puts) } - it "returns string" do + let(:ball) { Ball.new } + + subject { ball.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"]} + 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) } + 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 "return_all_colors" do - has_every_color = colors.all?{| el | subject.join.include?(el) } + context 'all_colors' do + subject { (1..100).map { Ball.new.shake } } + it 'return_all_colors' do + has_every_color = colors.all? { |el| subject.join.include?(el) } expect(has_every_color).to eq(true) end end From 9b7911286c86d54f43b05771ada5b18c269a7c67 Mon Sep 17 00:00:00 2001 From: "luba.t" Date: Sat, 27 Oct 2018 18:53:26 +0300 Subject: [PATCH 16/16] refactoring, review english --- bin/play.rb | 1 + lib/ball.rb | 6 +++--- lib/game.rb | 20 ++++++++++---------- spec/ball_spec.rb | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bin/play.rb b/bin/play.rb index 1ae636a..c012df3 100644 --- a/bin/play.rb +++ b/bin/play.rb @@ -1,5 +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 index 07c7205..ad1ad61 100644 --- a/lib/ball.rb +++ b/lib/ball.rb @@ -13,7 +13,7 @@ class Ball 'Signs point to yes', 'Yes' ] - NEITRAL = [ + NEUTRAL = [ 'Reply hazy, try again', 'Ask again later', 'Better not tell you now', @@ -29,7 +29,7 @@ class Ball ] def shake - all_ansvers = [POSITIVE, WEAK, NEITRAL, NEGATIVE] + all_ansvers = [POSITIVE, WEAK, NEUTRAL, NEGATIVE] random_emotion = all_ansvers[rand(all_ansvers.size)] ansver = random_emotion[rand(random_emotion.size)] puts "\n" @@ -45,7 +45,7 @@ def give_color(str, ansv) ansv.to_s.light_blue elsif str == WEAK ansv.to_s.light_green - elsif str == NEITRAL + elsif str == NEUTRAL ansv.to_s.light_yellow else ansv.to_s.light_red diff --git a/lib/game.rb b/lib/game.rb index a7b2937..01be882 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -22,7 +22,7 @@ def start when 8 then turn_lights_off when 9 then speak_with_parrot when 10 then exit - else puts "i don't now the command".light_red + else puts "I don't know the command".light_red end @parrot.life_time display_parrot @@ -53,7 +53,7 @@ def open_jail @parrot.mood = 'happy' @parrot.wants_to_fly = false else - puts 'You already opened jeil '.light_red + puts 'You already opened the jeil '.light_red end end @@ -63,15 +63,15 @@ def return_to_jail puts "#{@name} return to the jeil".light_green @parrot.mood = 'good' else - puts 'you did not open jail'.light_red + puts 'you have not open jail'.light_red end end def teach_new_word - puts "what word you wanna teach #{@name}".light_green + puts "what word do you want to teach #{@name}?".light_green word = gets.chomp @parrot.learned_words << word - puts "#{@name} teach word '#{word}'. it knows this words: #{@parrot.learned_words.join(', ')}".light_green + puts "#{@name} learned the word '#{word}'. it knows such words: #{@parrot.learned_words.join(', ')}".light_green @parrot.wants_banana = true end @@ -80,7 +80,7 @@ def turn_lights_off @parrot.wants_to_sleep = true puts "#{@name} sleep".light_green else - puts 'you did not turn lights on'.light_red + puts 'you have not turn lights on'.light_red end end @@ -90,13 +90,13 @@ def turn_lights_on puts "#{@name} wake up".light_green @parrot.mood = 'norm' else - puts 'you did not turn lights off'.light_red + puts 'you have not turn lights off'.light_red end end def give_banana if @parrot.wants_banana - puts "#{@name} eats banana".light_green + puts "#{@name} is eating the banana".light_green @parrot.mood = 'happy' @parrot.food_in_jeil += 4 @parrot.food_in_stomach = 4 @@ -107,7 +107,7 @@ def give_banana end def speak_with_parrot - puts "What word do you want to ask #{@name}".light_green + puts "What word do you want to ask #{@name}?".light_green word = gets.chomp if @parrot.learned_words.include?(word) puts word.light_blue @@ -121,7 +121,7 @@ def speak_with_parrot def ask_user puts "\n\n\n" - puts 'What do you want to do? choose command '.light_green + 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 diff --git a/spec/ball_spec.rb b/spec/ball_spec.rb index d36b506..c192962 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -22,7 +22,7 @@ context 'all_colors' do subject { (1..100).map { Ball.new.shake } } - it 'return_all_colors' do + it 'has all colors' do has_every_color = colors.all? { |el| subject.join.include?(el) } expect(has_every_color).to eq(true) end