From 927ca47b04a3dbae2b887e40901d63758b9cd193 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 6 Oct 2019 18:19:38 +0300 Subject: [PATCH] Show map always --- Makefile | 8 ++--- src/expansion.cr | 84 ++++++++++++++++++++++++++---------------------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index 4bcd78e..36c7a4e 100644 --- a/Makefile +++ b/Makefile @@ -14,14 +14,14 @@ build: mkdir -p build ./crystal build $(ENTRY_POINT) --release --no-debug --static -o build/$(APP_NAME) -.PHONY: run -run: format - ./crystal run $(ENTRY_POINT) - .PHONY: format format: ./crystal tool format ./src ./spec +.PHONY: run +run: format + ./crystal run $(ENTRY_POINT) + .PHONY: spec spec: format ./crystal spec --no-debug --warnings all --error-on-warnings --error-trace diff --git a/src/expansion.cr b/src/expansion.cr index c21ce10..beab965 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -10,40 +10,6 @@ world = Game::World.new(ts) router = CLI::CommandRouter.new -router.add "st" do - printf "Stat:\n Crystals: %d\n Terraformation: %d\n", - world.resources[Game::ResourceType::Crystal], - world.resources[Game::ResourceType::Terraformation] -end - -router.add "m" do - size = world.map.size - (0...size).each do |x| - if x == 0 - printf "+" - (0...size).each do |y| - printf "---+" - end - print "\n" - end - printf "|" - (0...size).each do |y| - printf "%c%d%d|", world.map.get(x, y).letter, x, y - end - print "\n" - printf "|" - (0...size).each do |y| - printf "%3d|", world.map.get(x, y).cur - end - print "\n" - printf "+" - (0...size).each do |y| - printf "---+" - end - print "\n" - end -end - router.add "q" do |p| items = world.queue.top(5) items.each do |i| @@ -73,12 +39,58 @@ router.add "terr {x} {y}" do |p| world.push(Game::BuildTerraformerCommand.new(point)) end +def render_map(world) + size = world.map.size + (0...size).each do |x| + if x == 0 + printf "+" + (0...size).each do |y| + printf "---+" + end + print "\n" + end + printf "|" + (0...size).each do |y| + printf "%c%d%d|", world.map.get(x, y).letter, x, y + end + print "\n" + printf "|" + (0...size).each do |y| + printf "%3d|", world.map.get(x, y).cur + end + print "\n" + printf "+" + (0...size).each do |y| + printf "---+" + end + print "\n" + end +end + +def render_resources(world) + printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n", + world.resources[Game::ResourceType::Crystal], + world.resources[Game::ResourceType::Terraformation] +end + +def render_world(world) + printf "Now: %s\n\n", Time.unix(world.ts).to_local.to_s + if world.win? + printf "YOU WIN!!!\n\n" + end + render_resources world + printf "\n" + render_map world + printf "\n" +end + def normalize_command(cmd) cmd.downcase.gsub(/\s+/, ' ').strip end printf "\u{001b}[2J" loop do + render_world world printf "In > " cmd = read_line() norm = normalize_command(cmd) @@ -88,10 +100,6 @@ loop do printf "\u{001b}[2J" current_time = Time.local.to_unix world.run current_time - printf "Now: %s\n\n", Time.unix(world.ts).to_local.to_s - if world.win? - printf "YOU WIN!!!\n\n" - end router.handle cmd printf "\n" end