Skip to content

lesson2 implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ruby '2.3.1'

gem 'rspec'
gem 'rubocop'
gem 'colorize'
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -34,8 +35,12 @@ PLATFORMS
ruby

DEPENDENCIES
colorize
rspec
rubocop

RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.10.6
1.16.6
5 changes: 5 additions & 0 deletions bin/magic_8_ball.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'colorize'
require_relative '../lib/ball'

ball = Ball.new
ball.shake
6 changes: 6 additions & 0 deletions bin/play.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative '../lib/parrot'
require_relative '../lib/game'
require 'colorize'

game = Game.new
game.start
54 changes: 54 additions & 0 deletions lib/ball.rb
Original file line number Diff line number Diff line change
@@ -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
137 changes: 137 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -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
141 changes: 141 additions & 0 deletions lib/parrot.rb
Original file line number Diff line number Diff line change
@@ -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
Loading