X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fgameboard.py;h=fa768dbad5db3481820d1775f277a2e37cb83d1c;hb=ea0fea7855b70665b1b155f1d20495e88096b73a;hp=a107b16315416a1deb6cce27e74e1abc9b9216bf;hpb=eccb327f1260b9aa981755840e097d5b23af05a9;p=naja.git diff --git a/naja/gameboard.py b/naja/gameboard.py index a107b16..fa768db 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -8,42 +8,87 @@ class GameBoard(object): A representation of the game board. """ - def __init__(self, player, health, wins, locations=None, state=None): + def __init__(self, player, health, wins, state=None): self.player = player - self.max_health = health - self.wins_required = wins - - if locations is None: - locations = self.generate_locations() - self.locations = locations if state is None: - state = self.generate_state() + state = self.generate_state(health, wins) self.update_state(state) + def export(self): + return { + 'max_health': self.max_health, + 'health': self.health, + 'wins_required': self.wins_required, + 'wins': self.wins, + 'locations': self.export_locations(), + } + + def export_locations(self): + return dict( + (position, location.export()) + for position, location in self.locations) + @classmethod def generate_locations(cls): - raise NotImplementedError("TODO") + # TODO: Generate some locations. + return {} - def generate_state(self): + def generate_state(self, max_health, wins_required): return { - 'health': self.max_health, + 'max_health': max_health, + 'health': max_health, + 'wins_required': wins_required, 'wins': 0, + 'locations': self.generate_locations(), } + def update_state(self, state): + self.max_health = state['max_health'] + self.wins_required = state['wins_required'] + self.health = state['health'] + self.wins = state['wins'] + self.locations = self.import_locations(state['locations']) + + def import_locations(self, locations): + return dict( + (position, LocationCard.import_location(definition)) + for position, definition in locations.iteritems()) + + def lose_health(self): + self.health -= 1 + # TODO: Check win/lose + class LocationCard(object): """ A particular set of options available on a location. """ - def __init__(self, bitwise_operation, bitwise_operand, actions): - self.bitwise_operation = bitwise_operation + def __init__(self, bitwise_operand, actions): self.bitwise_operand = bitwise_operand self.actions = actions - @staticmethod - def generate_location(): + @classmethod + def import_location(cls, state): + # TODO: Import real locations. + return cls(state['bitwise_operand'], []) + + @classmethod + def new_location(cls, definition): + return cls.import_location({ + 'bitwise_operand': cls.generate_bitwise_operand(), + 'actions': cls.build_actions(definition), + }) + + def export(self): + return { + 'bitwise_operand': self.bitwise_operand, + 'actions': [action.export() for action in self.actions], + } + + @classmethod + def build_actions(cls, definition): raise NotImplementedError("TODO") @staticmethod @@ -61,32 +106,3 @@ class LocationCard(object): if choice(range(3)) == 0: bits.add(choice(BITS.values())) return frozenset(bits) - - @staticmethod - def generate_location_actions(): - raise NotImplementedError("TODO") - - def apply_bitwise_operation(self, player): - operator = { - 'SET': player.bits.set_bits, - 'CLEAR': player.bits.clear_bits, - 'TOGGLE': player.bits.toggle_bits, - }[self.bitwise_operation] - operator(self.bitwise_operand) - - -class LocationAction(object): - """ - An action that may be performed on a location. - """ - - REQUIRED_BITS = frozenset() - - def __init__(self, **data): - self.data = data - - def check_available(self, player): - return player.bits.check_bits(self.REQUIRED_BITS) - - def perform_action(self, player, board): - raise NotImplementedError("TODO")