Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'pry'
- class Robot
- attr_reader :robot_location, :items, :health
- MAX_WEIGHT = 250
- def initialize
- @robot_location = [0, 0]
- @items = []
- @health = 100
- @weapon = nil
- end
- # Method to find out the position of the robot
- def position
- robot_location
- end
- def move_left
- robot_location[0] -= 1
- end
- def move_right
- robot_location[0] += 1
- end
- def move_up
- robot_location[1] += 1
- end
- def move_down
- robot_location[1] -= 1
- end
- def pick_up(item)
- if can_pickup?(item)
- if item.is_a?(Weapon)
- self.equipped_weapon = item
- else
- items << item
- end
- end
- end
- def items_weight
- total_weight = 0
- items.each {|item| total_weight += item.weight}
- total_weight
- end
- def can_pickup?(item)
- (items_weight + item.weight) <= MAX_WEIGHT
- end
- def wound(damage)
- @health -= damage
- @health = 0 if health < 0
- end
- def heal(heal)
- @health += heal
- @health = 100 if health > 100
- end
- def heal!(heal)
- raise StandardError, "robot is dead cannot be revived" if health <= 0
- heal(heal)
- end
- def attack(target)
- if equipped_weapon?
- @weapon.hit(target)
- else
- target.wound(5)
- end
- end
- def attack!(target)
- raise StandardError, "Can only attack robots" unless target.is_a?(Robot)
- attack(target)
- end
- def equipped_weapon=(new_weapon)
- @weapon = new_weapon
- end
- def equipped_weapon
- @weapon
- end
- def equipped_weapon?
- @weapon != nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement