diff --git a/board.py b/board.py index 37e22b5..d55f92a 100644 --- a/board.py +++ b/board.py @@ -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): diff --git a/test.py b/test.py index d564533..3467d56 100644 --- a/test.py +++ b/test.py @@ -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()