From 246d1ac44ad14d261bb00dc576a415a94ca939e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Thu, 15 Feb 2018 12:41:38 +0100 Subject: [PATCH] any_move_valid --- board.py | 10 ++++++++++ test.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) 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()