From: Simon Cross Date: Fri, 16 May 2014 20:36:02 +0000 (+0200) Subject: Implement static puzzle deck construction. X-Git-Tag: 0.1~183 X-Git-Url: https://git.ctpug.org.za/?p=naja.git;a=commitdiff_plain;h=aa858388f65344396a7b81a82198e3a6fbc1efca Implement static puzzle deck construction. --- diff --git a/naja/gameboard.py b/naja/gameboard.py index 8b0e099..a2e3779 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -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 diff --git a/naja/gamestate.py b/naja/gamestate.py index c3bc0ce..19051a4 100644 --- a/naja/gamestate.py +++ b/naja/gamestate.py @@ -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):