From 5f9abcbddfe5474601614daff66762e092e8ac95 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Tue, 8 Oct 2019 17:40:28 +0300 Subject: [PATCH] Add score calculation --- src/expansion.cr | 2 +- src/game/world.cr | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/expansion.cr b/src/expansion.cr index 270a8de..811405b 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -108,7 +108,7 @@ end def render_world(world) printf "Now:\n %s\n\n", render_time(world.ts) if world.win? - printf "YOU WIN!!!\n\n" + printf "YOU WIN!!! Score: %d\n\n", world.score end render_commands world printf "\n" diff --git a/src/game/world.cr b/src/game/world.cr index 82e1167..459b1dc 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -2,6 +2,7 @@ require "./resources" class Game::World property ts : Int64 + property win_ts : Int64? def initialize(@ts = 0_i64) @map = Map.new @@ -13,6 +14,7 @@ class Game::World getter resources getter map getter queue + getter win_ts def push(command : Command) dur = command.start(self) @@ -29,6 +31,9 @@ class Game::World command = item.command @ts = item.ts command.finish(self) + if win? + @win_ts = @ts + end end @ts = ts end @@ -36,4 +41,13 @@ class Game::World def win? @resources[ResourceType::Terraformation] >= 100 end + + def score + case @win_ts + when Int64 + Math.max(0, 3600_i64 - @win_ts.as(Int64)) + else + 0 + end + end end