Implement static puzzle deck construction.
authorSimon Cross <hodgestar@gmail.com>
Fri, 16 May 2014 20:36:02 +0000 (22:36 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 16 May 2014 20:36:12 +0000 (22:36 +0200)
naja/gameboard.py
naja/gamestate.py

index 8b0e099a77da9ebe02d6d2f58d314aec1a8a9572..a2e37796d7972025c077ef88e4b96f0641bb09a5 100644 (file)
@@ -19,12 +19,13 @@ class GameBoard(object):
         self.health = state['health']
         self.wins = state['wins']
         self.locations = [item.copy() for item in state['locations']]
+        self.puzzle = state['puzzle']
         self.player = player
         self.board_locations = board_locations
         self.player_mode = EXAMINE
 
     @classmethod
-    def new_game(cls, locations_definition,
+    def new_game(cls, deck,
                  initial_bits=PLAYER_DEFAULTS.INITIAL_BITS,
                  initial_pos=PLAYER_DEFAULTS.INITIAL_POS,
                  max_health=PLAYER_DEFAULTS.MAX_HEALTH,
@@ -36,11 +37,12 @@ class GameBoard(object):
             'health': max_health,
             'wins_required': wins_required,
             'wins': 0,
-            'locations': locations_definition,
+            'locations': deck['cards'],
+            'puzzle': deck.get('puzzle', False),
         }
         player = Player(initial_bits, initial_pos)
         board_locations = cls.import_board_locations(
-            cls.generate_board(locations_definition))
+            cls.generate_board(deck))
         return cls(state, player, board_locations)
 
     @classmethod
@@ -58,6 +60,7 @@ class GameBoard(object):
             'wins_required': self.wins_required,
             'wins': self.wins,
             'locations': [item.copy() for item in self.locations],
+            'puzzle': self.puzzle,
             'player': self.player.export(),
             'board_locations': self.export_board_locations(),
         }
@@ -80,12 +83,29 @@ class GameBoard(object):
             for position, definition in board_locations_definition)
 
     @classmethod
-    def generate_board(cls, locations_definition):
+    def generate_board(cls, deck):
+        if deck.get('puzzle', False):
+            return cls.generate_puzzle_board(deck)
+        else:
+            return cls.generate_random_board(deck)
+
+    @classmethod
+    def generate_puzzle_board(cls, deck):
+        assert len(deck['cards']) == 5 * 5
+        board_locations = [
+            [(i % 5, i // 5),
+             LocationCard.new_location(card.copy()).export()]
+            for i, card in enumerate(deck['cards'])
+        ]
+        return board_locations
+
+    @classmethod
+    def generate_random_board(cls, deck):
         board_locations = []
         for x in range(5):
             for y in range(5):
                 board_location = LocationCard.new_location(
-                    choice(locations_definition).copy())
+                    choice(deck['cards']).copy())
                 board_locations.append([(x, y), board_location.export()])
         return board_locations
 
index c3bc0ce96b75c27bd14bbe49b3daad3c20ee0ef9..19051a42b8c1d450d4f4b43a0662610f4ad56825 100644 (file)
@@ -23,8 +23,8 @@ class GameState(object):
 
     @classmethod
     def new(cls, deck='standard', **kw):
-        locations_deck = load_location_deck(deck)
-        return cls(GameBoard.new_game(locations_deck['cards'], **kw))
+        deck = load_location_deck(deck)
+        return cls(GameBoard.new_game(deck, **kw))
 
     @classmethod
     def load(cls, data):