From: Jeremy Thurgood Date: Sun, 11 May 2014 15:31:07 +0000 (+0200) Subject: Start of location actions. X-Git-Tag: 0.1~423^2 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=54b217b57e6b894e261d186319b1337fb53c0210;p=naja.git Start of location actions. --- diff --git a/naja/actions.py b/naja/actions.py new file mode 100644 index 0000000..333d174 --- /dev/null +++ b/naja/actions.py @@ -0,0 +1,16 @@ +from naja.gameboard import LocationAction + + +class DoNothing(LocationAction): + TEXT = "No effect." + + def perform_action(self, player, board): + pass + + +class LoseHeathOrMSB(LocationAction): + TEXT = "Lose health. If MSB is set, it will be cleared instead." + + def perform_action(self, player, board): + if not self.check_and_clear_MSB(player): + board.lose_health() diff --git a/naja/gameboard.py b/naja/gameboard.py index a107b16..5cbecb9 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -31,14 +31,21 @@ class GameBoard(object): 'wins': 0, } + def update_state(self, state): + self.health = state['health'] + self.wins = state['wins'] + + def lose_health(self): + self.health -= 1 + # TODO: Check win/lose + 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 + def __init__(self, bitwise_operand, actions): self.bitwise_operand = bitwise_operand self.actions = actions @@ -66,27 +73,27 @@ class LocationCard(object): 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. """ + TEXT = None REQUIRED_BITS = frozenset() def __init__(self, **data): 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") + + def check_and_clear_MSB(self, player): + if player.bits.check_bit(BITS.MSB): + player.bits.clear_bit(BITS.MSB) + return True + else: + return False diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py new file mode 100644 index 0000000..bf04e21 --- /dev/null +++ b/naja/tests/test_actions.py @@ -0,0 +1,19 @@ +from unittest import TestCase + +from naja.player import Player +from naja import actions + + +class TestActions(TestCase): + def make_player(self, *bits): + return Player(sum(1 << bit for bit in bits), None) + + def test_DoNothing_check_available(self): + player = self.make_player() + action = actions.DoNothing() + self.assertEqual(action.check_available(player), True) + + def test_LoseHealthOrMSB_check_available(self): + player = self.make_player() + action = actions.LoseHeathOrMSB() + self.assertEqual(action.check_available(player), True) diff --git a/naja/tests/test_gameboard.py b/naja/tests/test_gameboard.py index a895e18..0ad0d28 100644 --- a/naja/tests/test_gameboard.py +++ b/naja/tests/test_gameboard.py @@ -1,7 +1,15 @@ from unittest import TestCase from naja.constants import BITS -from naja.gameboard import LocationCard +from naja.gameboard import GameBoard, LocationCard + + +class TestGameBoard(TestCase): + def test_lose_health(self): + board = GameBoard(None, 4, 4, locations={}, state=None) + self.assertEqual(board.health, 4) + board.lose_health() + self.assertEqual(board.health, 3) class TestLocationCard(TestCase):