From: Jeremy Thurgood Date: Sun, 11 May 2014 15:52:57 +0000 (+0200) Subject: Decouple action from required bits. X-Git-Tag: 0.1~419^2 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=ede149141d8dcbd6bed5bf3ff710d2f716ac8421;p=naja.git Decouple action from required bits. --- diff --git a/naja/gameboard.py b/naja/gameboard.py index 5cbecb9..9c5af7b 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -80,13 +80,13 @@ class LocationAction(object): """ TEXT = None - REQUIRED_BITS = frozenset() - def __init__(self, **data): + def __init__(self, required_bits, **data): + self.required_bits = required_bits self.data = data def check_available(self, player): - return player.bits.check_bits(*self.REQUIRED_BITS) + return player.bits.check_bits(self.required_bits) def perform_action(self, player, board): raise NotImplementedError("TODO") diff --git a/naja/player.py b/naja/player.py index acae919..fcfc330 100644 --- a/naja/player.py +++ b/naja/player.py @@ -33,18 +33,18 @@ class PlayerBits(object): # Operate on sets of bits - def check_bits(self, *bits): + def check_bits(self, bits): return all(self.check_bit(bit) for bit in bits) - def set_bits(self, *bits): + def set_bits(self, bits): for bit in bits: self.set_bit(bit) - def clear_bits(self, *bits): + def clear_bits(self, bits): for bit in bits: self.clear_bit(bit) - def toggle_bits(self, *bits): + def toggle_bits(self, bits): for bit in bits: self.toggle_bit(bit) diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index bf04e21..8896b8d 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -10,10 +10,10 @@ class TestActions(TestCase): def test_DoNothing_check_available(self): player = self.make_player() - action = actions.DoNothing() + action = actions.DoNothing(set()) self.assertEqual(action.check_available(player), True) def test_LoseHealthOrMSB_check_available(self): player = self.make_player() - action = actions.LoseHeathOrMSB() + action = actions.LoseHeathOrMSB(set()) self.assertEqual(action.check_available(player), True) diff --git a/naja/tests/test_player.py b/naja/tests/test_player.py index 8373eee..d2b896e 100644 --- a/naja/tests/test_player.py +++ b/naja/tests/test_player.py @@ -52,35 +52,35 @@ class TestPlayerBits(TestCase): def test_check_bits(self): bits = PlayerBits(0x03) - self.assertEqual(bits.check_bits(BITS.NORTH), True) - self.assertEqual(bits.check_bits(BITS.NORTH, BITS.SOUTH), True) - self.assertEqual(bits.check_bits(BITS.CYAN), False) - self.assertEqual(bits.check_bits(BITS.CYAN, BITS.MSB), False) + self.assertEqual(bits.check_bits([BITS.NORTH]), True) + self.assertEqual(bits.check_bits([BITS.NORTH, BITS.SOUTH]), True) + self.assertEqual(bits.check_bits([BITS.CYAN]), False) + self.assertEqual(bits.check_bits([BITS.CYAN, BITS.MSB]), False) self.assertEqual( - bits.check_bits(BITS.NORTH, BITS.SOUTH, BITS.CYAN), False) + bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.CYAN]), False) def test_set_bits(self): bits = PlayerBits(0x03) self.assertEqual(bits._bits, 0x03) - bits.set_bits(BITS.NORTH, BITS.CYAN) + bits.set_bits([BITS.NORTH, BITS.CYAN]) self.assertEqual(bits._bits, 0x13) - bits.set_bits(BITS.NORTH, BITS.CYAN, BITS.MSB) + bits.set_bits([BITS.NORTH, BITS.CYAN, BITS.MSB]) self.assertEqual(bits._bits, 0x93) def test_clear_bits(self): bits = PlayerBits(0x03) self.assertEqual(bits._bits, 0x03) - bits.clear_bits(BITS.NORTH, BITS.CYAN) + bits.clear_bits([BITS.NORTH, BITS.CYAN]) self.assertEqual(bits._bits, 0x02) - bits.clear_bits(BITS.NORTH, BITS.CYAN, BITS.MSB) + bits.clear_bits([BITS.NORTH, BITS.CYAN, BITS.MSB]) self.assertEqual(bits._bits, 0x02) def test_toggle_bits(self): bits = PlayerBits(0x03) self.assertEqual(bits._bits, 0x03) - bits.toggle_bits(BITS.NORTH, BITS.CYAN) + bits.toggle_bits([BITS.NORTH, BITS.CYAN]) self.assertEqual(bits._bits, 0x12) - bits.toggle_bits(BITS.NORTH, BITS.CYAN) + bits.toggle_bits([BITS.NORTH, BITS.CYAN]) self.assertEqual(bits._bits, 0x03) - bits.toggle_bits(BITS.NORTH, BITS.CYAN, BITS.MSB) + bits.toggle_bits([BITS.NORTH, BITS.CYAN, BITS.MSB]) self.assertEqual(bits._bits, 0x92)