1 from random import choice
3 from naja.constants import BITS, DIRECTION_BITS, CONDITION_BITS
6 class GameBoard(object):
8 A representation of the game board.
11 def __init__(self, player, health, wins, state=None):
15 state = self.generate_state(health, wins)
16 self.update_state(state)
20 'max_health': self.max_health,
21 'health': self.health,
22 'wins_required': self.wins_required,
24 'locations': self.export_locations(),
27 def export_locations(self):
29 (position, location.export())
30 for position, location in self.locations)
33 def generate_locations(cls):
34 # TODO: Generate some locations.
37 def generate_state(self, max_health, wins_required):
39 'max_health': max_health,
41 'wins_required': wins_required,
43 'locations': self.generate_locations(),
46 def update_state(self, state):
47 self.max_health = state['max_health']
48 self.wins_required = state['wins_required']
49 self.health = state['health']
50 self.wins = state['wins']
51 self.locations = self.import_locations(state['locations'])
53 def import_locations(self, locations):
55 (position, LocationCard.import_location(definition))
56 for position, definition in locations.iteritems())
58 def lose_health(self):
60 # TODO: Check win/lose
63 class LocationCard(object):
65 A particular set of options available on a location.
68 def __init__(self, bitwise_operand, actions):
69 self.bitwise_operand = bitwise_operand
70 self.actions = actions
73 def import_location(cls, state):
74 # TODO: Import real locations.
75 return cls(state['bitwise_operand'], [])
78 def new_location(cls, definition):
79 return cls.import_location({
80 'bitwise_operand': cls.generate_bitwise_operand(),
81 'actions': cls.build_actions(definition),
86 'bitwise_operand': self.bitwise_operand,
87 'actions': [action.export() for action in self.actions],
91 def build_actions(cls, definition):
92 raise NotImplementedError("TODO")
95 def generate_bitwise_operand():
97 Generate a set of two or three bits. At least one direction and one
98 condition bit will be included. There is a low probability of choosing
99 a third bit from the complete set.
102 bits.add(choice(DIRECTION_BITS.values()))
103 bits.add(choice(CONDITION_BITS.values()))
104 # One in three chance of adding a third bit, with a further one in four
105 # chance that it will match a bit already chosen.
106 if choice(range(3)) == 0:
107 bits.add(choice(BITS.values()))
108 return frozenset(bits)