changeset 392:bb75979b58e6

Move game_over logic from gameboard to level, for later reworking
author Neil Muller <drnlmuller@gmail.com>
date Sun, 01 Nov 2009 21:08:43 +0000
parents fd8da9241381
children 8104e82afd7a
files gamelib/engine.py gamelib/gameboard.py gamelib/gameover.py gamelib/level.py
diffstat 4 files changed, 69 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/engine.py	Thu Oct 29 21:09:22 2009 +0000
+++ b/gamelib/engine.py	Sun Nov 01 21:08:43 2009 +0000
@@ -199,7 +199,7 @@
             self.dialog=None
             return
         if events_equal(e, START_DAY):
-            if self.game.gameboard.is_game_over():
+            if self.game.level.is_game_over(self.game.gameboard):
                 return GameOver(self.game)
             return DayState(self.game)
         elif (e.type is KEYDOWN and e.key == K_d) or \
--- a/gamelib/gameboard.py	Thu Oct 29 21:09:22 2009 +0000
+++ b/gamelib/gameboard.py	Sun Nov 01 21:08:43 2009 +0000
@@ -767,10 +767,10 @@
 
     def advance_day(self):
         self.days += 1
-        if self.days == self.level.turn_limit:
+        if self.level.is_last_day(self.days):
             self.toolbar.day_counter.style.color = (255, 0, 0)
         self.toolbar.update_day_counter("%s/%s" % (self.days,
-            self.level.turn_limit if self.level.turn_limit > 0 else "-"))
+            self.level.get_max_turns()))
 
     def clear_foxes(self):
         for fox in self.foxes.copy():
@@ -963,15 +963,6 @@
         width, height = self.tv.size
         return len([(x,y) for x in range(width) for y in range(height) if self.tv.get((x,y)) == self.WOODLAND])
 
-    def is_game_over(self):
-        """Return true if we're complete"""
-        if self.trees_left() == 0:
-            return True
-        if self.level.turn_limit > 0 and self.days >= self.level.turn_limit:
-            return True
-        if len(self.chickens) == 0:
-            return True
-
 
 class TextDialog(gui.Dialog):
     def __init__(self, title, text, **params):
--- a/gamelib/gameover.py	Thu Oct 29 21:09:22 2009 +0000
+++ b/gamelib/gameover.py	Sun Nov 01 21:08:43 2009 +0000
@@ -85,7 +85,7 @@
 
         self.tr()
         made_list = scoreboard.check(score) is not None
-        if gameboard.is_game_over():
+        if level.is_game_over(gameboard):
             if len(gameboard.chickens) > 0:
                 self.survived = WON
                 scoreboard.submit(score, 'Player')
--- a/gamelib/level.py	Thu Oct 29 21:09:22 2009 +0000
+++ b/gamelib/level.py	Sun Nov 01 21:08:43 2009 +0000
@@ -6,46 +6,69 @@
 from ConfigParser import RawConfigParser
 
 class Level(object):
-   """Container for level details"""
+    """Container for level details"""
 
-   def __init__(self, level_name):
-      default_map = '%s.tga' % level_name
-      level_info = data.filepath('levels/%s.conf' % level_name)
-      # Load the level info file
-      # setup defaults
-      defaults = {
-              'map' : default_map,
-              'level name' : level_name,
-              'sell price chicken' : constants.DEFAULT_SELL_PRICE_CHICKEN,
-              'sell price egg' : constants.DEFAULT_SELL_PRICE_EGG,
-              'sell price dead fox' : constants.DEFAULT_SELL_PRICE_DEAD_FOX,
-              'turn limit' : constants.DEFAULT_TURN_LIMIT,
-              'goal' : constants.DEFAULT_GOAL_DESC,
-              'max foxes' : constants.DEFAULT_MAX_FOXES,
-              'min foxes' : 0,
-              'starting cash' : constants.DEFAULT_STARTING_CASH,
-              }
-      # Add default fox weightings
-      for animal, prob in DEFAULT_FOX_WEIGHTINGS:
-          defaults[animal.CONFIG_NAME] = prob
-      config = RawConfigParser(defaults)
-      config.read(level_info)
-      # NB. This assumes the level file is correctly formatted. No provision
-      # is made for missing sections or incorrectly specified values.
-      # i.e. Things may blow up
-      map_file = config.get('Level', 'map')
-      self.map = data.filepath('levels/%s' % map_file)
-      self.level_name = config.get('Level', 'level name')
-      self.goal = config.get('Level', 'goal')
-      self.turn_limit = config.getint('Level', 'turn limit')
-      self.max_foxes = config.getint('Game values', 'max foxes')
-      self.min_foxes = config.getint('Game values', 'min foxes')
-      self.sell_price_chicken = config.getint('Game values', 'sell price chicken')
-      self.sell_price_egg = config.getint('Game values', 'sell price egg')
-      self.sell_price_dead_fox = config.getint('Game values',
-              'sell price dead fox')
-      self.starting_cash = config.getint('Game values', 'starting cash')
-      self.fox_weightings = []
-      for animal, _prob in DEFAULT_FOX_WEIGHTINGS:
-          self.fox_weightings.append((animal, config.getint('Fox probablities',
-                  animal.CONFIG_NAME)))
+    def __init__(self, level_name):
+        default_map = '%s.tga' % level_name
+        level_info = data.filepath('levels/%s.conf' % level_name)
+        # Load the level info file
+        # setup defaults
+        defaults = {
+                'map' : default_map,
+                'level name' : level_name,
+                'sell price chicken' : constants.DEFAULT_SELL_PRICE_CHICKEN,
+                'sell price egg' : constants.DEFAULT_SELL_PRICE_EGG,
+                'sell price dead fox' : constants.DEFAULT_SELL_PRICE_DEAD_FOX,
+                'turn limit' : constants.DEFAULT_TURN_LIMIT,
+                'goal' : constants.DEFAULT_GOAL_DESC,
+                'max foxes' : constants.DEFAULT_MAX_FOXES,
+                'min foxes' : 0,
+                'starting cash' : constants.DEFAULT_STARTING_CASH,
+                }
+        # Add default fox weightings
+        for animal, prob in DEFAULT_FOX_WEIGHTINGS:
+            defaults[animal.CONFIG_NAME] = prob
+        config = RawConfigParser(defaults)
+        config.read(level_info)
+        # NB. This assumes the level file is correctly formatted. No provision
+        # is made for missing sections or incorrectly specified values.
+        # i.e. Things may blow up
+        map_file = config.get('Level', 'map')
+        self.map = data.filepath('levels/%s' % map_file)
+        self.level_name = config.get('Level', 'level name')
+        self.goal = config.get('Level', 'goal')
+        self.turn_limit = config.getint('Level', 'turn limit')
+        self.max_foxes = config.getint('Game values', 'max foxes')
+        self.min_foxes = config.getint('Game values', 'min foxes')
+        self.sell_price_chicken = config.getint('Game values', 'sell price chicken')
+        self.sell_price_egg = config.getint('Game values', 'sell price egg')
+        self.sell_price_dead_fox = config.getint('Game values',
+                'sell price dead fox')
+        self.starting_cash = config.getint('Game values', 'starting cash')
+        self.fox_weightings = []
+        for animal, _prob in DEFAULT_FOX_WEIGHTINGS:
+            self.fox_weightings.append((animal, config.getint('Fox probablities',
+                animal.CONFIG_NAME)))
+
+    # Utility functions, so we can make things more flexible later
+
+    def is_last_day(self, days):
+        """Check if we're the last day"""
+        return days == self.turn_limit
+
+    def get_max_turns(self):
+        """Get the display string for the turn limit"""
+        if self.turn_limit > 0:
+            return self.turn_limit
+        else:
+            return '-'
+
+    def is_game_over(self, gameboard):
+        """Check if the game is over"""
+        if gameboard.trees_left() == 0:
+            return True
+        if self.turn_limit > 0 and gameboard.days >= self.turn_limit:
+            return True
+        if len(gameboard.chickens) == 0:
+            return True
+