Merge branch 'master' of git+ssh://ctpug.org.za/naja
[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     def update_state(self, state):
35         self.health = state['health']
36         self.wins = state['wins']
37
38     def lose_health(self):
39         self.health -= 1
40         # TODO: Check win/lose
41
42
43 class LocationCard(object):
44     """
45     A particular set of options available on a location.
46     """
47
48     def __init__(self, bitwise_operand, actions):
49         self.bitwise_operand = bitwise_operand
50         self.actions = actions
51
52     @staticmethod
53     def generate_location():
54         raise NotImplementedError("TODO")
55
56     @staticmethod
57     def generate_bitwise_operand():
58         """
59         Generate a set of two or three bits. At least one direction and one
60         condition bit will be included. There is a low probability of choosing
61         a third bit from the complete set.
62         """
63         bits = set()
64         bits.add(choice(DIRECTION_BITS.values()))
65         bits.add(choice(CONDITION_BITS.values()))
66         # One in three chance of adding a third bit, with a further one in four
67         # chance that it will match a bit already chosen.
68         if choice(range(3)) == 0:
69             bits.add(choice(BITS.values()))
70         return frozenset(bits)
71
72     @staticmethod
73     def generate_location_actions():
74         raise NotImplementedError("TODO")
75
76
77 class LocationAction(object):
78     """
79     An action that may be performed on a location.
80     """
81
82     TEXT = None
83
84     def __init__(self, required_bits, **data):
85         self.required_bits = required_bits
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")
93
94     def check_and_clear_MSB(self, player):
95         if player.bits.check_bit(BITS.MSB):
96             player.bits.clear_bit(BITS.MSB)
97             return True
98         else:
99             return False