player = Player(initial_bits, initial_pos)
board_locations = cls.import_board_locations(
cls.generate_board(deck))
- return cls(state, player, board_locations)
+ board = cls(state, player, board_locations)
+ player.set_gameboard(board)
+ return board
@classmethod
def import_game(cls, definition):
board_locations = [
[(i % 5, i // 5),
LocationCard.new_location(
- card.copy(), replacement_params).export()]
+ card.copy(), replacement_params, puzzle=True).export()]
for i, card in enumerate(deck['cards'])
]
return board_locations
"""
def __init__(self, card_name, bitwise_operand, location_actions,
- replacement_time, max_number=25):
+ replacement_time=None, max_number=25):
self.card_name = card_name
self.bitwise_operand = bitwise_operand
self.actions = location_actions
self.max_number = max_number
- self.check_actions()
self.replacement_time = replacement_time
@classmethod
return action_class(required_bits, **data)
@classmethod
- def new_location(cls, definition, replacement_params):
+ def new_location(cls, definition, replacement_params=None, puzzle=False):
if 'bits' in definition:
bits = cls.parse_bits(definition['bits'])
else:
max_number = definition.get('max_number', 25)
card_name = definition['card_name']
- return cls.import_location({
+ location = cls.import_location({
'bitwise_operand': bits,
'actions': definition['actions'],
'max_number': max_number,
'card_name': card_name,
'replacement_time': replacement_time,
})
+ if not puzzle:
+ location.check_actions()
+ return location
@classmethod
def parse_bits(self, bit_list):
A representation of the player.
"""
- def __init__(self, bits, position, movement_mode=None):
+ def __init__(self, bits, position, movement_mode=None, gameboard=None):
self.bits = PlayerBits(bits)
self.position = position
self.movement_mode = movement_mode if movement_mode else MOVES.ADJACENT
+ self.gameboard = gameboard
@classmethod
def import_player(cls, definition):
return True
return False
+ def set_gameboard(self, gameboard):
+ self.gameboard = gameboard
+
+ def pos_has_action(self, pos):
+ card = self.gameboard.board_locations[pos]
+ for action in card.actions:
+ if self.bits.check_bits(action.required_bits):
+ return True
+ return False
+
+ def filter_moves_with_no_actions(self, positions):
+ return [pos for pos in positions if self.pos_has_action(pos)]
+
def legal_moves(self):
POSITION_FUNCTION = {
MOVES.ADJACENT: self.get_adjacent_positions,
MOVES.BISHOP: self.get_bishop_positions,
MOVES.CASTLE: self.get_castle_positions,
}
- return POSITION_FUNCTION[self.movement_mode]()
+ positions = POSITION_FUNCTION[self.movement_mode]()
+ return self.filter_moves_with_no_actions(positions)
def allow_chess_move(self, chesspiece):
self.movement_mode = chesspiece
exported_state = board.export()
board_locations = exported_state.pop('board_locations')
self.assertEqual(exported_state, {
+ 'clock_count': 0,
'max_health': 4,
'health': 4,
'wins_required': 4,
'puzzle': False,
'player_mode': EXAMINE,
'replacement_params': None,
- 'clock_count': 0,
})
positions = []
for position, location_state in board_locations:
from naja.constants import BITS, MOVES
from naja.player import PlayerBits, Player
+from naja.gameboard import GameBoard
class TestPlayerBits(TestCase):
class TestPlayer(TestCase):
+
+ def setUp(self):
+ self.board = GameBoard.new_game({'cards': [
+ {'card_name': 'card1', 'actions': [
+ {
+ 'action_class': 'LoseHealthOrMSB',
+ 'required_bits': [],
+ }, {
+ 'action_class': 'GainHealth',
+ 'required_bits': [BITS.RED],
+ },
+ ]}
+ ]})
+
def test_new_player(self):
- player = Player(0x0f, (0, 1))
+ player = Player(0x0f, (0, 1), self.board)
self.assertEqual(player.bits.bits, 0x0f)
self.assertEqual(player.position, (0, 1))
})
def test_legal_moves_all_available(self):
- player = Player(0x0f, (2, 2))
+ player = Player(0x0f, (2, 2), gameboard=self.board)
self.assertEqual(
player.legal_moves(), [(2, 2), (2, 1), (2, 3), (3, 2), (1, 2)])
def test_legal_moves_some_unavailable(self):
- player = Player(0x0f, (0, 2))
+ player = Player(0x0f, (0, 2), gameboard=self.board)
player.bits.clear_bit(BITS.NORTH)
self.assertEqual(player.legal_moves(), [(0, 2), (0, 3), (1, 2)])
def test_legal_moves_castle(self):
- player = Player(0x0f, (1, 3), MOVES.CASTLE)
+ player = Player(0x0f, (1, 3), MOVES.CASTLE, gameboard=self.board)
self.assertEqual(player.legal_moves(), [
(0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 3),
(4, 3)])
def test_legal_moves_bishop(self):
- player = Player(0x0f, (1, 3), MOVES.BISHOP)
+ player = Player(0x0f, (1, 3), MOVES.BISHOP, gameboard=self.board)
self.assertEqual(player.legal_moves(), [
(0, 2), (0, 4), (1, 3), (2, 2), (2, 4), (3, 1), (4, 0)])
def test_legal_moves_knight(self):
- player = Player(0x0f, (1, 3), MOVES.KNIGHT)
+ player = Player(0x0f, (1, 3), MOVES.KNIGHT, gameboard=self.board)
self.assertEqual(player.legal_moves(), [
(0, 1), (1, 3), (2, 1), (3, 2), (3, 4)])
def test_set_position(self):
- player = Player(0x0f, (3, 3), MOVES.BISHOP)
+ player = Player(0x0f, (3, 3), MOVES.BISHOP, gameboard=self.board)
player.set_position((4, 4))
self.assertEqual(player.movement_mode, MOVES.ADJACENT)
[(x_offset, y_offset), (right, y_offset),
(right, bottom), (x_offset, bottom)], 4)
- if action.required_bits:
+ if action.required_bits in BIT_MAP:
img_name = BIT_MAP[action.required_bits].replace(
'.png', '_small.png')
img = resources.get_image(img_name,
self.surface.blit(img, (TILE_SIZE[0] - 20, 0))
def _prepare_lock(self, action, y_offset):
- if not action.required_bits:
+ if action.required_bits not in BIT_MAP:
return 4
img_name = BIT_MAP[action.required_bits]