import unittest from board import Board class TestIsMoveValid(unittest.TestCase): def setUp(self): self.initial = Board.initial_state def test_simple_movement(self): self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (1, 3)), True) # White self.assertEqual(Board.is_move_valid(self.initial, -1, 3, (24, 21)), True) # Black def test_backwards_movement_invalid(self): self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (12, 10)), False) self.assertEqual(Board.is_move_valid(self.initial, -1, 3, (8, 11)), False) def test_face_value_match_move_length(self): self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (1, 3)), True) self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (1, 4)), False) def test_bear_in(self): board = [ 1, 1, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 2, (0, 2)), True) board = [ 0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -1, -1 ] self.assertEqual(Board.is_move_valid(board, -1, 2, (25, 23)), True) def test_force_bear_in(self): board = [ 1, 1, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 2, (1, 3)), False) self.assertEqual(Board.is_move_valid(board, -1, 3, (24, 21)), True) board = [ 0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -1, -1 ] self.assertEqual(Board.is_move_valid(board, -1, 2, (24, 22)), False) self.assertEqual(Board.is_move_valid(board, 1, 2, (1, 3)), True) def test_owned_checker_at_from(self): self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (3, 5)), False) # No checkers self.assertEqual(Board.is_move_valid(self.initial, -1, 2, (23, 21)), False) # No checkers self.assertEqual(Board.is_move_valid(self.initial, 1, 2, (8, 10)), False) # Opponent checkers self.assertEqual(Board.is_move_valid(self.initial, -1, 2, (1, 3)), False) # Opponent checkers def test_no_block_at_to(self): self.assertEqual(Board.is_move_valid(self.initial, 1, 5, (1, 6)), False) self.assertEqual(Board.is_move_valid(self.initial, -1, 5, (24, 19)), False) def test_able_to_hit_opponent_checkers(self): board = [ 0, 2, 0, 0, -1, 0, -4, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 4, 0, 1, 0, 0, -2, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 3, (1, 4)), True) self.assertEqual(Board.is_move_valid(board, -1, 3, (24, 21)), True) def test_bear_off_simple(self): board = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 2, (24, 26)), True) self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), True) def test_bear_off_with_higher_face_value(self): board = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 5, (23, 28)), False) self.assertEqual(Board.is_move_valid(board, 1, 5, (22, 27)), True) def test_bear_off_all_in_last_quadrant(self): board = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), False) board = [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ] self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), False) board = [ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 ] self.assertEqual(Board.is_move_valid(board, -1, 1, (1, 0)), False) board = [ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] self.assertEqual(Board.is_move_valid(board, -1, 1, (1, 0)), False) # TODO: More tests for bearing off are needed class TestIsWinner(unittest.TestCase): def test_is_winner(self): board = [ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 3, 0, 4, 0, 1, 0, 0, 0, 0 ] self.assertEqual(Board.is_winner(board, 1), False) self.assertEqual(Board.is_winner(board, -1), True) board = [ 0, 0, 0, 0, -1, 0, -4, 0, -3, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0 ] self.assertEqual(Board.is_winner(board, 1), True) self.assertEqual(Board.is_winner(board, -1), False) class TestDoMove(unittest.TestCase): def test_simple_move(self): board = [ 0, 2, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 3, 0, 4, 0, 1, 0, 0, 0, 0 ] expected_board = [ 0, 2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 3, 0, 4, 0, 1, 0, 0, 0, 0 ] self.assertEqual(Board.do_move(board, -1, (3, 2)), expected_board) def test_bear_in(self): board = [ 1, 1, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0 ] expected_board = [ 0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0 ] self.assertEqual(Board.do_move(board, 1, (0, 1)), expected_board) board = [ 0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -1, -1 ] expected_board = [ 0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0 ] self.assertEqual(Board.do_move(board, -1, (25, 24)), expected_board) def test_bear_off(self): board = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ] expected_board = [0] * 26 self.assertEqual(Board.do_move(board, 1, (24, 30)), expected_board) self.assertEqual(Board.do_move(board, 1, (24, 26)), expected_board) self.assertEqual(Board.do_move(board, 1, (24, 25)), expected_board) board = [ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] expected_board = [0] * 26 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) if __name__ == '__main__': unittest.main()