Created
June 29, 2016 23:44
-
-
Save FioFiyo/0ebb98c65e8ef4785a890d32a79035f9 to your computer and use it in GitHub Desktop.
Warcraft3: Create classes and methods for the specs to pass.
This file contains 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
class Barracks | |
attr_reader :gold, :food | |
def initialize | |
@gold = 1000 | |
@food = 80 | |
end | |
def train_footman | |
if can_train_footman? | |
@gold -= 135 | |
@food -= 2 | |
Footman.new | |
end | |
end | |
def can_train_footman? | |
gold >= 135 && food >= 80 | |
end | |
def train_peasant | |
if can_train_peasant? | |
@gold -= 90 | |
@food -= 5 | |
Peasant.new | |
end | |
end | |
def can_train_peasant? | |
gold >= 90 && food >= 5 | |
end | |
end |
This file contains 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
# http://classic.battle.net/war3/human/units/footman.shtml | |
class Footman < Unit | |
def initialize | |
super(60,80) | |
end | |
def attack!(enemy) | |
enemy.damage(10) | |
end | |
def damage(attack_power) | |
@health_points -= attack_power | |
end | |
end |
This file contains 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
class Peasant < Unit | |
def initialize | |
super(35,0) | |
end | |
end |
This file contains 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
class Unit | |
attr_reader :health_points, :attack_power | |
def initialize(health_points, attack_power) | |
@health_points = health_points | |
@attack_power = attack_power | |
end | |
def damage(attack_power) | |
@health_points -= attack_power | |
end | |
def attack!(enemy_unit) | |
enemy_unit.damage(3) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment