Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. require 'pry'
  2.  
  3. class Robot
  4.  
  5. attr_reader :robot_location, :items, :health
  6.  
  7. MAX_WEIGHT = 250
  8.  
  9. def initialize
  10. @robot_location = [0, 0]
  11. @items = []
  12. @health = 100
  13. @weapon = nil
  14. end
  15.  
  16. # Method to find out the position of the robot
  17. def position
  18. robot_location
  19. end
  20.  
  21. def move_left
  22. robot_location[0] -= 1
  23. end
  24.  
  25. def move_right
  26. robot_location[0] += 1
  27. end
  28.  
  29. def move_up
  30. robot_location[1] += 1
  31. end
  32.  
  33. def move_down
  34. robot_location[1] -= 1
  35. end
  36.  
  37. def pick_up(item)
  38. if can_pickup?(item)
  39. if item.is_a?(Weapon)
  40. self.equipped_weapon = item
  41. else
  42. items << item
  43. end
  44. end
  45. end
  46.  
  47. def items_weight
  48. total_weight = 0
  49. items.each {|item| total_weight += item.weight}
  50. total_weight
  51. end
  52.  
  53. def can_pickup?(item)
  54. (items_weight + item.weight) <= MAX_WEIGHT
  55. end
  56.  
  57. def wound(damage)
  58. @health -= damage
  59. @health = 0 if health < 0
  60. end
  61.  
  62. def heal(heal)
  63. @health += heal
  64. @health = 100 if health > 100
  65. end
  66.  
  67. def heal!(heal)
  68. raise StandardError, "robot is dead cannot be revived" if health <= 0
  69. heal(heal)
  70. end
  71.  
  72. def attack(target)
  73. if equipped_weapon?
  74. @weapon.hit(target)
  75. else
  76. target.wound(5)
  77. end
  78. end
  79.  
  80. def attack!(target)
  81. raise StandardError, "Can only attack robots" unless target.is_a?(Robot)
  82. attack(target)
  83. end
  84.  
  85. def equipped_weapon=(new_weapon)
  86. @weapon = new_weapon
  87. end
  88.  
  89. def equipped_weapon
  90. @weapon
  91. end
  92.  
  93. def equipped_weapon?
  94. @weapon != nil
  95. end
  96.  
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement