X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fgameboard.py;h=18ecaf5c62d369af2f8a3d32a0493b45b0926923;hb=87a82ccb10b6f7b07c48f8e9993243b9592e00b4;hp=8b0e099a77da9ebe02d6d2f58d314aec1a8a9572;hpb=bc80496f4c94c07e4ce636e7a2f7345c548f5374;p=naja.git diff --git a/naja/gameboard.py b/naja/gameboard.py index 8b0e099..18ecaf5 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 @@ -103,6 +123,10 @@ class GameBoard(object): if self.wins >= self.wins_required: self.end_game(win=True) + def card_used(self, position): + if not self.puzzle: + self.replace_card(position) + def replace_card(self, position): location = LocationCard.new_location(choice(self.locations).copy()) self.board_locations[position] = location