Skip to content

Instantly share code, notes, and snippets.

@joshuastr
Created May 4, 2016 17:00
Show Gist options
  • Save joshuastr/ed1a44a6d8ad5df8d2c343498a9b4774 to your computer and use it in GitHub Desktop.
Save joshuastr/ed1a44a6d8ad5df8d2c343498a9b4774 to your computer and use it in GitHub Desktop.
Warcraft Barracks 3
class Barracks
attr_reader :gold, :food
def initialize
@gold = 1000
@food = 80
@footman_training_gold = 135
@footman_training_food = 2
@peasant_training_gold = 90
@peasant_training_food = 5
end
def can_train_footman?
if (self.gold >= @footman_training_gold && self.food >= @footman_training_food)
true
else
false
end
end
def train_footman
if can_train_footman? == true
@gold -= @footman_training_gold
@food -= @footmn_training_food
Footman.new
else
nil
end
end
def can_train_peasant?
if (self.gold >= @peasant_training_gold && self.food >= @peasant_training_food)
true
else
false
end
end
def train_peasant
if can_train_peasant? == true
@gold -= @peasant_training_gold
@food -= @peasant_training_food
Peasant.new
else
nil
end
end
end
# http://classic.battle.net/war3/human/units/footman.shtml
class Footman < Unit
# attr_accessor :health_points, :attack_power
def initialize
super(60,10)
# @health_points = 60
# @attack_power = 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)
end
def attack!(enemy)
super
end
def damage(ap)
super
end
end
class Peasant < Unit
def initialize
super(35, 0)
end
def attack!(enemy)
super
end
def damage(ap)
super
end
end
class Unit
attr_reader :health_points, :attack_power
def initialize(hp,ap)
@health_points = hp
@attack_power = ap
end
def attack!(enemy)
enemy.damage(@attack_power)
end
def damage(ap)
@health_points -= ap
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment