Adjust save slot positioning.
[naja.git] / naja / gameboard.py
index b827877b43d3bc530abc4bd3eb768fbba7de6b25..2ed511afeb81eab12875286c3506899ecb49ec9b 100644 (file)
@@ -30,20 +30,47 @@ class GameBoard(object):
         self.replacement_params = state.get('replacement_params', None)
 
     @classmethod
-    def new_game(cls, deck,
-                 initial_bits=PLAYER_DEFAULTS.INITIAL_BITS,
-                 initial_pos=PLAYER_DEFAULTS.INITIAL_POS,
-                 max_health=PLAYER_DEFAULTS.MAX_HEALTH,
-                 wins_required=PLAYER_DEFAULTS.WINS_REQUIRED):
+    def new_game(cls, deck, initial_bits=None, initial_pos=None,
+                 max_health=None, wins_required=None):
+
+        defaults = {
+            'initial_bits': PLAYER_DEFAULTS.INITIAL_BITS,
+            'initial_pos': PLAYER_DEFAULTS.INITIAL_POS,
+            'max_health': PLAYER_DEFAULTS.MAX_HEALTH,
+            'wins_required': PLAYER_DEFAULTS.WINS_REQUIRED,
+        }
+
+        puzzle = deck.get('puzzle', False)
+
+        if puzzle:
+            puzzle_defaults = deck.get('defaults', {})
+            for k, v in puzzle_defaults.iteritems():
+                if isinstance(v, list):
+                    puzzle_defaults[k] = [int(x) for x in v]
+                else:
+                    puzzle_defaults[k] = int(v)
+            defaults.update(puzzle_defaults)
+
+        if initial_bits is None:
+            initial_bits = defaults['initial_bits']
+        if initial_pos is None:
+            initial_pos = defaults['initial_pos']
+        if max_health is None:
+            max_health = defaults['max_health']
+        if wins_required is None:
+            wins_required = defaults['wins_required']
+
+        # Overriden by command line
         if options.initial_bits:
             initial_bits = options.initial_bits
+
         state = {
             'max_health': max_health,
             'health': max_health,
             'wins_required': wins_required,
             'wins': 0,
             'locations': deck['cards'],
-            'puzzle': deck.get('puzzle', False),
+            'puzzle': puzzle,
             'clock_count': 0,
             'replacement_params': deck.get('replacement_params', None),
         }
@@ -74,6 +101,8 @@ class GameBoard(object):
             'clock_count': self.clock_count,
             'replacement_params': self.replacement_params,
         }
+        if options.cheat_enabled:
+            self.has_cheated = True
         if self.has_cheated:
             data['cheater'] = True
         return data
@@ -222,7 +251,8 @@ class GameBoard(object):
         if ROTATION[direction] == ROTATION.CLOCKWISE:
             new_positions = locations_to_rotate[1:] + [locations_to_rotate[0]]
         elif ROTATION[direction] == ROTATION.ANTICLOCKWISE:
-            new_positions = ([locations_to_rotate[-1]] + locations_to_rotate[:-1])
+            new_positions = (
+                [locations_to_rotate[-1]] + locations_to_rotate[:-1])
 
         for old, new in zip(locations_to_rotate, new_positions):
             rotated_locations[new] = self.board_locations[old]
@@ -362,8 +392,8 @@ class LocationCard(object):
             return None
         else:
             if replacement_params['chance'] > random.random():
-                return random.randint(replacement_params[0],
-                                      replacement_params[1])
+                return random.randint(replacement_params['min'],
+                                      replacement_params['max'])
             else:
                 return None
 
@@ -372,4 +402,3 @@ class LocationCard(object):
             self.replacement_time -= 1
             if self.replacement_time <= 0:
                 board.replace_card(position)
-