Skip to content

Commit

Permalink
Move tiles into a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Mar 10, 2011
1 parent 1e3e442 commit cff3769
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 91 deletions.
6 changes: 1 addition & 5 deletions lib/ivory_tower.rb
@@ -1,6 +1,2 @@
require_relative "ivory_tower/allowed_units"
require_relative "ivory_tower/base"
require_relative "ivory_tower/rally_point"
require_relative "ivory_tower/meadow"
require_relative "ivory_tower/mountain"
require_relative "ivory_tower/forest"
require_relative "ivory_tower/tile"
21 changes: 0 additions & 21 deletions lib/ivory_tower/base.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/ivory_tower/forest.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/ivory_tower/meadow.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/ivory_tower/mountain.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/ivory_tower/rally_point.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/ivory_tower/tile.rb
@@ -0,0 +1,31 @@
require_relative "tile/base"
require_relative "tile/rally_point"
require_relative "tile/meadow"
require_relative "tile/mountain"
require_relative "tile/forest"

module IvoryTower
module Tile
extend self

def base(*a,&b)
IvoryTower::Tile::Base.new(*a,&b)
end

def rally_point(*a, &b)
IvoryTower::Tile::RallyPoint.new(*a,&b)
end

def meadow(*a, &b)
IvoryTower::Tile::Meadow.new(*a,&b)
end

def mountain(*a, &b)
IvoryTower::Tile::Meadow.new(*a,&b)
end

def forest(*a, &b)
IvoryTower::Tile::Forest.new(*a,&b)
end
end
end
23 changes: 23 additions & 0 deletions lib/ivory_tower/tile/base.rb
@@ -0,0 +1,23 @@
module IvoryTower
module Tile
class Base
extend AllowedUnits::ClassMethods

allows :air_units, :ground_units, :sea_units

def initialize(health)
@health = health
end

attr_accessor :health

def <<(monster)
@health -= 1
end

def destroyed?
@health == 0
end
end
end
end
8 changes: 8 additions & 0 deletions lib/ivory_tower/tile/forest.rb
@@ -0,0 +1,8 @@
module IvoryTower
module Tile
class Forest
include AllowedUnits
allows :ground_units, :air_units, :towers
end
end
end
8 changes: 8 additions & 0 deletions lib/ivory_tower/tile/meadow.rb
@@ -0,0 +1,8 @@
module IvoryTower
module Tile
class Meadow
include AllowedUnits
allows :ground_units, :air_units
end
end
end
8 changes: 8 additions & 0 deletions lib/ivory_tower/tile/mountain.rb
@@ -0,0 +1,8 @@
module IvoryTower
module Tile
class Mountain
include AllowedUnits
allows :air_units
end
end
end
28 changes: 28 additions & 0 deletions lib/ivory_tower/tile/rally_point.rb
@@ -0,0 +1,28 @@
module IvoryTower
module Tile
class RallyPoint
include AllowedUnits
allows :ground_units, :sea_units, :air_units

def initialize(name)
@name = name
end

attr_reader :name

def <<(monster)
occupants << monster

self
end

def delete(monster)
occupants.delete(monster)
end

def empty?
occupants.empty?
end
end
end
end
12 changes: 6 additions & 6 deletions test/base_test.rb
Expand Up @@ -4,12 +4,12 @@
include AllowedUnits::TestHelpers

test "should have an initial health" do
base = IvoryTower::Base.new(10)
base = IvoryTower::Tile.base(10)
assert_equal 10, base.health
end

test "should have its health reduced each time a monster is added" do
base = IvoryTower::Base.new(10)
base = IvoryTower::Tile.base(10)

base << new_monster
assert_equal 9, base.health
Expand All @@ -19,14 +19,14 @@
end

test "should be destroyed when health is reduced to zero" do
base = IvoryTower::Base.new(1)
base = IvoryTower::Tile.base(1)

base << new_monster
assert base.destroyed?
end

test "should not be destroyed when health is positive" do
base = IvoryTower::Base.new(2)
base = IvoryTower::Tile.base(2)

refute base.destroyed?

Expand All @@ -36,13 +36,13 @@
end

test "should allow air, ground, and sea units" do
base = IvoryTower::Base.new(10)
base = IvoryTower::Tile.base(10)

assert_allows_units(base, :air_units, :ground_units, :sea_units)
end

test "should not allow towers" do
base = IvoryTower::Base.new(10)
base = IvoryTower::Tile.base(10)

refute_allows_towers(base)
end
Expand Down
6 changes: 3 additions & 3 deletions test/forest_test.rb
Expand Up @@ -4,18 +4,18 @@
include AllowedUnits::TestHelpers

test "should initially be unoccupied" do
forest = IvoryTower::Forest.new
forest = IvoryTower::Tile.forest
refute forest.occupied?
end

test "should allow air and ground units" do
forest = IvoryTower::Forest.new
forest = IvoryTower::Tile.forest

assert_allows_units(forest, :air_units, :ground_units)
end

test "should allow towers" do
forest = IvoryTower::Forest.new
forest = IvoryTower::Tile.forest

assert_allows_towers(forest)
end
Expand Down
6 changes: 3 additions & 3 deletions test/meadow_test.rb
Expand Up @@ -4,18 +4,18 @@
include AllowedUnits::TestHelpers

test "should initially be unoccupied" do
meadow = IvoryTower::Meadow.new
meadow = IvoryTower::Tile.meadow
refute meadow.occupied?
end

test "should allow air and ground units" do
meadow = IvoryTower::Meadow.new
meadow = IvoryTower::Tile.meadow

assert_allows_units(meadow, :air_units, :ground_units)
end

test "should not allow towers" do
meadow = IvoryTower::Meadow.new
meadow = IvoryTower::Tile.meadow

refute_allows_towers(meadow)
end
Expand Down
6 changes: 3 additions & 3 deletions test/mountain_test.rb
Expand Up @@ -4,18 +4,18 @@
include AllowedUnits::TestHelpers

test "should initially be unoccupied" do
mountain = IvoryTower::Mountain.new
mountain = IvoryTower::Tile.mountain
refute mountain.occupied?
end

test "should allow air units" do
mountain = IvoryTower::Mountain.new
mountain = IvoryTower::Tile.mountain

assert_allows_units(mountain, :air_units)
end

test "should not allow towers" do
mountain = IvoryTower::Mountain.new
mountain = IvoryTower::Tile.mountain

refute_allows_towers(mountain)
end
Expand Down
12 changes: 6 additions & 6 deletions test/rally_point_test.rb
Expand Up @@ -4,24 +4,24 @@
include AllowedUnits::TestHelpers

test "should have a name" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
assert_equal "1", rally_point.name
end

test "should be empty initially" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
assert rally_point.empty?
end

test "should not be empty once occupants are added" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
rally_point << new_monster

refute rally_point.empty?
end

test "should be able to remove an occupant" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
monster1 = new_monster
monster2 = new_monster

Expand All @@ -37,14 +37,14 @@
end

test "should allow air, ground, and sea units" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
assert_allows_units rally_point, :air_units,
:ground_units,
:sea_units
end

test "should not allow towers" do
rally_point = IvoryTower::RallyPoint.new("1")
rally_point = IvoryTower::Tile.rally_point("1")
refute_allows_towers(rally_point)
end

Expand Down

0 comments on commit cff3769

Please sign in to comment.