Prototype player object.
authorJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 13:12:14 +0000 (15:12 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 13:12:14 +0000 (15:12 +0200)
naja/constants.py
naja/player.py [new file with mode: 0644]
naja/tests/__init__.py [new file with mode: 0644]
naja/tests/test_player.py [new file with mode: 0644]

index faa8c55b14cac16ed99e0459f773c8f7e4039230..0dc6b84726fd370b7079d666c589040c7c4bdc84 100644 (file)
@@ -16,3 +16,13 @@ CHANNELS = 2   # 1 == mono, 2 == stereo
 BUFFER = 1024  # audio buffer size in no. of samples
 DEFAULT_SOUND_VOLUME = 1.0  # sound volume
 DEFAULT_MUSIC_VOLUME = 0.3  # music volume
+
+# Player bits
+NORTH = 0
+SOUTH = 1
+EAST = 2
+WEST = 3
+CYAN = 4
+MAGENTA = 5
+YELLOW = 6
+MSB = 7
diff --git a/naja/player.py b/naja/player.py
new file mode 100644 (file)
index 0000000..acae919
--- /dev/null
@@ -0,0 +1,59 @@
+
+
+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
diff --git a/naja/tests/__init__.py b/naja/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/naja/tests/test_player.py b/naja/tests/test_player.py
new file mode 100644 (file)
index 0000000..48b2ef8
--- /dev/null
@@ -0,0 +1,85 @@
+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)