From a159986e2cc51ea73b7184e063f3b853e7bc0e0e Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Sun, 28 Oct 2018 21:09:28 +0200 Subject: [PATCH 1/5] create pokemon class --- .gitignore | 1 + Gemfile | 2 +- Gemfile.lock | 5 +- spec/ball_spec.rb | 2 +- src/ball.rb | 7 ++ src/pokemon.rb | 226 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 src/ball.rb create mode 100644 src/pokemon.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file 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/spec/ball_spec.rb b/spec/ball_spec.rb index 5083084..131a88c 100644 --- a/spec/ball_spec.rb +++ b/spec/ball_spec.rb @@ -1,5 +1,5 @@ require 'yaml' -require_relative '../ball' +require_relative '../src/ball' RSpec.describe Ball do let(:answers) { YAML.load_file(File.join(__dir__, '../answers.yml')) } diff --git a/src/ball.rb b/src/ball.rb new file mode 100644 index 0000000..448ab56 --- /dev/null +++ b/src/ball.rb @@ -0,0 +1,7 @@ +class Ball + + + def shake(color) + + end +end diff --git a/src/pokemon.rb b/src/pokemon.rb new file mode 100644 index 0000000..6110bb3 --- /dev/null +++ b/src/pokemon.rb @@ -0,0 +1,226 @@ +class Pokemon + RANDOM_ANSWER = { + 'feed' => { + 'well' => [ + 'наелся.', + 'отлично покушал :)' + ], + 'bad' => [ + 'не понравилась еда.', + 'не наелся и хочет покушать еще.', + 'подавился, но вы успели ему помочь.' + ], + 'oops' => [ + 'отравился и погиб.', + 'подавился и погиб' + ] + }, + 'walk' => { + 'well' => [ + 'отлично провел время.', + 'хорошо прогулялся.' + ], + 'bad' => [ + 'упал в яму и повредил ногу.', + 'не нравится погода и он хочет домой.' + ], + 'oops' => [ + 'потерялся.', + 'убежал.' + ] + } + } + + RANDOM_ACTION = { + 'feed' => [ + 'feed_well', + 'feed_bad', + 'feed_oops' + ], + 'walk' => [ + 'walk_well', + 'walk_bad', + 'walk_oops' + ] + } + + POKEMON_TYPES = { + 'fiery' => { + 'health' => 5, + 'max_health' => 10, + 'damage' => 10 + }, + 'earthy' => { + 'health' => 10, + 'max_health' => 15, + 'damage' => 5 + }, + 'water' => { + 'health' => 5, + 'max_health' => 12, + 'damage' => 7 + } + } + + def initialize(name, type) + @name = name + @is_sleep = false + @mood = 5 + @water = 5 + @food = 5 + @energy = 5 + @money = 10 + + initialize_type(type) + + puts @name + ' родился.' + end + + public + + def info + puts "Name: #{@name}" + puts "Type: #{@type}" + puts "Health: #{@health}" + puts "Max health: #{@max_health}" + puts "Water: #{@water}" + puts "Food: #{@food}" + puts "Energy: #{@energy}" + puts "Money: #{@money}" + end + + def feed() + puts 'Вы кормите ' + @name + passage_of_time('feed') + end + + def walk + puts 'Вы выгуливаете ' + @name + passage_of_time('walk') + end + + def sleep + @is_sleep = true + energy = @energy + 5 + water = @water - 2 + @energy = energy >= 10 ? 10 : energy + @water = water < 0 ? 0 : water + passage_of_time('sleep') + end + + def help + puts 'Список доступных команд:' + puts ' help' + puts ' info' + puts ' feed' + puts ' walk' + puts ' sleep' + end + + private + + def initialize_type(type) + pokemon_skills = POKEMON_TYPES[type] + @type = type + @health = pokemon_skills['health'] + @max_health = pokemon_skills['health'] + @damage = pokemon_skills['damage'] + end + + def rand_number(x, y) + rand(y - x) + x + end + + def get_random_answer(action, state) + @name + ' ' + RANDOM_ANSWER[action][state][rand_number(0, RANDOM_ANSWER[action][state].length)] + end + + def get_random_action(action) + RANDOM_ACTION['walk'][rand_number(0, RANDOM_ACTION['walk'].length)] + end + + def passage_of_time(action) + if @health.zero? + puts "#{@name} погиб." + elsif @food.zero? + puts "#{@name} погиб от голода" + elsif @energy.zero? + puts "#{@name} устал и пошел спать." + sleep + elsif @health < 3 && @mood < 3 && @water < 3 && @food < 3 + puts "#{@name} убежал от вас потому что вы о нем не заботились." + else + case action + when 'feed' + self.send(get_random_action('feed')) + when 'walk' + self.send(get_random_action('walk')) + when 'sleep' + self.send(get_random_action('sleep')) + end + end + end + + def feed_well + random = rand_number(1, 5) + random = @money <= random ? @money : random + food = @food + random + health = @health + random + @food = food >= 10 ? 10 : food + @health = health > @max_health ? @max_health : health + puts get_random_answer('feed', 'well') + end + + def feed_bad + puts get_random_answer('feed', 'bad') + end + + def feed_oops + @health = 0 + puts get_random_answer('feed', 'oops') + end + + def walk_well + mood = @mood + 2 + energy = @energy - 2 + water = @water - 2 + @mood = mood >= 10 ? 10 : mood + @energy = energy < 0 ? 0 : energy + @water = water < 0 ? 0 : water + puts get_random_answer('walk', 'well') + end + + def walk_bad + puts get_random_answer('walk', 'bad') + end + + def walk_oops + @health = 0 + puts get_random_answer('walk', 'bad') + end +end + +pokemon = Pokemon.new('Pikachu', 'water') + +command = '' + +until command == 'exit' + puts '################' + puts 'Введите команду:' + puts '################' + command = gets.chomp + case command + when 'info' + pokemon.info + when 'help' + pokemon.help + when 'feed' + pokemon.feed + when 'walk' + pokemon.walk + when 'sleep' + pokemon.sleep + else + puts command + ' неизвестная команда, что бы получить полный список команд введите help' + end +end From fe6a0b430c3b141c695494bc8230ffcb154ff067 Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Sun, 28 Oct 2018 22:09:54 +0200 Subject: [PATCH 2/5] add new methods --- src/pokemon.rb | 77 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/src/pokemon.rb b/src/pokemon.rb index 6110bb3..3ccd751 100644 --- a/src/pokemon.rb +++ b/src/pokemon.rb @@ -28,6 +28,16 @@ class Pokemon 'потерялся.', 'убежал.' ] + }, + 'sleep' => { + 'well' => [ + 'выспался.' + ] + }, + 'watch' => { + 'well' => [ + 'улыбается и радуется жизни.' + ] } } @@ -41,6 +51,12 @@ class Pokemon 'walk_well', 'walk_bad', 'walk_oops' + ], + 'sleep' => [ + 'sleep_well' + ], + 'watch' => [ + 'watch_well' ] } @@ -89,6 +105,16 @@ def info puts "Money: #{@money}" end + def help + puts 'Список доступных команд:' + puts ' help' + puts ' info' + puts ' feed' + puts ' walk' + puts ' sleep' + puts ' watch' + end + def feed() puts 'Вы кормите ' + @name passage_of_time('feed') @@ -100,25 +126,21 @@ def walk end def sleep - @is_sleep = true - energy = @energy + 5 - water = @water - 2 - @energy = energy >= 10 ? 10 : energy - @water = water < 0 ? 0 : water + puts 'Zzzzz....' passage_of_time('sleep') end - def help - puts 'Список доступных команд:' - puts ' help' - puts ' info' - puts ' feed' - puts ' walk' - puts ' sleep' + def watch + puts 'Вы наблюдаете за ' + @name + passage_of_time('watch') end - private + def fight + + passage_of_time('watch') + end + private def initialize_type(type) pokemon_skills = POKEMON_TYPES[type] @type = type @@ -136,7 +158,7 @@ def get_random_answer(action, state) end def get_random_action(action) - RANDOM_ACTION['walk'][rand_number(0, RANDOM_ACTION['walk'].length)] + RANDOM_ACTION[action][rand_number(0, RANDOM_ACTION[action].length)] end def passage_of_time(action) @@ -157,13 +179,14 @@ def passage_of_time(action) self.send(get_random_action('walk')) when 'sleep' self.send(get_random_action('sleep')) + when 'watch' + self.send(get_random_action('watch')) end end end def feed_well random = rand_number(1, 5) - random = @money <= random ? @money : random food = @food + random health = @health + random @food = food >= 10 ? 10 : food @@ -198,6 +221,18 @@ def walk_oops @health = 0 puts get_random_answer('walk', 'bad') end + + def sleep_well + energy = @energy + 5 + water = @water - 2 + @energy = energy >= 10 ? 10 : energy + @water = water < 0 ? 0 : water + puts get_random_answer('sleep', 'well') + end + + def watch_well + puts get_random_answer('watch', 'well') + end end pokemon = Pokemon.new('Pikachu', 'water') @@ -205,11 +240,11 @@ def walk_oops command = '' until command == 'exit' - puts '################' - puts 'Введите команду:' - puts '################' + print 'Введите команду: ' command = gets.chomp case command + when 'exit' + puts 'Пока :)' when 'info' pokemon.info when 'help' @@ -220,7 +255,11 @@ def walk_oops pokemon.walk when 'sleep' pokemon.sleep + when 'watch' + pokemon.watch else - puts command + ' неизвестная команда, что бы получить полный список команд введите help' + puts command + ' неизвестная команда.' + print 'Что бы получить полный ' + puts 'список команд введите help' end end From 3b32ea7ad54ddd2cdc8638f31dc1bcde781af9b1 Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Sun, 28 Oct 2018 22:25:04 +0200 Subject: [PATCH 3/5] add new methods --- src/pokemon.rb | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/pokemon.rb b/src/pokemon.rb index 3ccd751..559030c 100644 --- a/src/pokemon.rb +++ b/src/pokemon.rb @@ -38,6 +38,12 @@ class Pokemon 'well' => [ 'улыбается и радуется жизни.' ] + }, + 'fight' => { + 'well' => [ + 'легко одержал победу над противником.', + 'победил.' + ] } } @@ -57,22 +63,22 @@ class Pokemon ], 'watch' => [ 'watch_well' + ], + 'fight' => [ + 'fight_well' ] } POKEMON_TYPES = { 'fiery' => { - 'health' => 5, 'max_health' => 10, 'damage' => 10 }, 'earthy' => { - 'health' => 10, 'max_health' => 15, 'damage' => 5 }, 'water' => { - 'health' => 5, 'max_health' => 12, 'damage' => 7 } @@ -113,6 +119,7 @@ def help puts ' walk' puts ' sleep' puts ' watch' + puts ' fight' end def feed() @@ -136,21 +143,24 @@ def watch end def fight - - passage_of_time('watch') + if @health < 5 + puts 'У вашего покемона слишком мало жизней для сражения.' + else + passage_of_time('fight') + end end private def initialize_type(type) pokemon_skills = POKEMON_TYPES[type] @type = type - @health = pokemon_skills['health'] - @max_health = pokemon_skills['health'] + @health = pokemon_skills['max_health'] + @max_health = pokemon_skills['max_health'] @damage = pokemon_skills['damage'] end - def rand_number(x, y) - rand(y - x) + x + def rand_number(a, b) + rand(b - a) + a end def get_random_answer(action, state) @@ -172,16 +182,7 @@ def passage_of_time(action) elsif @health < 3 && @mood < 3 && @water < 3 && @food < 3 puts "#{@name} убежал от вас потому что вы о нем не заботились." else - case action - when 'feed' - self.send(get_random_action('feed')) - when 'walk' - self.send(get_random_action('walk')) - when 'sleep' - self.send(get_random_action('sleep')) - when 'watch' - self.send(get_random_action('watch')) - end + self.send(get_random_action(action)) end end @@ -231,8 +232,17 @@ def sleep_well end def watch_well + energy = @energy + 5 + @energy = energy >= 10 ? 10 : energy puts get_random_answer('watch', 'well') end + + def fight_well + random = rand_number(1, 5) + @money += random + @health -= 1 + puts get_random_answer('fight', 'well') + end end pokemon = Pokemon.new('Pikachu', 'water') @@ -257,6 +267,8 @@ def watch_well pokemon.sleep when 'watch' pokemon.watch + when 'fight' + pokemon.fight else puts command + ' неизвестная команда.' print 'Что бы получить полный ' From 657e564347b0b15ee04d842768f66042608d301c Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Tue, 30 Oct 2018 22:35:41 +0200 Subject: [PATCH 4/5] finish work on tamagotchi --- index.rb | 48 ++++++++++++++++++++++++++++++++ src/pokemon.rb | 75 ++++++++++++++++++++++---------------------------- 2 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 index.rb diff --git a/index.rb b/index.rb new file mode 100644 index 0000000..41437be --- /dev/null +++ b/index.rb @@ -0,0 +1,48 @@ +require_relative './src/pokemon' + +class Game + def initialize + start + end + + def start + puts 'Добро пожаловать!' + print 'Введите имя покемона: ' + name = gets.chomp + print 'Введите тип покемона (water, fiery, earthy): ' + type = gets.chomp + + pokemon = Pokemon.new(name, type) + + command = '' + + until command == 'exit' + print 'Введите команду: ' + command = gets.chomp + + if pokemon.respond_to?(command) + pokemon.send(command) + elsif command == 'exit' + exit + else + puts command + ' неизвестная команда.' + print 'Что бы получить полный ' + puts 'список команд введите help' + end + end + end + + def exit + puts 'Пока :)' + command = '' + until command == 'start' + puts 'Что бы заупстить игру заново введите start.' + command = gets.chomp + if command == 'start' + start + end + end + end +end + +Game.new diff --git a/src/pokemon.rb b/src/pokemon.rb index 559030c..e3ce8f4 100644 --- a/src/pokemon.rb +++ b/src/pokemon.rb @@ -86,6 +86,7 @@ class Pokemon def initialize(name, type) @name = name + @is_dead = false @is_sleep = false @mood = 5 @water = 5 @@ -123,34 +124,55 @@ def help end def feed() - puts 'Вы кормите ' + @name - passage_of_time('feed') + if @is_dead + puts @name + ' мертв.' + else + puts 'Вы кормите ' + @name + passage_of_time('feed') + end end def walk - puts 'Вы выгуливаете ' + @name - passage_of_time('walk') + if @is_dead + puts @name + ' мертв.' + else + puts 'Вы выгуливаете ' + @name + passage_of_time('walk') + end end def sleep - puts 'Zzzzz....' - passage_of_time('sleep') + if @is_dead + puts @name + ' мертв.' + else + puts 'Zzzzz....' + passage_of_time('sleep') + end end def watch - puts 'Вы наблюдаете за ' + @name - passage_of_time('watch') + if @is_dead + puts @name + ' мертв.' + else + puts 'Вы наблюдаете за ' + @name + passage_of_time('watch') + end end def fight - if @health < 5 - puts 'У вашего покемона слишком мало жизней для сражения.' + if @is_dead + puts @name + ' мертв.' else - passage_of_time('fight') + if @health < 5 + puts 'У вашего покемона слишком мало жизней для сражения.' + else + passage_of_time('fight') + end end end private + def initialize_type(type) pokemon_skills = POKEMON_TYPES[type] @type = type @@ -244,34 +266,3 @@ def fight_well puts get_random_answer('fight', 'well') end end - -pokemon = Pokemon.new('Pikachu', 'water') - -command = '' - -until command == 'exit' - print 'Введите команду: ' - command = gets.chomp - case command - when 'exit' - puts 'Пока :)' - when 'info' - pokemon.info - when 'help' - pokemon.help - when 'feed' - pokemon.feed - when 'walk' - pokemon.walk - when 'sleep' - pokemon.sleep - when 'watch' - pokemon.watch - when 'fight' - pokemon.fight - else - puts command + ' неизвестная команда.' - print 'Что бы получить полный ' - puts 'список команд введите help' - end -end From a73e3f84a8fdc0505a3b38400ecfae50e74f002a Mon Sep 17 00:00:00 2001 From: Yarmolenko Daniil Date: Tue, 30 Oct 2018 23:11:12 +0200 Subject: [PATCH 5/5] finish work on ball class and code refactoring --- index.rb | 35 ++++++++++++++++------------------- src/ball.rb | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/index.rb b/index.rb index 41437be..857ee4a 100644 --- a/index.rb +++ b/index.rb @@ -11,25 +11,7 @@ def start name = gets.chomp print 'Введите тип покемона (water, fiery, earthy): ' type = gets.chomp - - pokemon = Pokemon.new(name, type) - - command = '' - - until command == 'exit' - print 'Введите команду: ' - command = gets.chomp - - if pokemon.respond_to?(command) - pokemon.send(command) - elsif command == 'exit' - exit - else - puts command + ' неизвестная команда.' - print 'Что бы получить полный ' - puts 'список команд введите help' - end - end + @pokemon = Pokemon.new(name, type) end def exit @@ -43,6 +25,21 @@ def exit end end end + + def get_command + command = '' + until command == 'exit' + print 'Введите команду: ' + command = gets.chomp + if @pokemon.respond_to?(command) + @pokemon.send(command) + elsif command == 'exit' + exit + else + p command + ' неизвестная команда. Что бы получить полный список команд введите help' + end + end + end end Game.new diff --git a/src/ball.rb b/src/ball.rb index 448ab56..ab79979 100644 --- a/src/ball.rb +++ b/src/ball.rb @@ -1,7 +1,21 @@ -class Ball +require 'yaml' +class Ball + ANSWERS = YAML.load_file(File.join(__dir__, '../answers.yml')) - def shake(color) + def colorize(color_index, answer) + "\e[#{31 + color_index}m#{answer}\e[0m" + end + def shake + index = rand(ANSWERS.size) + answer = ANSWERS[index] + color_index = index / 5 + puts colorize(color_index, answer) + answer end end + +ball = Ball.new + +ball.shake