--- /dev/null
+from random import choice
+
+from naja.constants import BITS, DIRECTION_BITS, CONDITION_BITS
+
+
+class GameBoard(object):
+ """
+ A representation of the game board.
+ """
+
+ def __init__(self, player, health, wins, locations=None, state=None):
+ self.player = player
+ self.max_health = health
+ self.wins_required = wins
+
+ if locations is None:
+ locations = self.generate_locations()
+ self.locations = locations
+
+ if state is None:
+ state = self.generate_state()
+ self.update_state(state)
+
+ @classmethod
+ def generate_locations(cls):
+ raise NotImplementedError("TODO")
+
+ def generate_state(self):
+ return {
+ 'health': self.max_health,
+ 'wins': 0,
+ }
+
+
+class LocationCard(object):
+ """
+ A particular set of options available on a location.
+ """
+
+ def __init__(self, bitwise_operation, bitwise_operand, actions):
+ self.bitwise_operation = bitwise_operation
+ self.bitwise_operand = bitwise_operand
+ self.actions = actions
+
+ @staticmethod
+ def generate_location():
+ raise NotImplementedError("TODO")
+
+ @staticmethod
+ def generate_bitwise_operand():
+ """
+ Generate a set of two or three bits. At least one direction and one
+ condition bit will be included. There is a low probability of choosing
+ a third bit from the complete set.
+ """
+ bits = set()
+ bits.add(choice(DIRECTION_BITS.values()))
+ bits.add(choice(CONDITION_BITS.values()))
+ # One in three chance of adding a third bit, with a further one in four
+ # chance that it will match a bit already chosen.
+ if choice(range(3)) == 0:
+ bits.add(choice(BITS.values()))
+ return frozenset(bits)
+
+ @staticmethod
+ def generate_location_actions():
+ raise NotImplementedError("TODO")
+
+ def apply_bitwise_operation(self, player):
+ operator = {
+ 'SET': player.bits.set_bits,
+ 'CLEAR': player.bits.clear_bits,
+ 'TOGGLE': player.bits.toggle_bits,
+ }[self.bitwise_operation]
+ operator(self.bitwise_operand)
+
+
+class LocationAction(object):
+ """
+ An action that may be performed on a location.
+ """
+
+ REQUIRED_BITS = frozenset()
+
+ def __init__(self, **data):
+ self.data = data
+
+ def check_available(self, player):
+ return player.bits.check_bits(self.REQUIRED_BITS)
+
+ def perform_action(self, player, board):
+ raise NotImplementedError("TODO")
from unittest import TestCase
-from naja.constants import NORTH, SOUTH, CYAN, MSB
+from naja.constants import BITS
from naja.player import PlayerBits
def test_check_bit(self):
bits = PlayerBits(0x01)
- self.assertEqual(bits.check_bit(NORTH), True)
- self.assertEqual(bits.check_bit(SOUTH), False)
+ self.assertEqual(bits.check_bit(BITS.NORTH), True)
+ self.assertEqual(bits.check_bit(BITS.SOUTH), False)
def test_set_bit(self):
bits = PlayerBits(0x00)
self.assertEqual(bits._bits, 0x00)
- bits.set_bit(NORTH)
+ bits.set_bit(BITS.NORTH)
self.assertEqual(bits._bits, 0x01)
- bits.set_bit(NORTH)
+ bits.set_bit(BITS.NORTH)
self.assertEqual(bits._bits, 0x01)
def test_clear_bit(self):
bits = PlayerBits(0x01)
self.assertEqual(bits._bits, 0x01)
- bits.clear_bit(NORTH)
+ bits.clear_bit(BITS.NORTH)
self.assertEqual(bits._bits, 0x00)
- bits.clear_bit(NORTH)
+ bits.clear_bit(BITS.NORTH)
self.assertEqual(bits._bits, 0x00)
def test_toggle_bit(self):
bits = PlayerBits(0x00)
self.assertEqual(bits._bits, 0x00)
- bits.toggle_bit(NORTH)
+ bits.toggle_bit(BITS.NORTH)
self.assertEqual(bits._bits, 0x01)
- bits.toggle_bit(NORTH)
+ bits.toggle_bit(BITS.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)
+ 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)
def test_set_bits(self):
bits = PlayerBits(0x03)
self.assertEqual(bits._bits, 0x03)
- bits.set_bits(NORTH, CYAN)
+ bits.set_bits(BITS.NORTH, BITS.CYAN)
self.assertEqual(bits._bits, 0x13)
- bits.set_bits(NORTH, CYAN, 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(NORTH, CYAN)
+ bits.clear_bits(BITS.NORTH, BITS.CYAN)
self.assertEqual(bits._bits, 0x02)
- bits.clear_bits(NORTH, CYAN, 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(NORTH, CYAN)
+ bits.toggle_bits(BITS.NORTH, BITS.CYAN)
self.assertEqual(bits._bits, 0x12)
- bits.toggle_bits(NORTH, CYAN)
+ bits.toggle_bits(BITS.NORTH, BITS.CYAN)
self.assertEqual(bits._bits, 0x03)
- bits.toggle_bits(NORTH, CYAN, MSB)
+ bits.toggle_bits(BITS.NORTH, BITS.CYAN, BITS.MSB)
self.assertEqual(bits._bits, 0x92)