Default default action.
[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     MOVE, ACT)
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 = MOVE
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         # TODO: Check win/lose
92
93     def change_mode(self):
94         """Advance to the next mode"""
95         if self.player_mode == MOVE:
96             self.player_mode = ACT
97         elif self.player_mode == ACT:
98             self.player_mode = MOVE
99         else:
100             raise RuntimeError("Illegal player mode %s" % self.player_mode)
101
102
103 class LocationCard(object):
104     """
105     A particular set of options available on a location.
106     """
107
108     def __init__(self, bitwise_operand, location_actions):
109         self.bitwise_operand = bitwise_operand
110         self.actions = location_actions
111         self.check_actions()
112
113     @classmethod
114     def import_location(cls, state):
115         location_actions = [
116             cls.build_action(definition) for definition in state['actions']]
117         return cls(state['bitwise_operand'], location_actions)
118
119     @classmethod
120     def build_action(cls, definition):
121         action_class = getattr(actions, definition['action_class'])
122         required_bits = definition['required_bits']
123         data = definition.get('data', {})
124         return action_class(required_bits, **data)
125
126     @classmethod
127     def new_location(cls, definition):
128         return cls.import_location({
129             'bitwise_operand': cls.generate_bitwise_operand(),
130             'actions': definition['actions'],
131         })
132
133     def export(self):
134         return {
135             'bitwise_operand': self.bitwise_operand,
136             'actions': [action.export() for action in self.actions],
137         }
138
139     def check_actions(self):
140         if not self.actions:
141             print "Warning: Location has no actions."
142             self.insert_default_default_action()
143         if self.actions[0].required_bits:
144             self.insert_default_default_action()
145
146     def insert_default_default_action(self):
147         self.actions.insert(0, self.build_action({
148             'action_class': 'DoNothing',
149             'required_bits': [],
150         }))
151
152     @staticmethod
153     def generate_bitwise_operand():
154         """
155         Generate a set of two or three bits. At least one direction and one
156         condition bit will be included. There is a low probability of choosing
157         a third bit from the complete set.
158         """
159         bits = set()
160         bits.add(choice(DIRECTION_BITS.values()))
161         bits.add(choice(CONDITION_BITS.values()))
162         # One in three chance of adding a third bit, with a further one in four
163         # chance that it will match a bit already chosen.
164         if choice(range(3)) == 0:
165             bits.add(choice(BITS.values()))
166         return frozenset(bits)