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
41 changes: 41 additions & 0 deletions employee_after.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Employee
attr_reader :state, :hp

def initialize(state: state)
@state = state
@hp = init_hp
end

def tickets_attack(atk)
est = EmployeeState.gen(state)
@hp = est.tickets_attack(@hp, atk)
update_state!
end

def take_break
est = EmployeeState.gen(state)
@hp = est.take_break(@hp)
update_state!
end

private

def update_state!
@state = if hp > 100
:super
elsif hp.between?(61, 100)
:normal
elsif hp.between?(31, 60)
:warning
elsif hp.between?(1, 30)
:dying
elsif hp <= 0
:game_over
end
end

def init_hp
est = EmployeeState.gen(state)
est.init_hp
end
end
96 changes: 96 additions & 0 deletions employee_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module EmployeeState
SUPPORT_STATES = [:super, :normal, :warning, :dying, :game_over]

def self.gen(state)
if SUPPORT_STATES.include?(state)
state_name = state.to_s.split('_').map(&:capitalize).join
Object.const_get("EmployeeState::#{state_name}").new
else
fail "Unknown state: #{state}"
end
end

class Base
def init_hp
fail NoImplementedError
end

def tickets_attack(hp, atk)
fail NoImplementedError
end

def take_break(hp)
fail NoImplementedError
end
end

class Super < Base
def tickets_attack(hp, atk)
hp - atk * 0.6
end

def take_break(hp)
hp - 10
end

def init_hp
120
end
end

class Normal < Base
def tickets_attack(hp, atk)
hp - atk * 0.8
end

def take_break(hp)
hp + 10
end

def init_hp
100
end
end

class Warning < Base
def tickets_attack(hp, atk)
hp - atk * 1
end

def take_break(hp)
hp + 40
end

def init_hp
60
end
end

class Dying < Base
def tickets_attack(hp, atk)
hp - atk * 1.5
end

def take_break(hp)
hp * 2
end

def init_hp
30
end
end

class GameOver < Base
def tickets_attack(hp, atk)
hp
end

def take_break(hp)
hp
end

def init_hp
0
end
end
end
3 changes: 2 additions & 1 deletion test/employee_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pry'
require 'minitest/autorun'
require_relative '../employee'
require_relative '../employee_state'
require_relative '../employee_after'

describe Employee do

Expand Down