--- /dev/null
+
+
+class PlayerBits(object):
+ """
+ A glorified byte.
+ """
+
+ def __init__(self, bits):
+ self.bits = bits
+
+ @property
+ def bits(self):
+ return self._bits
+
+ @bits.setter
+ def bits(self, value):
+ assert 0 <= value <= 0xff
+ self._bits = value
+
+ # Operate on individual bits
+
+ def check_bit(self, bit):
+ return bool(self.bits & (1 << bit))
+
+ def set_bit(self, bit):
+ self.bits |= (1 << bit)
+
+ def clear_bit(self, bit):
+ self.bits &= (0xff ^ (1 << bit))
+
+ def toggle_bit(self, bit):
+ self.bits ^= (1 << bit)
+
+ # Operate on sets of bits
+
+ def check_bits(self, *bits):
+ return all(self.check_bit(bit) for bit in bits)
+
+ def set_bits(self, *bits):
+ for bit in bits:
+ self.set_bit(bit)
+
+ def clear_bits(self, *bits):
+ for bit in bits:
+ self.clear_bit(bit)
+
+ def toggle_bits(self, *bits):
+ for bit in bits:
+ self.toggle_bit(bit)
+
+
+class Player(object):
+ """
+ A representation of the player.
+ """
+
+ def __init__(self, bits, position):
+ self.bits = PlayerBits(bits)
+ self.position = position
--- /dev/null
+from unittest import TestCase
+
+from naja.constants import NORTH, SOUTH, CYAN, MSB
+from naja.player import PlayerBits
+
+
+class TestPlayerBits(TestCase):
+ def test_range_limits(self):
+ bits = PlayerBits(0x00)
+
+ def set_value(value):
+ bits.bits = value
+
+ self.assertEqual(bits.bits, 0x00)
+ self.assertRaises(AssertionError, set_value, -1)
+ self.assertEqual(bits.bits, 0x00)
+ bits.bits = 255
+ self.assertEqual(bits.bits, 0xff)
+ bits.bits = 0
+ self.assertEqual(bits.bits, 0x00)
+ self.assertRaises(AssertionError, set_value, 256)
+ self.assertEqual(bits.bits, 0x00)
+
+ def test_check_bit(self):
+ bits = PlayerBits(0x01)
+ self.assertEqual(bits.check_bit(NORTH), True)
+ self.assertEqual(bits.check_bit(SOUTH), False)
+
+ def test_set_bit(self):
+ bits = PlayerBits(0x00)
+ self.assertEqual(bits._bits, 0x00)
+ bits.set_bit(NORTH)
+ self.assertEqual(bits._bits, 0x01)
+ bits.set_bit(NORTH)
+ self.assertEqual(bits._bits, 0x01)
+
+ def test_clear_bit(self):
+ bits = PlayerBits(0x01)
+ self.assertEqual(bits._bits, 0x01)
+ bits.clear_bit(NORTH)
+ self.assertEqual(bits._bits, 0x00)
+ bits.clear_bit(NORTH)
+ self.assertEqual(bits._bits, 0x00)
+
+ def test_toggle_bit(self):
+ bits = PlayerBits(0x00)
+ self.assertEqual(bits._bits, 0x00)
+ bits.toggle_bit(NORTH)
+ self.assertEqual(bits._bits, 0x01)
+ bits.toggle_bit(NORTH)
+ self.assertEqual(bits._bits, 0x00)
+
+ def test_check_bits(self):
+ bits = PlayerBits(0x03)
+ self.assertEqual(bits.check_bits(NORTH), True)
+ self.assertEqual(bits.check_bits(NORTH, SOUTH), True)
+ self.assertEqual(bits.check_bits(CYAN), False)
+ self.assertEqual(bits.check_bits(CYAN, MSB), False)
+ self.assertEqual(bits.check_bits(NORTH, SOUTH, CYAN), False)
+
+ def test_set_bits(self):
+ bits = PlayerBits(0x03)
+ self.assertEqual(bits._bits, 0x03)
+ bits.set_bits(NORTH, CYAN)
+ self.assertEqual(bits._bits, 0x13)
+ bits.set_bits(NORTH, CYAN, MSB)
+ self.assertEqual(bits._bits, 0x93)
+
+ def test_clear_bits(self):
+ bits = PlayerBits(0x03)
+ self.assertEqual(bits._bits, 0x03)
+ bits.clear_bits(NORTH, CYAN)
+ self.assertEqual(bits._bits, 0x02)
+ bits.clear_bits(NORTH, CYAN, MSB)
+ self.assertEqual(bits._bits, 0x02)
+
+ def test_toggle_bits(self):
+ bits = PlayerBits(0x03)
+ self.assertEqual(bits._bits, 0x03)
+ bits.toggle_bits(NORTH, CYAN)
+ self.assertEqual(bits._bits, 0x12)
+ bits.toggle_bits(NORTH, CYAN)
+ self.assertEqual(bits._bits, 0x03)
+ bits.toggle_bits(NORTH, CYAN, MSB)
+ self.assertEqual(bits._bits, 0x92)