Created
June 29, 2016 00:07
-
-
Save Leejojo/1175ff8acf7a5071f18a4da887819908 to your computer and use it in GitHub Desktop.
Warcraft 3
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 >= 2 | |
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
class Footman < Unit | |
attr_reader :health_points, :attack_power | |
def initialize(hp = 60, ap = 10) | |
# Need to default the 2 instance variables here | |
# Also also give code outside this class access to these variables (via attr_reader, attr_writer or attr_accessor | |
# @health_points = hp | |
# @attack_power = ap | |
super(hp, ap) | |
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 | |
attr_reader :health_points, :attack_power | |
def initialize(hp = 35, ap = 0) | |
super(hp, ap) | |
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(hp, ap) | |
# Need to default the 2 instance variables here | |
# Also also give code outside this class access to these variables (via attr_reader, attr_writer or attr_accessor | |
@health_points = hp | |
@attack_power = ap | |
end | |
def attack!(enemy) | |
enemy.damage(@attack_power) | |
end | |
def damage(damage_point) | |
@health_points -= damage_point | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment