Add score calculation

This commit is contained in:
Anton Vakhrushev 2019-10-08 17:40:28 +03:00
parent bc57cae1e1
commit 5f9abcbddf
2 changed files with 15 additions and 1 deletions

View File

@ -108,7 +108,7 @@ end
def render_world(world) def render_world(world)
printf "Now:\n %s\n\n", render_time(world.ts) printf "Now:\n %s\n\n", render_time(world.ts)
if world.win? if world.win?
printf "YOU WIN!!!\n\n" printf "YOU WIN!!! Score: %d\n\n", world.score
end end
render_commands world render_commands world
printf "\n" printf "\n"

View File

@ -2,6 +2,7 @@ require "./resources"
class Game::World class Game::World
property ts : Int64 property ts : Int64
property win_ts : Int64?
def initialize(@ts = 0_i64) def initialize(@ts = 0_i64)
@map = Map.new @map = Map.new
@ -13,6 +14,7 @@ class Game::World
getter resources getter resources
getter map getter map
getter queue getter queue
getter win_ts
def push(command : Command) def push(command : Command)
dur = command.start(self) dur = command.start(self)
@ -29,6 +31,9 @@ class Game::World
command = item.command command = item.command
@ts = item.ts @ts = item.ts
command.finish(self) command.finish(self)
if win?
@win_ts = @ts
end
end end
@ts = ts @ts = ts
end end
@ -36,4 +41,13 @@ class Game::World
def win? def win?
@resources[ResourceType::Terraformation] >= 100 @resources[ResourceType::Terraformation] >= 100
end end
def score
case @win_ts
when Int64
Math.max(0, 3600_i64 - @win_ts.as(Int64))
else
0
end
end
end end