1 from naja.constants import BITS
4 class PlayerBits(object):
9 def __init__(self, bits):
17 def bits(self, value):
18 assert 0 <= value <= 0xff
21 # Operate on individual bits
23 def check_bit(self, bit):
24 return bool(self.bits & bit)
26 def set_bit(self, bit):
29 def clear_bit(self, bit):
30 self.bits &= (0xff ^ bit)
32 def toggle_bit(self, bit):
35 # Operate on sets of bits
37 def check_bits(self, bits):
38 return all(self.check_bit(bit) for bit in bits)
40 def set_bits(self, bits):
44 def clear_bits(self, bits):
48 def toggle_bits(self, bits):
55 A representation of the player.
58 def __init__(self, bits, position):
59 self.bits = PlayerBits(bits)
60 self.position = position
63 def import_player(cls, definition):
64 return cls(definition['bits'], tuple(definition['position']))
68 'bits': self.bits.bits,
69 'position': list(self.position),
72 def move(self, direction):
73 if not self.bits.check_bit(direction):
75 # TODO: Something cleaner than this.
77 if direction == BITS.NORTH:
78 if y > 0 and self.bits.check_bit(BITS.NORTH):
79 self.position = (x, y - 1)
81 elif direction == BITS.SOUTH:
82 if y < 4 and self.bits.check_bit(BITS.SOUTH):
83 self.position = (x, y + 1)
85 elif direction == BITS.EAST:
86 if x < 4 and self.bits.check_bit(BITS.EAST):
87 self.position = (x + 1, y)
89 elif direction == BITS.WEST:
90 if x > 0 and self.bits.check_bit(BITS.WEST):
91 self.position = (x - 1, y)