1 from random import choice
3 from naja.constants import BITS, DIRECTION_BITS, CONDITION_BITS
4 from naja.player import Player
5 from naja import actions
8 class GameBoard(object):
10 A representation of the game board.
13 def __init__(self, state, player, board_locations):
14 self.max_health = state['max_health']
15 self.wins_required = state['wins_required']
16 self.health = state['health']
17 self.wins = state['wins']
18 self.locations = self.import_locations(state['locations'])
20 self.board_locations = board_locations
23 def new_game(cls, max_health, wins_required, locations_definition):
25 'max_health': max_health,
27 'wins_required': wins_required,
29 'locations': locations_definition,
31 player = Player(0x0f, (2, 2))
32 board_locations = cls.generate_board(locations_definition)
33 return cls(state, player, board_locations)
36 def import_game(cls, definition):
37 state = definition.copy()
38 player = Player.import_player(state.pop('player'))
39 board_locations = cls.import_board_locations(
40 state.pop('board_locations'))
41 return cls(state, player, board_locations)
45 'max_health': self.max_health,
46 'health': self.health,
47 'wins_required': self.wins_required,
49 'locations': self.export_locations(),
50 'player': self.player.export(),
51 'board_locations': self.export_board_locations(),
54 def export_locations(self):
55 return [location.export() for location in self.locations]
58 def import_locations(cls, locations_definition):
60 LocationCard.import_location(definition)
61 for definition in locations_definition]
63 def export_board_locations(self):
65 (position, location.export())
66 for position, location in self.board_locations)
69 def import_board_locations(cls, board_locations_definition):
71 (position, LocationCard.import_location(definition))
72 for position, definition in board_locations_definition.iteritems())
75 def generate_board(cls, locations_definition):
76 # TODO: Choose some locations.
79 def lose_health(self):
81 # TODO: Check win/lose
84 class LocationCard(object):
86 A particular set of options available on a location.
89 def __init__(self, bitwise_operand, location_actions):
90 self.bitwise_operand = bitwise_operand
91 self.actions = location_actions
94 def import_location(cls, state):
95 # TODO: Import real locations.
97 cls.build_action(definition) for definition in state['actions']]
98 return cls(state['bitwise_operand'], location_actions)
101 def build_action(cls, definition):
102 action_class = getattr(actions, definition['action_class'])
103 required_bits = definition['required_bits']
104 data = definition.get('data', {})
105 return action_class(required_bits, **data)
108 def new_location(cls, definition):
109 return cls.import_location({
110 'bitwise_operand': cls.generate_bitwise_operand(),
111 'actions': definition['actions'],
116 'bitwise_operand': self.bitwise_operand,
117 'actions': [action.export() for action in self.actions],
121 def generate_bitwise_operand():
123 Generate a set of two or three bits. At least one direction and one
124 condition bit will be included. There is a low probability of choosing
125 a third bit from the complete set.
128 bits.add(choice(DIRECTION_BITS.values()))
129 bits.add(choice(CONDITION_BITS.values()))
130 # One in three chance of adding a third bit, with a further one in four
131 # chance that it will match a bit already chosen.
132 if choice(range(3)) == 0:
133 bits.add(choice(BITS.values()))
134 return frozenset(bits)