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(
37 "%s does not implement perform_action()." % (type(self).__name__,))
39 def check_and_clear_MSB(self, player):
40 if player.bits.check_bit(BITS.MSB):
41 player.bits.clear_bit(BITS.MSB)
47 return {'required_bits': list(self.required_bits),
49 'action_class': self.__class__.__name__}
52 class DoNothing(LocationAction):
55 def perform_action(self, board, location):
59 class LoseHealthOrMSB(LocationAction):
60 TEXT = "Lose HEALTH or MSB."
63 def perform_action(self, board, location):
64 if not self.check_and_clear_MSB(board.player):
68 class SetBits(LocationAction):
69 TEXT = "Set bits specified by this location."
71 def perform_action(self, board, location):
72 board.player.bits.set_bits(location.bitwise_operand)
75 class ToggleBits(LocationAction):
76 TEXT = "Toggle bits specified by this location."
78 def perform_action(self, board, location):
79 board.player.bits.toggle_bits(location.bitwise_operand)
82 class LoseHealthOrMSBAndSetBits(LocationAction):
83 TEXT = "Lose HEALTH or MSB, then set bits specified by this location."
86 def perform_action(self, board, location):
87 if not self.check_and_clear_MSB(board.player):
89 board.player.bits.set_bits(location.bitwise_operand)
92 class AcquireWinToken(LocationAction):
93 TEXT = "Gain WINTOKEN, then clear {RED,GREEN,BLUE}."
95 def perform_action(self, board, location):
96 board.acquire_win_token()
97 board.player.bits.clear_bits(set([
98 BITS.RED, BITS.GREEN, BITS.BLUE,
102 class GainHealthAndClearBitsOrMSB(LocationAction):
103 TEXT = "Gain HEALTH, then clear bits specified by this location or MSB."
106 def perform_action(self, board, location):
108 if not self.check_and_clear_MSB(board.player):
109 board.player.bits.clear_bits(location.bitwise_operand)
112 class ShiftLocations(LocationAction):
113 TEXT = "Shift current %(rowcol)s %(direction)s."
115 def perform_action(self, board, location):
116 board.shift_locations(self.data['direction'])
119 class AllowChessMove(LocationAction):
120 TEXT = "Move like a %(chesspiece_name)s for one turn."
122 def perform_action(self, board, location):
123 if self.data['chesspiece'] in CHESS_PIECES:
124 chesspiece = CHESS_PIECES[self.data['chesspiece']]
125 board.allow_chess_move(chesspiece)