From e50c357dc15f5c24d3c0b8a4dd57c9f19590419b Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Sun, 11 May 2014 20:52:16 +0200 Subject: [PATCH 1/1] More actions. --- naja/actions.py | 20 ++++++++++++++--- naja/tests/test_actions.py | 45 +++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/naja/actions.py b/naja/actions.py index b0371a7..5599bd7 100644 --- a/naja/actions.py +++ b/naja/actions.py @@ -15,7 +15,7 @@ class LocationAction(object): def check_available(self, player): return player.bits.check_bits(self.required_bits) - def perform_action(self, board): + def perform_action(self, board, location): raise NotImplementedError("TODO") def check_and_clear_MSB(self, player): @@ -29,13 +29,27 @@ class LocationAction(object): class DoNothing(LocationAction): TEXT = "No effect." - def perform_action(self, board): + def perform_action(self, board, location): pass class LoseHeathOrMSB(LocationAction): TEXT = "Lose health. If MSB is set, it will be cleared instead." - def perform_action(self, board): + def perform_action(self, board, location): if not self.check_and_clear_MSB(board.player): board.lose_health() + + +class SetBits(LocationAction): + TEXT = "Set bits specified by this location." + + def perform_action(self, board, location): + board.player.bits.set_bits(location.bitwise_operand) + + +class ToggleBits(LocationAction): + TEXT = "Toggle bits specified by this location." + + def perform_action(self, board, location): + board.player.bits.toggle_bits(location.bitwise_operand) diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index e4a4fa5..3d3a58e 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -1,7 +1,7 @@ from unittest import TestCase from naja.constants import BITS -from naja.gameboard import GameBoard +from naja.gameboard import GameBoard, LocationCard from naja.player import Player from naja import actions @@ -24,17 +24,56 @@ class TestActions(TestCase): def test_DoNothing(self): board = GameBoard.new_game([]) state_before = board.export() - actions.DoNothing(set()).perform_action(board) + actions.DoNothing(set()).perform_action(board, None) state_after = board.export() self.assertEqual(state_before, state_after) def test_LoseHealthOrMSB_MSB_clear(self): board = GameBoard.new_game([]) state_before = board.export() - actions.LoseHeathOrMSB(set()).perform_action(board) + actions.LoseHeathOrMSB(set()).perform_action(board, None) state_after = board.export() self.assertEqual(state_after['health'], state_before['health'] - 1) state_before.pop('health') state_after.pop('health') self.assertEqual(state_before, state_after) + + def test_LoseHealthOrMSB_MSB_set(self): + board = GameBoard.new_game([]) + board.player.bits.set_bit(BITS.MSB) + state_before = board.export() + actions.LoseHeathOrMSB(set()).perform_action(board, None) + state_after = board.export() + self.assertEqual(board.player.bits.check_bit(BITS.MSB), False) + + state_before['player'].pop('bits') + state_after['player'].pop('bits') + self.assertEqual(state_before, state_after) + + def test_SetBits(self): + board = GameBoard.new_game([]) + state_before = board.export() + location = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + actions.SetBits(set()).perform_action(board, location) + state_after = board.export() + self.assertEqual( + board.player.bits.check_bits([BITS.MSB, BITS.NORTH]), True) + + state_before['player'].pop('bits') + state_after['player'].pop('bits') + self.assertEqual(state_before, state_after) + + def test_ToggleBits(self): + board = GameBoard.new_game([]) + board.player.bits.set_bit(BITS.NORTH) + state_before = board.export() + location = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + actions.ToggleBits(set()).perform_action(board, location) + state_after = board.export() + self.assertEqual(board.player.bits.check_bit(BITS.MSB), True) + self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False) + + state_before['player'].pop('bits') + state_after['player'].pop('bits') + self.assertEqual(state_before, state_after) -- 2.34.1