diff --git a/spec/expansion_spec.cr b/spec/expansion_spec.cr index 81c82ff..b9d50c0 100644 --- a/spec/expansion_spec.cr +++ b/spec/expansion_spec.cr @@ -2,9 +2,9 @@ require "./spec_helper" require "./../src/game/map" require "./../src/game/queue" -describe Point do - p1 = Point.new(0, 0) - p2 = Point.new(5, 5) +describe Game::Point do + p1 = Game::Point.new(0, 0) + p2 = Game::Point.new(5, 5) it "can calc distance" do p1.distance(p2).should eq 10 end diff --git a/spec/queue_spec.cr b/spec/queue_spec.cr index 80191e0..4fdd075 100644 --- a/spec/queue_spec.cr +++ b/spec/queue_spec.cr @@ -16,32 +16,32 @@ end define_dummy_classes(5) -describe App::Queue do +describe Game::Queue do it "should pop nil on empty queue" do - queue = App::Queue.new + queue = Game::Queue.new item = queue.pop(50) item.should be_nil end it "should pop command on one element queue" do - queue = App::Queue.new + queue = Game::Queue.new queue.push(10, Test::DummyCommand1.new) item = queue.pop(50) item.nil?.should be_false - item.as(App::Queue::Item).ts.should eq 10 - item.as(App::Queue::Item).command.should be_a(Test::DummyCommand1) + item.as(Game::Queue::Item).ts.should eq 10 + item.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) end it "should pop commands in proper order" do - queue = App::Queue.new + queue = Game::Queue.new queue.push(10, Test::DummyCommand1.new) queue.push(50, Test::DummyCommand2.new) queue.push(30, Test::DummyCommand3.new) item1 = queue.pop(100) - item1.as(App::Queue::Item).command.should be_a(Test::DummyCommand1) + item1.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) item2 = queue.pop(100) - item2.as(App::Queue::Item).command.should be_a(Test::DummyCommand3) + item2.as(Game::Queue::Item).command.should be_a(Test::DummyCommand3) item3 = queue.pop(100) - item3.as(App::Queue::Item).command.should be_a(Test::DummyCommand2) + item3.as(Game::Queue::Item).command.should be_a(Test::DummyCommand2) end end diff --git a/spec/world_spec.cr b/spec/world_spec.cr index b509e50..81139eb 100644 --- a/spec/world_spec.cr +++ b/spec/world_spec.cr @@ -4,18 +4,18 @@ require "../src/game/world" describe "World" do it "should build crystal harvester" do world = Game::World.new - point = Point.new(2, 3) + point = Game::Point.new(2, 3) cmd = Game::BuildCrystalHarvesterCommand.new(point) world.push(cmd) world.run(100) - world.map.get(point).has_role(TileRole::CrystalHarvester) + world.map.get(point).has_role(Game::TileRole::CrystalHarvester) end it "should fail when not enought resources" do world = Game::World.new - point = Point.new(2, 3) + point = Game::Point.new(2, 3) cmd = Game::BuildCrystalRestorerCommand.new(point) - expect_raises(NotEnoughtResources) do + expect_raises(Game::NotEnoughtResources) do world.push(cmd) end end diff --git a/src/expansion.cr b/src/expansion.cr index 69d4d6c..336cce4 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -12,8 +12,8 @@ router = CLI::CommandRouter.new router.add "st" do printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n", world.ts, - world.resources[ResourceType::Crystal], - world.resources[ResourceType::Terraformation] + world.resources[Game::ResourceType::Crystal], + world.resources[Game::ResourceType::Terraformation] end router.add "m" do @@ -29,20 +29,20 @@ end router.add "harv {x} {y}" do |p| x = p["x"].to_i32 y = p["y"].to_i32 - world.push(Game::BuildCrystalHarvesterCommand.new(Point.new(x, y))) + world.push(Game::BuildCrystalHarvesterCommand.new(Game::Point.new(x, y))) printf "Build harvester at %d %d\n", x, y end router.add "rest {x} {y}" do |p| x = p["x"].to_i32 y = p["y"].to_i32 - world.push(Game::BuildCrystalRestorerCommand.new(Point.new(x, y))) + world.push(Game::BuildCrystalRestorerCommand.new(Game::Point.new(x, y))) end router.add "terr {x} {y}" do |p| x = p["x"].to_i32 y = p["y"].to_i32 - world.push(Game::BuildTerraformerCommand.new(Point.new(x, y))) + world.push(Game::BuildTerraformerCommand.new(Game::Point.new(x, y))) end def normalize_command(cmd) diff --git a/src/game/exception.cr b/src/game/exception.cr index 9dcf0f3..2c8e0ab 100644 --- a/src/game/exception.cr +++ b/src/game/exception.cr @@ -1,2 +1,4 @@ -class NotEnoughtResources < Exception +module Game + class NotEnoughtResources < Exception + end end diff --git a/src/game/map.cr b/src/game/map.cr index ee3fe4a..7e6c270 100644 --- a/src/game/map.cr +++ b/src/game/map.cr @@ -1,81 +1,83 @@ -struct Point - property x : Int32 - property y : Int32 +module Game + struct Point + property x : Int32 + property y : Int32 - def initialize(@x : Int32, @y : Int32) - end - - getter x - getter y - - def distance(other) : Int32 - return (other.x - @x).abs + (other.y - @y).abs - end -end - -class Map - SIZE = 4 - - def initialize - @data = {} of String => Tile - (0...SIZE).each do |x| - (0...SIZE).each do |y| - self.set(PlateauTile.new(Point.new(x, y))) - end + def initialize(@x : Int32, @y : Int32) end - self.set(MainBaseTile.new(Point.new(0, 0))) - self.set(CrystalTile.new(Point.new(1, 1), 100)) - self.set(CrystalTile.new(Point.new(3, 1), 200)) - self.set(CrystalTile.new(Point.new(2, 2), 100)) - end - def get(point : Point) : Tile - @data[key(point)] - end + getter x + getter y - def set(tile : Tile) - @data[key(tile.point)] = tile - end - - def set(point : Point, tile : Tile) - @data[key(point)] = tile - end - - def tiles - (0...SIZE).each do |x| - (0...SIZE).each do |y| - point = Point.new(x, y) - tile = self.get(point) - yield point, tile - end + def distance(other) : Int32 + return (other.x - @x).abs + (other.y - @y).abs end end - def nearest_tile(point : Point, &block) : Tile | Nil - seek_tile = nil - min_dist = Int32::MAX - tiles do |tile_point, tile| - if (yield tile) - tile_dist = tile_point.distance(point) - if tile_dist < min_dist - min_dist = tile_dist - seek_tile = tile + class Map + SIZE = 4 + + def initialize + @data = {} of String => Tile + (0...SIZE).each do |x| + (0...SIZE).each do |y| + self.set(PlateauTile.new(Point.new(x, y))) + end + end + self.set(MainBaseTile.new(Point.new(0, 0))) + self.set(CrystalTile.new(Point.new(1, 1), 100)) + self.set(CrystalTile.new(Point.new(3, 1), 200)) + self.set(CrystalTile.new(Point.new(2, 2), 100)) + end + + def get(point : Point) : Tile + @data[key(point)] + end + + def set(tile : Tile) + @data[key(tile.point)] = tile + end + + def set(point : Point, tile : Tile) + @data[key(point)] = tile + end + + def tiles + (0...SIZE).each do |x| + (0...SIZE).each do |y| + point = Point.new(x, y) + tile = self.get(point) + yield point, tile end end end - seek_tile - end - def print - (0...SIZE).each do |x| - (0...SIZE).each do |y| - printf "%c", @data[key(Point.new(x, y))].letter + def nearest_tile(point : Point, &block) : Tile | Nil + seek_tile = nil + min_dist = Int32::MAX + tiles do |tile_point, tile| + if (yield tile) + tile_dist = tile_point.distance(point) + if tile_dist < min_dist + min_dist = tile_dist + seek_tile = tile + end + end end - printf "\n" + seek_tile + end + + def print + (0...SIZE).each do |x| + (0...SIZE).each do |y| + printf "%c", @data[key(Point.new(x, y))].letter + end + printf "\n" + end + end + + private def key(p : Point) : String + return sprintf "%d:%d", p.x, p.y end end - - private def key(p : Point) : String - return sprintf "%d:%d", p.x, p.y - end end diff --git a/src/game/queue.cr b/src/game/queue.cr index 38d5734..be5c175 100644 --- a/src/game/queue.cr +++ b/src/game/queue.cr @@ -1,4 +1,4 @@ -class App::Queue +class Game::Queue struct Item def initialize(@ts : Int32, @command : Game::Command) end diff --git a/src/game/resources.cr b/src/game/resources.cr index 6da26ec..e1bcd08 100644 --- a/src/game/resources.cr +++ b/src/game/resources.cr @@ -1,11 +1,11 @@ require "./exception" -enum ResourceType +enum Game::ResourceType Crystal Terraformation end -class Resources +class Game::Resources def initialize @values = {} of ResourceType => Int32 ResourceType.each do |t| diff --git a/src/game/tile.cr b/src/game/tile.cr index a301cf1..2120ac2 100644 --- a/src/game/tile.cr +++ b/src/game/tile.cr @@ -1,104 +1,106 @@ -enum TileRole - CrystalDeposits - CrystalHarvester - CrystalRestorer - Plateau - Terraformer - Warehouse -end - -abstract class Tile - property cap : Int32 = 0 - property cur : Int32 = 0 - - def initialize(@point : Point) +module Game + enum TileRole + CrystalDeposits + CrystalHarvester + CrystalRestorer + Plateau + Terraformer + Warehouse end - getter point - getter cap - getter cur + abstract class Tile + property cap : Int32 = 0 + property cur : Int32 = 0 - abstract def letter : Char - abstract def has_role(role : TileRole) : Bool + def initialize(@point : Point) + end - def withdraw(value) - if value >= @cur - wd = @cur - @cur = 0 - wd - else - @cur -= value - value + getter point + getter cap + getter cur + + abstract def letter : Char + abstract def has_role(role : TileRole) : Bool + + def withdraw(value) + if value >= @cur + wd = @cur + @cur = 0 + wd + else + @cur -= value + value + end + end + + def charge(value) + charged = @cur + value + @cur = charged <= @cap ? charged : @cap end end - def charge(value) - charged = @cur + value - @cur = charged <= @cap ? charged : @cap - end -end - -class PlateauTile < Tile - def letter : Char - '.' - end - - def has_role(role : TileRole) : Bool - role == TileRole::Plateau - end -end - -class MainBaseTile < Tile - def letter : Char - 'H' - end - - def has_role(role : TileRole) : Bool - role == TileRole::Warehouse - end -end - -class CrystalTile < Tile - def initialize(@point : Point, cap : Int32) - @cap = cap - @cur = cap - end - - def letter : Char - 'f' - end - - def has_role(role : TileRole) : Bool - role == TileRole::CrystalDeposits - end -end - -class CrystalHarvesterTile < Tile - def letter : Char - 'm' - end - - def has_role(role : TileRole) : Bool - role == TileRole::CrystalHarvester - end -end - -class CrystalRestorerTile < Tile - def letter : Char - 'h' - end - - def has_role(role : TileRole) : Bool - role == TileRole::CrystalRestorer - end -end - -class TerraformerTile < Tile - def letter : Char - 'T' - end - - def has_role(role : TileRole) : Bool - role == TileRole::Terraformer + class PlateauTile < Tile + def letter : Char + '.' + end + + def has_role(role : TileRole) : Bool + role == TileRole::Plateau + end + end + + class MainBaseTile < Tile + def letter : Char + 'H' + end + + def has_role(role : TileRole) : Bool + role == TileRole::Warehouse + end + end + + class CrystalTile < Tile + def initialize(@point : Point, cap : Int32) + @cap = cap + @cur = cap + end + + def letter : Char + 'f' + end + + def has_role(role : TileRole) : Bool + role == TileRole::CrystalDeposits + end + end + + class CrystalHarvesterTile < Tile + def letter : Char + 'm' + end + + def has_role(role : TileRole) : Bool + role == TileRole::CrystalHarvester + end + end + + class CrystalRestorerTile < Tile + def letter : Char + 'h' + end + + def has_role(role : TileRole) : Bool + role == TileRole::CrystalRestorer + end + end + + class TerraformerTile < Tile + def letter : Char + 'T' + end + + def has_role(role : TileRole) : Bool + role == TileRole::Terraformer + end end end diff --git a/src/game/world.cr b/src/game/world.cr index c4e8247..af6c38b 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -6,7 +6,7 @@ class Game::World def initialize(@ts = 0) @map = Map.new @resources = Resources.new - @tasks = App::Queue.new + @tasks = Queue.new end getter ts