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)
20 def check_available(self, player):
21 return player.bits.check_bits(self.required_bits)
23 def perform_action(self, board, location):
24 raise NotImplementedError("TODO")
26 def check_and_clear_MSB(self, player):
27 if player.bits.check_bit(BITS.MSB):
28 player.bits.clear_bit(BITS.MSB)
34 return {'required_bits': list(self.required_bits),
36 'action_class': self.__class__.__name__}
39 class DoNothing(LocationAction):
42 def perform_action(self, board, location):
46 class LoseHealthOrMSB(LocationAction):
47 TEXT = "Lose health or MSB."
50 def perform_action(self, board, location):
51 if not self.check_and_clear_MSB(board.player):
55 class SetBits(LocationAction):
56 TEXT = "Set bits specified by this location."
58 def perform_action(self, board, location):
59 board.player.bits.set_bits(location.bitwise_operand)
62 class ToggleBits(LocationAction):
63 TEXT = "Toggle bits specified by this location."
65 def perform_action(self, board, location):
66 board.player.bits.toggle_bits(location.bitwise_operand)
69 class LoseHealthOrMSBAndSetBits(LocationAction):
70 TEXT = "Lose health or MSB, then set bits specified by this location."
73 def perform_action(self, board, location):
74 if not self.check_and_clear_MSB(board.player):
76 board.player.bits.set_bits(location.bitwise_operand)
79 class AcquireWinToken(LocationAction):
80 TEXT = "Acquire a win token, then clear all high bits."
83 def perform_action(self, board, location):
84 if self.check_and_clear_MSB(board.player):
85 board.acquire_win_token()
86 board.player.bits.clear_bits(set([
87 BITS.CYAN, BITS.MAGENTA, BITS.YELLOW,
91 class GainHealthAndClearBitsOrMSB(LocationAction):
92 TEXT = "Gain health, then clear bits specified by this location or MSB."
95 def perform_action(self, board, location):
97 if not self.check_and_clear_MSB(board.player):
98 board.player.bits.clear_bits(location.bitwise_operand)
101 class ShiftLocations(LocationAction):
102 # TODO: Direction parameter in TEXT.
103 TEXT = "Shift board locations."
105 def perform_action(self, board, location):
106 board.shift_locations(self.data['direction'])