Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

ruby '2.3.1'
ruby '2.5.1'

gem 'rspec'
gem 'rubocop'
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ DEPENDENCIES
rspec
rubocop

RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.10.6
1.16.6
42 changes: 42 additions & 0 deletions ball.rb
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions ball_version_1.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/ball_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
121 changes: 121 additions & 0 deletions tamagochi.rb
Original file line number Diff line number Diff line change
@@ -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