From 1bec9b987c424b0c625ce538cb81086ed33838da Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Mon, 16 Sep 2019 17:27:28 +0300 Subject: [PATCH] First test --- Makefile | 4 ++-- spec/village_spec.cr | 14 +++++++++++--- src/command.cr | 7 ++++--- src/map.cr | 16 +++++++--------- src/village.cr | 5 +---- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 972aff5..2d41ed0 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ format: ./cr tool format ./src ./spec .PHONY: spec -spec: - ./cr spec --warnings all --error-on-warnings +spec: format + ./cr spec --warnings all --error-on-warnings --time .PHONY: release release: diff --git a/spec/village_spec.cr b/spec/village_spec.cr index 14e8f93..b53eefc 100644 --- a/spec/village_spec.cr +++ b/spec/village_spec.cr @@ -1,5 +1,13 @@ -require "./spec_helper" +require "spec" +require "./../src/map" -describe Village do - # TODO: Write tests +describe Point do + p1 = Point.new(0, 0) + p2 = Point.new(5, 5) + it "can calc distance" do + p1.distance(p2).should eq 10 + end + it "can calc reverse destance" do + p2.distance(p1).should eq 10 + end end diff --git a/src/command.cr b/src/command.cr index 887369e..91f4245 100644 --- a/src/command.cr +++ b/src/command.cr @@ -25,6 +25,7 @@ end class GetWoodCommand < Command BASE_TIME = 10 BASE_WOOD = 80 + REST_TIME = 5 def initialize(@point : Point) @wood = 0 @@ -36,7 +37,7 @@ class GetWoodCommand < Command calc_time(wood_tile.as(Tile)) else printf " << no wood tile\n" - BASE_TIME + REST_TIME end end @@ -86,6 +87,7 @@ end class GrowWoodCommand < Command BASE_TIME = 15 BASE_WOOD = 30 + REST_TIME = 5 @wood_tile : Tile | Nil @@ -99,8 +101,7 @@ class GrowWoodCommand < Command calc_time(@wood_tile.as(Tile)) else printf " >> no wood tile\n" - @wood = 0 - BASE_TIME + REST_TIME end end diff --git a/src/map.cr b/src/map.cr index d2aa21c..8f31c46 100644 --- a/src/map.cr +++ b/src/map.cr @@ -1,17 +1,15 @@ struct Point + property x : Int32 + property y : Int32 + def initialize(@x : Int32, @y : Int32) end - def x - @x - end + getter x + getter y - def y - @y - end - - def distance(p : Point) : Int32 - return (p.x - @x).abs + (p.y - @y).abs + def distance(other) : Int32 + return (other.x - @x).abs + (other.y - @y).abs end end diff --git a/src/village.cr b/src/village.cr index 0c33662..5abf537 100644 --- a/src/village.cr +++ b/src/village.cr @@ -4,8 +4,7 @@ require "./map" require "./queue" class Resources - def initialize - @wood = 0 + def initialize(@wood = 0) end def add_wood(x) @@ -60,9 +59,7 @@ end w = World.new w.map.print -w.push(BuildWoodMillCommand.new(Point.new(2, 1))) w.push(BuildWoodMillCommand.new(Point.new(2, 3))) -w.push(BuildForesterHouseCommand.new(Point.new(2, 0))) w.push(BuildForesterHouseCommand.new(Point.new(1, 2))) w.push(BuildForesterHouseCommand.new(Point.new(3, 2))) w.run(120)