1 from naja.constants import BITS
4 class LocationAction(object):
6 An action that may be performed on a location.
12 def __init__(self, required_bits, **data):
14 for bit in required_bits:
15 # Convert names to numbers if applicable.
16 bits.add(BITS.get(bit, bit))
17 self.required_bits = frozenset(bits)
21 substitutions = self.data.copy()
22 if 'direction' in self.data:
23 substitutions['rowcol'] = {
28 }[self.data['direction']]
29 return self.TEXT % substitutions
31 def check_available(self, player):
32 return player.bits.check_bits(self.required_bits)
34 def perform_action(self, board, location):
35 raise NotImplementedError("TODO")
37 def check_and_clear_MSB(self, player):
38 if player.bits.check_bit(BITS.MSB):
39 player.bits.clear_bit(BITS.MSB)
45 return {'required_bits': list(self.required_bits),
47 'action_class': self.__class__.__name__}
50 class DoNothing(LocationAction):
53 def perform_action(self, board, location):
57 class LoseHealthOrMSB(LocationAction):
58 TEXT = "Lose health or MSB."
61 def perform_action(self, board, location):
62 if not self.check_and_clear_MSB(board.player):
66 class SetBits(LocationAction):
67 TEXT = "Set bits specified by this location."
69 def perform_action(self, board, location):
70 board.player.bits.set_bits(location.bitwise_operand)
73 class ToggleBits(LocationAction):
74 TEXT = "Toggle bits specified by this location."
76 def perform_action(self, board, location):
77 board.player.bits.toggle_bits(location.bitwise_operand)
80 class LoseHealthOrMSBAndSetBits(LocationAction):
81 TEXT = "Lose health or MSB, then set bits specified by this location."
84 def perform_action(self, board, location):
85 if not self.check_and_clear_MSB(board.player):
87 board.player.bits.set_bits(location.bitwise_operand)
90 class AcquireWinToken(LocationAction):
91 TEXT = "Acquire a win token, then clear all key bits."
93 def perform_action(self, board, location):
94 board.acquire_win_token()
95 board.player.bits.clear_bits(set([
96 BITS.RED, BITS.GREEN, BITS.BLUE,
100 class GainHealthAndClearBitsOrMSB(LocationAction):
101 TEXT = "Gain health, then clear bits specified by this location or MSB."
104 def perform_action(self, board, location):
106 if not self.check_and_clear_MSB(board.player):
107 board.player.bits.clear_bits(location.bitwise_operand)
110 class ShiftLocations(LocationAction):
111 TEXT = "Shift current %(rowcol)s %(direction)s."
113 def perform_action(self, board, location):
114 board.shift_locations(self.data['direction'])