More state management.
[naja.git] / naja / gameboard.py
1 from random import choice
2
3 from naja.constants import BITS, DIRECTION_BITS, CONDITION_BITS
4 from naja.player import Player
5 from naja import actions
6
7
8 class GameBoard(object):
9     """
10     A representation of the game board.
11     """
12
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'])
19         self.player = player
20         self.board_locations = board_locations
21
22     @classmethod
23     def new_game(cls, max_health, wins_required, locations_definition):
24         state = {
25             'max_health': max_health,
26             'health': max_health,
27             'wins_required': wins_required,
28             'wins': 0,
29             'locations': locations_definition,
30         }
31         player = Player(0x0f, (2, 2))
32         board_locations = cls.generate_board(locations_definition)
33         return cls(state, player, board_locations)
34
35     @classmethod
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)
42
43     def export(self):
44         return {
45             'max_health': self.max_health,
46             'health': self.health,
47             'wins_required': self.wins_required,
48             'wins': self.wins,
49             'locations': self.export_locations(),
50             'player': self.player.export(),
51             'board_locations': self.export_board_locations(),
52         }
53
54     def export_locations(self):
55         return [location.export() for location in self.locations]
56
57     @classmethod
58     def import_locations(cls, locations_definition):
59         return [
60             LocationCard.import_location(definition)
61             for definition in locations_definition]
62
63     def export_board_locations(self):
64         return dict(
65             (position, location.export())
66             for position, location in self.board_locations)
67
68     @classmethod
69     def import_board_locations(cls, board_locations_definition):
70         return dict(
71             (position, LocationCard.import_location(definition))
72             for position, definition in board_locations_definition.iteritems())
73
74     @classmethod
75     def generate_board(cls, locations_definition):
76         # TODO: Choose some locations.
77         return {}
78
79     def lose_health(self):
80         self.health -= 1
81         # TODO: Check win/lose
82
83
84 class LocationCard(object):
85     """
86     A particular set of options available on a location.
87     """
88
89     def __init__(self, bitwise_operand, location_actions):
90         self.bitwise_operand = bitwise_operand
91         self.actions = location_actions
92
93     @classmethod
94     def import_location(cls, state):
95         # TODO: Import real locations.
96         location_actions = [
97             cls.build_action(definition) for definition in state['actions']]
98         return cls(state['bitwise_operand'], location_actions)
99
100     @classmethod
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)
106
107     @classmethod
108     def new_location(cls, definition):
109         return cls.import_location({
110             'bitwise_operand': cls.generate_bitwise_operand(),
111             'actions': definition['actions'],
112         })
113
114     def export(self):
115         return {
116             'bitwise_operand': self.bitwise_operand,
117             'actions': [action.export() for action in self.actions],
118         }
119
120     @staticmethod
121     def generate_bitwise_operand():
122         """
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.
126         """
127         bits = 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)