Created
March 6, 2012 01:08
-
-
Save russ/1982634 to your computer and use it in GitHub Desktop.
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
module RPG | |
class Battle | |
ARENAS = [:town, :desert, :jungle, :mountain, :boat] | |
attr_accessor :fighters | |
attr_reader :battleground | |
def initialize(fighter_a, fighter_b) | |
@fighters = [fighter_a, fighter_b] | |
@battleground = ARENAS.sort_by { rand }.first | |
end | |
def first_attacker | |
fighters.sort do |b, a| | |
a.initiative_roll <=> b.initiative_roll | |
end.first | |
end | |
end | |
class Fighter | |
attr_accessor :name, :hp, :strength, :defense | |
def initialize(*options) | |
options = Hash[*options] | |
@name = options[:name] | |
@hp = options[:hp] | |
@strength = options[:strength] | |
@defense = options[:defense] | |
end | |
def initiative_roll | |
@hp + @strength + @defense | |
end | |
def defend_from(attacker) | |
attacker.defense < defense | |
end | |
def attack(opponent, amount) | |
opponent.hit_for!(amount) | |
end | |
def hit_for!(amount) | |
@hp -= amount | |
end | |
def heal_for!(amount) | |
@hp += amount | |
end | |
def alive? | |
@hp > 0 | |
end | |
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
require_relative 'spec_helper' | |
require_relative 'rpg_battle' | |
module RPG | |
describe Battle do | |
it "enters battle with 2 opponents" do | |
battle = Battle.new(mock, mock) | |
battle.fighters.size.should == 2 | |
end | |
it "chooses a battle ground" do | |
battle = Battle.new(mock, mock) | |
battle.battleground.should_not be_nil | |
end | |
it "chooses a valid battle ground" do | |
battle = Battle.new(mock, mock) | |
RPG::Battle::ARENAS.should include battle.battleground | |
end | |
it "chooses a first attacker based on their initiative roll" do | |
fighter1 = mock(initiative_roll: 10) | |
fighter2 = mock(initiative_roll: 20) | |
battle = Battle.new(fighter1, fighter2) | |
battle.first_attacker.should == fighter2 | |
end | |
end | |
describe Fighter do | |
it "calculates an initiative roll based on their attributes" do | |
fighter = Fighter.new(hp: 10, strength: 10, defense: 10) | |
fighter.initiative_roll.should == 30 | |
end | |
it "can defend from an attack" do | |
fighter = Fighter.new(defense: 10) | |
fighter.defend_from(mock(defense: 5)).should == true | |
end | |
it "can fail to defend from an attack" do | |
fighter = Fighter.new(defense: 5) | |
fighter.defend_from(mock(defense: 10)).should == false | |
end | |
it "can attack an opponent" do | |
opponent = mock | |
opponent.should_receive(:hit_for!).with(10) | |
fighter = Fighter.new | |
fighter.attack(opponent, 10) | |
end | |
it "can take damage" do | |
fighter = Fighter.new(hp: 10) | |
fighter.hit_for!(10) | |
fighter.hp.should == 0 | |
end | |
it "can heal" do | |
fighter = Fighter.new(hp: 10) | |
fighter.heal_for!(10) | |
fighter.hp.should == 20 | |
end | |
it "knows if he is alive" do | |
fighter = Fighter.new(hp: 10) | |
fighter.alive?.should == true | |
end | |
it "knows if he is dead" do | |
fighter = Fighter.new(hp: 0) | |
fighter.alive?.should == false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment