Decomposition

This commit is contained in:
Anton Vakhrushev 2019-09-13 18:31:38 +03:00
parent 5a717b5961
commit 60d5b60153
2 changed files with 85 additions and 6 deletions

10
src/queue.cr Normal file
View File

@ -0,0 +1,10 @@
class App::CommandQueue
def initialize
end
def push(ts : Int32, cmd : Command)
end
def pop(ts : Int32) : Command | Nil
end
end

View File

@ -51,9 +51,76 @@ class GetWoodCommand < Command
end end
end end
abstract class Tile
abstract def letter : Char
end
class GrassTile < Tile
def letter : Char
'.'
end
end
class WoodTile < Tile
def initialize(cap : Int32)
@cap = cap
@cur = cap
end
def cap
@cap
end
def inc(v : Int32)
@cur += v
if @cur < 0
@cur = 0
end
if @cur > @cap
@cur = @cap
end
end
def letter : Char
'f'
end
end
class Map
def initialize
@data = {} of String => Tile
(0...4).each do |x|
(0...4).each do |y|
@data[key(x, y)] = GrassTile.new
end
end
@data[key(1, 1)] = WoodTile.new(100)
@data[key(3, 1)] = WoodTile.new(200)
@data[key(2, 2)] = WoodTile.new(100)
end
def get(x : Int32, y : Int32) : Tile
@data[key(x, y)]
end
def print
(0...4).each do |x|
(0...4).each do |y|
printf "%c", @data[key(x, y)].letter
end
printf "\n"
end
end
private def key(x : Int32, y : Int32) : String
return sprintf "%d:%d", x, y
end
end
class World class World
def initialize def initialize
@resources = Resources.new @resources = Resources.new
@map = Map.new
@queue = Array(Command).new @queue = Array(Command).new
end end
@ -61,6 +128,10 @@ class World
@resources @resources
end end
def map
@map
end
def push(command : Command) : Bool def push(command : Command) : Bool
if !command.supports?(self) if !command.supports?(self)
return false return false
@ -86,9 +157,7 @@ class World
end end
end end
q = World.new w = World.new
q.push(BuildMillCommand.new(0)) w.map.print
q.push(BuildMillCommand.new(0)) w.push(BuildMillCommand.new(2))
q.push(BuildMillCommand.new(0)) w.run(10)
q.push(BuildMillCommand.new(2))
q.run(10)