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):
13 self.required_bits = required_bits
17 substitutions = self.data.copy()
18 if 'direction' in self.data:
19 substitutions['rowcol'] = {
24 }[self.data['direction']]
25 return self.TEXT % substitutions
27 def check_available(self, player):
28 return player.bits.check_bits(self.required_bits)
30 def perform_action(self, board, location):
31 raise NotImplementedError("TODO")
33 def check_and_clear_MSB(self, player):
34 if player.bits.check_bit(BITS.MSB):
35 player.bits.clear_bit(BITS.MSB)
41 return {'required_bits': list(self.required_bits),
43 'action_class': self.__class__.__name__}
46 class DoNothing(LocationAction):
49 def perform_action(self, board, location):
53 class LoseHealthOrMSB(LocationAction):
54 TEXT = "Lose health or MSB."
57 def perform_action(self, board, location):
58 if not self.check_and_clear_MSB(board.player):
62 class SetBits(LocationAction):
63 TEXT = "Set bits specified by this location."
65 def perform_action(self, board, location):
66 board.player.bits.set_bits(location.bitwise_operand)
69 class ToggleBits(LocationAction):
70 TEXT = "Toggle bits specified by this location."
72 def perform_action(self, board, location):
73 board.player.bits.toggle_bits(location.bitwise_operand)
76 class LoseHealthOrMSBAndSetBits(LocationAction):
77 TEXT = "Lose health or MSB, then set bits specified by this location."
80 def perform_action(self, board, location):
81 if not self.check_and_clear_MSB(board.player):
83 board.player.bits.set_bits(location.bitwise_operand)
86 class AcquireWinToken(LocationAction):
87 TEXT = "Acquire a win token, then clear all key bits."
89 def perform_action(self, board, location):
90 board.acquire_win_token()
91 board.player.bits.clear_bits(set([
92 BITS.RED, BITS.GREEN, BITS.BLUE,
96 class GainHealthAndClearBitsOrMSB(LocationAction):
97 TEXT = "Gain health, then clear bits specified by this location or MSB."
100 def perform_action(self, board, location):
102 if not self.check_and_clear_MSB(board.player):
103 board.player.bits.clear_bits(location.bitwise_operand)
106 class ShiftLocations(LocationAction):
107 TEXT = "Shift current %(rowcol)s %(direction)s."
109 def perform_action(self, board, location):
110 board.shift_locations(self.data['direction'])