Start of some game board stuff.
[naja.git] / naja / gameboard.py
1 from random import choice
2
3 from naja.constants import BITS, DIRECTION_BITS, CONDITION_BITS
4
5
6 class GameBoard(object):
7     """
8     A representation of the game board.
9     """
10
11     def __init__(self, player, health, wins, locations=None, state=None):
12         self.player = player
13         self.max_health = health
14         self.wins_required = wins
15
16         if locations is None:
17             locations = self.generate_locations()
18         self.locations = locations
19
20         if state is None:
21             state = self.generate_state()
22         self.update_state(state)
23
24     @classmethod
25     def generate_locations(cls):
26         raise NotImplementedError("TODO")
27
28     def generate_state(self):
29         return {
30             'health': self.max_health,
31             'wins': 0,
32         }
33
34
35 class LocationCard(object):
36     """
37     A particular set of options available on a location.
38     """
39
40     def __init__(self, bitwise_operation, bitwise_operand, actions):
41         self.bitwise_operation = bitwise_operation
42         self.bitwise_operand = bitwise_operand
43         self.actions = actions
44
45     @staticmethod
46     def generate_location():
47         raise NotImplementedError("TODO")
48
49     @staticmethod
50     def generate_bitwise_operand():
51         """
52         Generate a set of two or three bits. At least one direction and one
53         condition bit will be included. There is a low probability of choosing
54         a third bit from the complete set.
55         """
56         bits = set()
57         bits.add(choice(DIRECTION_BITS.values()))
58         bits.add(choice(CONDITION_BITS.values()))
59         # One in three chance of adding a third bit, with a further one in four
60         # chance that it will match a bit already chosen.
61         if choice(range(3)) == 0:
62             bits.add(choice(BITS.values()))
63         return frozenset(bits)
64
65     @staticmethod
66     def generate_location_actions():
67         raise NotImplementedError("TODO")
68
69     def apply_bitwise_operation(self, player):
70         operator = {
71             'SET': player.bits.set_bits,
72             'CLEAR': player.bits.clear_bits,
73             'TOGGLE': player.bits.toggle_bits,
74         }[self.bitwise_operation]
75         operator(self.bitwise_operand)
76
77
78 class LocationAction(object):
79     """
80     An action that may be performed on a location.
81     """
82
83     REQUIRED_BITS = frozenset()
84
85     def __init__(self, **data):
86         self.data = data
87
88     def check_available(self, player):
89         return player.bits.check_bits(self.REQUIRED_BITS)
90
91     def perform_action(self, player, board):
92         raise NotImplementedError("TODO")