Created
January 3, 2013 19:41
-
-
Save rsepassi/4446406 to your computer and use it in GitHub Desktop.
intro exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Time to complete: 40 minutes | |
class Robot | |
attr_accessor :position, :items, :health, :equipped_weapon | |
def initialize | |
@position = [0,0] | |
@items = [] | |
@health = 100 | |
end | |
def move_left | |
position[0] -= 1 | |
end | |
def move_right | |
position[0] += 1 | |
end | |
def move_up | |
position[1] += 1 | |
end | |
def move_down | |
position[1] -= 1 | |
end | |
def pick_up(item) | |
if items_weight >= 250 | |
raise ArgumentError | |
else | |
items << item | |
end | |
end | |
def items_weight | |
items.inject(0) { |sum, item| sum += item.weight } | |
end | |
def wound(hit) | |
hit >= @health ? @health = 0 : @health -= hit | |
end | |
def heal(life) | |
(life + @health) >= 100 ? @health = 100 : @health += life | |
end | |
def attack(robot) | |
equipped_weapon ? equipped_weapon.hit(robot) : robot.wound(5) | |
end | |
end | |
class Item | |
attr_reader :name, :weight | |
def initialize(name, weight) | |
@name, @weight = name, weight | |
end | |
end | |
class Bolts < Item | |
def initialize | |
super("bolts", 25) | |
end | |
def feed(robot) | |
robot.heal(weight) | |
end | |
end | |
class Weapon < Item | |
attr_accessor :damage | |
def initialize(name, weight, damage) | |
@name, @weight, @damage = name, weight, damage | |
end | |
def hit(robot) | |
robot.wound(damage) | |
end | |
end | |
class Laser < Weapon | |
def initialize | |
super("laser", 125, 25) | |
end | |
end | |
class PlasmaCannon < Weapon | |
def initialize | |
super("plasma_cannon", 200, 55) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment