backgammon/test.py

277 lines
11 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(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_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
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)
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)
class TestLegalMoves(unittest.TestCase):
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-14 10:48:07 +00:00
if __name__ == '__main__':
unittest.main()