1 from naja.constants import BITS, CHESS_PIECES
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()
19 if 'direction' in self.data:
20 substitutions['rowcol'] = {
25 }[self.data['direction']]
27 if 'chesspiece' in self.data:
28 substitutions['chesspiece_name'] = self.data['chesspiece'].lower()
30 return self.TEXT % substitutions
32 def check_available(self, player):
33 return player.bits.check_bits(self.required_bits)
35 def perform_action(self, board, location):
36 raise NotImplementedError("TODO")
38 def check_and_clear_MSB(self, player):
39 if player.bits.check_bit(BITS.MSB):
40 player.bits.clear_bit(BITS.MSB)
46 return {'required_bits': list(self.required_bits),
48 'action_class': self.__class__.__name__}
51 class DoNothing(LocationAction):
54 def perform_action(self, board, location):
58 class LoseHealthOrMSB(LocationAction):
59 TEXT = "Lose health or MSB."
62 def perform_action(self, board, location):
63 if not self.check_and_clear_MSB(board.player):
67 class SetBits(LocationAction):
68 TEXT = "Set bits specified by this location."
70 def perform_action(self, board, location):
71 board.player.bits.set_bits(location.bitwise_operand)
74 class ToggleBits(LocationAction):
75 TEXT = "Toggle bits specified by this location."
77 def perform_action(self, board, location):
78 board.player.bits.toggle_bits(location.bitwise_operand)
81 class LoseHealthOrMSBAndSetBits(LocationAction):
82 TEXT = "Lose health or MSB, then set bits specified by this location."
85 def perform_action(self, board, location):
86 if not self.check_and_clear_MSB(board.player):
88 board.player.bits.set_bits(location.bitwise_operand)
91 class AcquireWinToken(LocationAction):
92 TEXT = "Acquire a win token, then clear all key bits."
94 def perform_action(self, board, location):
95 board.acquire_win_token()
96 board.player.bits.clear_bits(set([
97 BITS.RED, BITS.GREEN, BITS.BLUE,
101 class GainHealthAndClearBitsOrMSB(LocationAction):
102 TEXT = "Gain health, then clear bits specified by this location or MSB."
105 def perform_action(self, board, location):
107 if not self.check_and_clear_MSB(board.player):
108 board.player.bits.clear_bits(location.bitwise_operand)
111 class ShiftLocations(LocationAction):
112 TEXT = "Shift current %(rowcol)s %(direction)s."
114 def perform_action(self, board, location):
115 board.shift_locations(self.data['direction'])
118 class AllowChessMove(LocationAction) :
119 TEXT = "Move like a %(chesspiece_name)s for one turn."
121 def perform_action(self, board, location):
122 if self.data['chesspiece'] in CHESS_PIECES:
123 chesspiece = CHESS_PIECES[self.data['chesspiece']]
124 board.allow_chess_move(chesspiece)