Skip to content

Instantly share code, notes, and snippets.

@Ebonkuab
Created January 13, 2016 22:38
Show Gist options
  • Save Ebonkuab/d4a6748c23735c094fb3 to your computer and use it in GitHub Desktop.
Save Ebonkuab/d4a6748c23735c094fb3 to your computer and use it in GitHub Desktop.
following the TDD concept and using Rspec as the automated testing platform to code A simulation of Warcraft 3 barracks Simulator
#require_relative 'footman'
class Barracks
attr_accessor :gold,:food
def initialize ( )
# 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)
@gold = 1000
@food = 80
end
def can_train_footman?
if gold >= 135 && food >= 2
true
else
false
end
end
def train_peasant
if can_train_peasant?
self.gold -= 90
self.food -= 5
Peasant.new
else
nil
end
end
def can_train_peasant?
if gold >= 90 && food >= 5
true
else
false
end
end
def train_footman ()
if can_train_footman?
self.gold -= 135
self.food -= 2
Footman.new
else
nil
end
end
end
# http://classic.battle.net/war3/human/units/footman.shtml
class Footman < Unit
def initialize
super(60, 10)
end
end
class Peasant < Unit
# attr_accessor :health_points,:attack_power
def initialize
super(35,0)
end
end
class Unit
attr_reader :health_points
attr_reader :attack_power
def initialize (health_points , attack_power )
@health_points = health_points
@attack_power = attack_power
end
def damage (input)
@health_points -= input
end
def attack! ( enemy)
enemy.damage(@attack_power)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment