From bd3b37e4cbe324c68abde257434b0dee3257163d Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Sat, 22 Feb 2014 20:48:32 +1100 Subject: winners --- robots/cursesviewer.py | 11 +++++++++-- robots/game.py | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/robots/cursesviewer.py b/robots/cursesviewer.py index 4dbe198..b37a06a 100644 --- a/robots/cursesviewer.py +++ b/robots/cursesviewer.py @@ -26,7 +26,7 @@ class CursesViewer: self.player_colours['*'] = 'white' def draw_board(self): - print(self.terminal.clear) + print(self.terminal.clear, end='') robots = {} for player, info in self.game.players.items(): @@ -64,9 +64,16 @@ class CursesViewer: def run(self): limiter = rate_limit(10) + self.draw_board() while not self.game.finished: next(limiter) - self.draw_board() self.game.next() + self.draw_board() + + winners = self.game.finished + if winners is True: + print('Game ended in a draw.') + else: + print('Game won by:', ', '.join(winners)) diff --git a/robots/game.py b/robots/game.py index 8fc507b..d87c495 100644 --- a/robots/game.py +++ b/robots/game.py @@ -99,7 +99,25 @@ class Game: @property def finished(self): - return False + target = int(round(self.available_tiles * 0.5)) + + winners = [] + roboteers = [] + + for whoami, info in self.players.items(): + n_tiles = self._painted_tiles[whoami] + if n_tiles > target: + winners.append(whoami) + + if info['robots']: + roboteers.append(whoami) + + won = bool(winners) + if len(roboteers) <= 1: + won = True + winners.extend(roboteers) + + return winners or won def call_bots(self): for whoami, bot in self.bots.items(): -- cgit v1.2.3