1 from random import choice
3 from naja.constants import(
4 BITS, DIRECTION_BITS, CONDITION_BITS, PLAYER_DEFAULTS)
5 from naja.player import Player
6 from naja import actions
9 class GameBoard(object):
11 A representation of the game board.
14 def __init__(self, state, player, board_locations):
15 self.max_health = state['max_health']
16 self.wins_required = state['wins_required']
17 self.health = state['health']
18 self.wins = state['wins']
19 self.locations = self.import_locations(state['locations'])
21 self.board_locations = board_locations
24 def new_game(cls, locations_definition,
25 initial_bits=PLAYER_DEFAULTS.INITIAL_BITS,
26 initial_pos=PLAYER_DEFAULTS.INITIAL_POS,
27 max_health=PLAYER_DEFAULTS.MAX_HEALTH,
28 wins_required=PLAYER_DEFAULTS.WINS_REQUIRED):
30 'max_health': max_health,
32 'wins_required': wins_required,
34 'locations': locations_definition,
36 player = Player(initial_bits, initial_pos)
37 board_locations = cls.generate_board(locations_definition)
38 return cls(state, player, board_locations)
41 def import_game(cls, definition):
42 state = definition.copy()
43 player = Player.import_player(state.pop('player'))
44 board_locations = cls.import_board_locations(
45 state.pop('board_locations'))
46 return cls(state, player, board_locations)
50 'max_health': self.max_health,
51 'health': self.health,
52 'wins_required': self.wins_required,
54 'locations': self.export_locations(),
55 'player': self.player.export(),
56 'board_locations': self.export_board_locations(),
59 def export_locations(self):
60 return [location.export() for location in self.locations]
63 def import_locations(cls, locations_definition):
65 LocationCard.import_location(definition)
66 for definition in locations_definition]
68 def export_board_locations(self):
70 (position, location.export())
71 for position, location in self.board_locations)
74 def import_board_locations(cls, board_locations_definition):
76 (position, LocationCard.import_location(definition))
77 for position, definition in board_locations_definition.iteritems())
80 def generate_board(cls, locations_definition):
81 # TODO: Choose some locations.
84 def lose_health(self):
86 # TODO: Check win/lose
89 class LocationCard(object):
91 A particular set of options available on a location.
94 def __init__(self, bitwise_operand, location_actions):
95 self.bitwise_operand = bitwise_operand
96 self.actions = location_actions
99 def import_location(cls, state):
100 # TODO: Import real locations.
102 cls.build_action(definition) for definition in state['actions']]
103 return cls(state['bitwise_operand'], location_actions)
106 def build_action(cls, definition):
107 action_class = getattr(actions, definition['action_class'])
108 required_bits = definition['required_bits']
109 data = definition.get('data', {})
110 return action_class(required_bits, **data)
113 def new_location(cls, definition):
114 return cls.import_location({
115 'bitwise_operand': cls.generate_bitwise_operand(),
116 'actions': definition['actions'],
121 'bitwise_operand': self.bitwise_operand,
122 'actions': [action.export() for action in self.actions],
126 def generate_bitwise_operand():
128 Generate a set of two or three bits. At least one direction and one
129 condition bit will be included. There is a low probability of choosing
130 a third bit from the complete set.
133 bits.add(choice(DIRECTION_BITS.values()))
134 bits.add(choice(CONDITION_BITS.values()))
135 # One in three chance of adding a third bit, with a further one in four
136 # chance that it will match a bit already chosen.
137 if choice(range(3)) == 0:
138 bits.add(choice(BITS.values()))
139 return frozenset(bits)