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 = [item.copy() for item in 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.import_board_locations(
38 cls.generate_board(locations_definition))
39 return cls(state, player, board_locations)
42 def import_game(cls, definition):
43 state = definition.copy()
44 player = Player.import_player(state.pop('player'))
45 board_locations = cls.import_board_locations(
46 state.pop('board_locations'))
47 return cls(state, player, board_locations)
51 'max_health': self.max_health,
52 'health': self.health,
53 'wins_required': self.wins_required,
55 'locations': [item.copy() for item in self.locations],
56 'player': self.player.export(),
57 'board_locations': self.export_board_locations(),
61 def import_locations(cls, locations_definition):
63 LocationCard.import_location(definition)
64 for definition in locations_definition]
66 def export_board_locations(self):
68 (position, location.export())
69 for position, location in self.board_locations.iteritems())
72 def import_board_locations(cls, board_locations_definition):
74 (position, LocationCard.import_location(definition))
75 for position, definition in board_locations_definition.iteritems())
78 def generate_board(cls, locations_definition):
82 board_location = LocationCard.new_location(
83 choice(locations_definition).copy())
84 board_locations[(x, y)] = board_location.export()
85 return board_locations
87 def lose_health(self):
89 # TODO: Check win/lose
92 class LocationCard(object):
94 A particular set of options available on a location.
97 def __init__(self, bitwise_operand, location_actions):
98 self.bitwise_operand = bitwise_operand
99 self.actions = location_actions
102 def import_location(cls, state):
104 cls.build_action(definition) for definition in state['actions']]
105 return cls(state['bitwise_operand'], location_actions)
108 def build_action(cls, definition):
109 action_class = getattr(actions, definition['action_class'])
110 required_bits = definition['required_bits']
111 data = definition.get('data', {})
112 return action_class(required_bits, **data)
115 def new_location(cls, definition):
116 return cls.import_location({
117 'bitwise_operand': cls.generate_bitwise_operand(),
118 'actions': definition['actions'],
123 'bitwise_operand': self.bitwise_operand,
124 'actions': [action.export() for action in self.actions],
128 def generate_bitwise_operand():
130 Generate a set of two or three bits. At least one direction and one
131 condition bit will be included. There is a low probability of choosing
132 a third bit from the complete set.
135 bits.add(choice(DIRECTION_BITS.values()))
136 bits.add(choice(CONDITION_BITS.values()))
137 # One in three chance of adding a third bit, with a further one in four
138 # chance that it will match a bit already chosen.
139 if choice(range(3)) == 0:
140 bits.add(choice(BITS.values()))
141 return frozenset(bits)