Skip to content

Homework 2 #7

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 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
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
45 changes: 45 additions & 0 deletions index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
end

def exit
puts 'Пока :)'
command = ''
until command == 'start'
puts 'Что бы заупстить игру заново введите start.'
command = gets.chomp
if command == 'start'
start
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
2 changes: 1 addition & 1 deletion spec/ball_spec.rb
Original file line number Diff line number Diff line change
@@ -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')) }
Expand Down
21 changes: 21 additions & 0 deletions src/ball.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'yaml'

class Ball
ANSWERS = YAML.load_file(File.join(__dir__, '../answers.yml'))

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
268 changes: 268 additions & 0 deletions src/pokemon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
class Pokemon
RANDOM_ANSWER = {
'feed' => {
'well' => [
'наелся.',
'отлично покушал :)'
],
'bad' => [
'не понравилась еда.',
'не наелся и хочет покушать еще.',
'подавился, но вы успели ему помочь.'
],
'oops' => [
'отравился и погиб.',
'подавился и погиб'
]
},
'walk' => {
'well' => [
'отлично провел время.',
'хорошо прогулялся.'
],
'bad' => [
'упал в яму и повредил ногу.',
'не нравится погода и он хочет домой.'
],
'oops' => [
'потерялся.',
'убежал.'
]
},
'sleep' => {
'well' => [
'выспался.'
]
},
'watch' => {
'well' => [
'улыбается и радуется жизни.'
]
},
'fight' => {
'well' => [
'легко одержал победу над противником.',
'победил.'
]
}
}

RANDOM_ACTION = {
'feed' => [
'feed_well',
'feed_bad',
'feed_oops'
],
'walk' => [
'walk_well',
'walk_bad',
'walk_oops'
],
'sleep' => [
'sleep_well'
],
'watch' => [
'watch_well'
],
'fight' => [
'fight_well'
]
}

POKEMON_TYPES = {
'fiery' => {
'max_health' => 10,
'damage' => 10
},
'earthy' => {
'max_health' => 15,
'damage' => 5
},
'water' => {
'max_health' => 12,
'damage' => 7
}
}

def initialize(name, type)
@name = name
@is_dead = false
@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 help
puts 'Список доступных команд:'
puts ' help'
puts ' info'
puts ' feed'
puts ' walk'
puts ' sleep'
puts ' watch'
puts ' fight'
end

def feed()
if @is_dead
puts @name + ' мертв.'
else
puts 'Вы кормите ' + @name
passage_of_time('feed')
end
end

def walk
if @is_dead
puts @name + ' мертв.'
else
puts 'Вы выгуливаете ' + @name
passage_of_time('walk')
end
end

def sleep
if @is_dead
puts @name + ' мертв.'
else
puts 'Zzzzz....'
passage_of_time('sleep')
end
end

def watch
if @is_dead
puts @name + ' мертв.'
else
puts 'Вы наблюдаете за ' + @name
passage_of_time('watch')
end
end

def fight
if @is_dead
puts @name + ' мертв.'
else
if @health < 5
puts 'У вашего покемона слишком мало жизней для сражения.'
else
passage_of_time('fight')
end
end
end

private

def initialize_type(type)
pokemon_skills = POKEMON_TYPES[type]
@type = type
@health = pokemon_skills['max_health']
@max_health = pokemon_skills['max_health']
@damage = pokemon_skills['damage']
end

def rand_number(a, b)
rand(b - a) + a
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[action][rand_number(0, RANDOM_ACTION[action].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
self.send(get_random_action(action))
end
end

def feed_well
random = rand_number(1, 5)
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

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
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