any_move_valid

This commit is contained in:
Christoffer Müller Madsen 2018-02-15 12:41:38 +01:00
parent 77f5e74785
commit 246d1ac44a
2 changed files with 39 additions and 0 deletions

View File

@ -120,6 +120,16 @@ class Board:
checkers_at_from_idx(),
no_block_at_to_idx(),
can_bear_off() if bearing_off else True ])
@staticmethod
def any_move_valid(board, player, roll):
for die in roll:
idxs = Board.idxs_with_checkers_of_player(board, player)
for idx in idxs:
if Board.is_move_valid(board, player, die,
(idx, idx + (die * player))):
return True
return False
@staticmethod
def is_winner(board, player):

29
test.py
View File

@ -218,6 +218,35 @@ class TestDoMove(unittest.TestCase):
self.assertEqual(Board.do_move(board, -1, (1, 0)), expected_board)
self.assertEqual(Board.do_move(board, -1, (1, -1)), expected_board)
self.assertEqual(Board.do_move(board, -1, (1, -4)), expected_board)
class TestAnyMoveValid(unittest.TestCase):
def test_bear_in_but_blocked(self):
board = [ 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
2, 2, 0, 2, 2, 2,
-1 ]
self.assertEqual(Board.any_move_valid(board, -1, (3,6)), False)
self.assertEqual(Board.any_move_valid(board, -1, (6,3)), False)
self.assertEqual(Board.any_move_valid(board, -1, (3,4)), True)
self.assertEqual(Board.any_move_valid(board, -1, (4,1)), True)
def test_simple_block(self):
board = [ 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
2, 2, -1, 0, 0, 0,
0 ]
self.assertEqual(Board.any_move_valid(board, -1, (1,1)), False)
self.assertEqual(Board.any_move_valid(board, -1, (2,2)), False)
self.assertEqual(Board.any_move_valid(board, -1, (2,1)), False)
self.assertEqual(Board.any_move_valid(board, -1, (1,2)), False)
self.assertEqual(Board.any_move_valid(board, -1, (3,1)), True)
self.assertEqual(Board.any_move_valid(board, -1, (1,3)), True)
self.assertEqual(Board.any_move_valid(board, -1, (4,4)), True)
if __name__ == '__main__':
unittest.main()