started implementing rotation
[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     ACT, EXAMINE, ROTATION)
6 from naja.player import Player
7 from naja import actions
8
9
10 class GameBoard(object):
11     """
12     A representation of the game board.
13     """
14
15     def __init__(self, state, player, board_locations):
16         self.max_health = state['max_health']
17         self.wins_required = state['wins_required']
18         self.health = state['health']
19         self.wins = state['wins']
20         self.locations = [item.copy() for item in state['locations']]
21         self.player = player
22         self.board_locations = board_locations
23         self.player_mode = EXAMINE
24
25     @classmethod
26     def new_game(cls, locations_definition,
27                  initial_bits=PLAYER_DEFAULTS.INITIAL_BITS,
28                  initial_pos=PLAYER_DEFAULTS.INITIAL_POS,
29                  max_health=PLAYER_DEFAULTS.MAX_HEALTH,
30                  wins_required=PLAYER_DEFAULTS.WINS_REQUIRED):
31         state = {
32             'max_health': max_health,
33             'health': max_health,
34             'wins_required': wins_required,
35             'wins': 0,
36             'locations': locations_definition,
37         }
38         player = Player(initial_bits, initial_pos)
39         board_locations = cls.import_board_locations(
40             cls.generate_board(locations_definition))
41         return cls(state, player, board_locations)
42
43     @classmethod
44     def import_game(cls, definition):
45         state = definition.copy()
46         player = Player.import_player(state.pop('player'))
47         board_locations = cls.import_board_locations(
48             state.pop('board_locations'))
49         return cls(state, player, board_locations)
50
51     def export(self):
52         return {
53             'max_health': self.max_health,
54             'health': self.health,
55             'wins_required': self.wins_required,
56             'wins': self.wins,
57             'locations': [item.copy() for item in self.locations],
58             'player': self.player.export(),
59             'board_locations': self.export_board_locations(),
60         }
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.iteritems())
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         board_locations = {}
82         for x in range(5):
83             for y in range(5):
84                 board_location = LocationCard.new_location(
85                     choice(locations_definition).copy())
86                 board_locations[(x, y)] = board_location.export()
87         return board_locations
88
89     def lose_health(self):
90         self.health -= 1
91         if self.health <= 0:
92             self.end_game(win=False)
93
94     def gain_health(self):
95         if self.health < self.max_health:
96             self.health += 1
97
98     def acquire_win_token(self):
99         self.wins += 1
100         if self.wins >= self.wins_required:
101             self.end_game(win=True)
102
103     def replace_card(self, position):
104         location = LocationCard.new_location(choice(self.locations).copy())
105         self.board_locations[position] = location
106
107     def shift_location_row(self, change, is_vertical):
108         px, py = self.player.position
109         shifted_locations = {}
110         mkpos = lambda i: (px, i) if is_vertical else (i, py)
111
112         for i in range(5):
113             if (px, py) == mkpos(i):
114                 continue
115             new_i = (i + change) % 5
116             if (px, py) == mkpos(new_i):
117                 new_i = (new_i + change) % 5
118             shifted_locations[mkpos(new_i)] = self.board_locations[mkpos(i)]
119
120         self.board_locations.update(shifted_locations)
121
122     def shift_locations(self, direction):
123         if BITS[direction] == BITS.NORTH:
124             self.shift_location_row(-1, is_vertical=True)
125         elif BITS[direction] == BITS.SOUTH:
126             self.shift_location_row(1, is_vertical=True)
127         elif BITS[direction] == BITS.EAST:
128             self.shift_location_row(1, is_vertical=False)
129         elif BITS[direction] == BITS.WEST:
130             self.shift_location_row(-1, is_vertical=False)
131
132     def rotate_locations(self, direction):
133         px, py = self.player.position
134         locations_to_rotate = []
135         rotated_locations = {}
136
137         if py > 0:
138             for i in range(max(0, px -1), min(5, px + 2)):
139                 locations_to_rotate.append((i, py - 1))
140
141         if px > 0:
142             locations_to_rotate.append((px - 1, py))
143
144         if px < 4:
145             locations_to_rotate.append((px + 1, py))
146
147         if py < 4:
148             for i in range(max(0, px -1), min(5, px + 2)):
149                 locations_to_rotate.append((i, py + 1))
150
151         if direction == ROTATION.CLOCKWISE:
152             new_positions = locations_to_rotate[1:] + locations_to_rotate[0]
153         elif direction == ROTATION.ANTICLOCKWISE:
154             new_positions = locations_to_rotate[-1] + locations_to_rotate[:-1]
155
156         for old, new in zip(locations_to_rotate, new_positions):
157             rotated_locations[old] = self.board_locations[new]
158
159         self.board_locations.update(rotated_locations)
160
161     def allow_chess_move(self, chesspiece):
162         self.player.allow_chess_move(chesspiece)
163
164     def change_mode(self, new_mode):
165         """Advance to the next mode"""
166         if new_mode == self.player_mode:
167             raise RuntimeError("Inconsistent state. Setting mode %s to itself"
168                                % self.player_mode)
169         elif new_mode in (ACT, EXAMINE):
170             self.player_mode = new_mode
171         else:
172             raise RuntimeError("Illegal player mode %s" % self.player_mode)
173
174     def end_game(self, win):
175         # TODO: Find a way to not have UI stuff in game logic stuff.
176         from naja.events import SceneChangeEvent
177         from naja.scenes.lose import LoseScene
178         from naja.scenes.win import WinScene
179         if win:
180             SceneChangeEvent.post(WinScene)
181         else:
182             SceneChangeEvent.post(LoseScene)
183
184
185 class LocationCard(object):
186     """
187     A particular set of options available on a location.
188     """
189
190     def __init__(self, bitwise_operand, location_actions):
191         self.bitwise_operand = bitwise_operand
192         self.actions = location_actions
193         self.check_actions()
194
195     @classmethod
196     def import_location(cls, state):
197         location_actions = [
198             cls.build_action(definition) for definition in state['actions']]
199         return cls(state['bitwise_operand'], location_actions)
200
201     @classmethod
202     def build_action(cls, definition):
203         action_class = getattr(actions, definition['action_class'])
204         required_bits = cls.parse_bits(definition['required_bits'])
205         data = definition.get('data', {})
206         return action_class(required_bits, **data)
207
208     @classmethod
209     def new_location(cls, definition):
210         if 'bits' in definition:
211             bits = cls.parse_bits(definition['bits'])
212         else:
213             bits = cls.generate_bitwise_operand()
214         return cls.import_location({
215             'bitwise_operand': bits,
216             'actions': definition['actions'],
217         })
218
219     @classmethod
220     def parse_bits(self, bit_list):
221         # Convert names to numbers if applicable.
222         return frozenset(BITS.get(bit, bit) for bit in bit_list)
223
224     def export(self):
225         return {
226             'bitwise_operand': self.bitwise_operand,
227             'actions': [action.export() for action in self.actions],
228         }
229
230     def check_actions(self):
231         if not self.actions:
232             print "Warning: Location has no actions."
233             self.insert_default_default_action()
234         if self.actions[0].required_bits:
235             self.insert_default_default_action()
236
237     def insert_default_default_action(self):
238         self.actions.insert(0, self.build_action({
239             'action_class': 'DoNothing',
240             'required_bits': [],
241         }))
242
243     @staticmethod
244     def generate_bitwise_operand():
245         """
246         Generate a set of two or three bits. At least one direction and one
247         condition bit will be included. There is a low probability of choosing
248         a third bit from the complete set.
249         """
250         bits = set()
251         bits.add(choice(DIRECTION_BITS.values()))
252         bits.add(choice(CONDITION_BITS.values()))
253         # One in three chance of adding a third bit, with a further one in four
254         # chance that it will match a bit already chosen.
255         if choice(range(3)) == 0:
256             bits.add(choice(BITS.values()))
257         return frozenset(bits)