backgammon/test.py

557 lines
20 KiB
Python
Raw Normal View History

2018-02-14 10:48:07 +00:00
import unittest
from board import Board
class TestIsMoveValid(unittest.TestCase):
def setUp(self):
self.initial = Board.initial_state
2018-02-14 10:48:07 +00:00
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
2018-02-14 10:48:07 +00:00
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)
2018-02-14 10:48:07 +00:00
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)
2018-02-14 10:48:07 +00:00
def test_bear_in(self):
board = ( 1,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
self.assertEqual(Board.is_move_valid(board, 1, 2, (0, 2)), True)
board = ( 0,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
self.assertEqual(Board.is_move_valid(board, -1, 2, (25, 23)), True)
def test_force_bear_in(self):
board = ( 1,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
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,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
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
2018-02-14 10:48:07 +00:00
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)
2018-02-14 10:48:07 +00:00
def test_able_to_hit_opponent_checkers(self):
board = ( 0,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
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_white(self):
board = ( 0,
2018-02-14 10:48:07 +00:00
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 )
2018-02-14 10:48:07 +00:00
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_simple_black(self):
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 )
self.assertEqual(Board.is_move_valid(board, -1, 2, (1, -1)), True)
self.assertEqual(Board.is_move_valid(board, -1, 1, (1, 0)), 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)
2018-02-14 10:48:07 +00:00
# TODO: More tests for bearing off are needed
2018-02-15 12:57:59 +00:00
class TestNumOfChecker(unittest.TestCase):
def test_simple_1(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.num_of_checkers_for_player(board,1), 2+5+3+4+1)
self.assertEqual(Board.num_of_checkers_for_player(board,-1), 0)
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.num_of_checkers_for_player(board,-1), 2+5+3+4+1)
self.assertEqual(Board.num_of_checkers_for_player(board,1), 0)
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 )
2018-02-15 12:57:59 +00:00
self.assertEqual(Board.outcome(board), {1: -2, -1: 2})
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 )
2018-02-15 12:57:59 +00:00
self.assertEqual(Board.outcome(board), {1: 2, -1: -2})
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, -2,
0 )
self.assertEqual(Board.outcome(board), {1: 1, -1: -1})
board = ( 0,
2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 5,
0, 0, 0, 0, 3, 0,
5, 0, 0, 0, 0, 0,
0 )
self.assertEqual(Board.outcome(board), {1: -2, -1: 2})
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 = tuple([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 = tuple([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)
2018-02-15 11:41:38 +00:00
class TestAnyMoveValid(unittest.TestCase):
def test_bear_in_but_blocked(self):
board = ( 0,
2018-02-15 11:41:38 +00:00
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 )
2018-02-15 11:41:38 +00:00
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,
2018-02-15 11:41:38 +00:00
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 )
2018-02-15 11:41:38 +00:00
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)
def test_white_bear_off_odd(self):
board = (0,
-14, -1, 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.any_move_valid(board, 1, (1,2)), True)
self.assertEqual(Board.any_move_valid(board, 1, (2,4)), True)
self.assertEqual(Board.any_move_valid(board, 1, (4,6)), True)
def test_black_bear_off_odd(self):
board = (0,
-1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 14,
0)
self.assertEqual(Board.any_move_valid(board, -1, (1,2)), True)
self.assertEqual(Board.any_move_valid(board, -1, (2,4)), True)
self.assertEqual(Board.any_move_valid(board, -1, (4,6)), True)
class TestLegalMoves(unittest.TestCase):
def test_white_bear_off(self):
board = (0,
-14, -1, 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_set = {( 0,
-14, -1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0 )}
self.assertEqual(Board.calculate_legal_states(board, 1, (1,2)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, 1, (2,4)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, 1, (4,2)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, 1, (4,6)), expected_board_set)
def test_black_bear_off(self):
board = (0,
-1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 14,
0)
expected_board_set = {( 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 14,
0 )}
self.assertEqual(Board.calculate_legal_states(board, -1, (1,2)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, -1, (2,4)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, -1, (4,2)), expected_board_set)
self.assertEqual(Board.calculate_legal_states(board, -1, (4,6)), expected_board_set)
def test_blocked(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 )
expected_board_set_3_1 = {( 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -1, 0,
2, 2, 0, 0, 0, 0,
0 )}
expected_board_set_3_4 = {( 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, -1, 0, 0, 0, 0,
2, 2, 0, 0, 0, 0,
0 )}
self.assertEqual(Board.calculate_legal_states(board, -1, (1,2)), { board })
self.assertEqual(Board.calculate_legal_states(board, -1, (1,1)), { board })
self.assertEqual(Board.calculate_legal_states(board, -1, (3,1)), expected_board_set_3_1)
self.assertEqual(Board.calculate_legal_states(board, -1, (3,4)), expected_board_set_3_4)
2018-02-15 12:18:54 +00:00
def test_multiple_checkers(self):
board = ( 0,
0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 )
expected_board_set_3_5 = {
( 0,
0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2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0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 ),
( 0,
0, 2, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 ),
( 0,
1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 ),
( 0,
0, 1, 0, 3, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 ),
( 0,
1, 0, 1, 0, 2, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -2, 0,
0 )
}
self.assertEqual(Board.calculate_legal_states(board, 1, (1,1)), expected_board_set_1_1)
def test_hit_on_bear_in(self):
board = (0,
-2, 0, -2, -1, -3, -2,
0, -2, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0,
6, 0, 3, 1, 1, -4,
-1)
expected_board_set = { (1,
-2, 0, -2, -1, -3, -2,
0, -2, 0, 0, 0, 0,
-1, 0, 0, 0, 0, -1,
6, 0, 3, 0, 1, -4,
0),
(1,
-2, 0, -2, -1, -3, -2,
0, -2, -1, 0, 0, 0,
0, 0, 0, 0, 0, 0,
6, 0, 3, -1, 1, -4,
0),
(1,
-2, 0, -2, -2, -3, -2,
0, -1, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0,
6, 0, 3, -1, 1, -4,
0),
(1,
-2, -1, -2, -1, -3, -1,
0, -2, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0,
6, 0, 3, -1, 1, -4,
0),
(1,
-3, 0, -2, -1, -2, -2,
0, -2, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0,
6, 0, 3, -1, 1, -4,
0),
(1,
-2, 0, -2, -1, -3, -2,
0, -2, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0,
6, -1, 3, -1, 1, -3,
0)
}
self.assertEqual(Board.calculate_legal_states(board, -1, (4,3)), expected_board_set)
2018-02-14 10:48:07 +00:00
if __name__ == '__main__':
unittest.main()