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