Add a non-empty set of locations for testing
[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 = [item.copy() for item in 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.import_board_locations(
38             cls.generate_board(locations_definition))
39         return cls(state, player, board_locations)
40
41     @classmethod
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)
48
49     def export(self):
50         return {
51             'max_health': self.max_health,
52             'health': self.health,
53             'wins_required': self.wins_required,
54             'wins': self.wins,
55             'locations': [item.copy() for item in self.locations],
56             'player': self.player.export(),
57             'board_locations': self.export_board_locations(),
58         }
59
60     @classmethod
61     def import_locations(cls, locations_definition):
62         return [
63             LocationCard.import_location(definition)
64             for definition in locations_definition]
65
66     def export_board_locations(self):
67         return dict(
68             (position, location.export())
69             for position, location in self.board_locations.iteritems())
70
71     @classmethod
72     def import_board_locations(cls, board_locations_definition):
73         return dict(
74             (position, LocationCard.import_location(definition))
75             for position, definition in board_locations_definition.iteritems())
76
77     @classmethod
78     def generate_board(cls, locations_definition):
79         board_locations = {}
80         for x in range(5):
81             for y in range(5):
82                 board_location = LocationCard.new_location(
83                     choice(locations_definition).copy())
84                 board_locations[(x, y)] = board_location.export()
85         return board_locations
86
87     def lose_health(self):
88         self.health -= 1
89         # TODO: Check win/lose
90
91
92 class LocationCard(object):
93     """
94     A particular set of options available on a location.
95     """
96
97     def __init__(self, bitwise_operand, location_actions):
98         self.bitwise_operand = bitwise_operand
99         self.actions = location_actions
100
101     @classmethod
102     def import_location(cls, state):
103         location_actions = [
104             cls.build_action(definition) for definition in state['actions']]
105         return cls(state['bitwise_operand'], location_actions)
106
107     @classmethod
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)
113
114     @classmethod
115     def new_location(cls, definition):
116         return cls.import_location({
117             'bitwise_operand': cls.generate_bitwise_operand(),
118             'actions': definition['actions'],
119         })
120
121     def export(self):
122         return {
123             'bitwise_operand': self.bitwise_operand,
124             'actions': [action.export() for action in self.actions],
125         }
126
127     @staticmethod
128     def generate_bitwise_operand():
129         """
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.
133         """
134         bits = 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)