示例#1
0
    def test_get_valid_flips(self):
        # Set up a testable game
        game = Othello(8)
        game.active_color = 'white'
        game.board.record_tile(Point(2, 2), 'white')
        game.board.record_tile(Point(5, 4), 'black')
        game.board.record_tile(Point(6, 5), 'black')
        game.board.record_tile(Point(7, 5), 'white')
        game.board.record_tile(Point(6, 6), 'white')
        game.board.record_tile(Point(5, 6), 'black')
        game.board.record_tile(Point(5, 7), 'black')
        game.board.record_tile(Point(4, 6), 'black')

        # Test good input (a move that results in a variety of flips or lack
        # thereof in each direction)
        test_move = game.get_valid_flips(Point(5, 5))
        expected_outcome = [Point(6, 5), Point(4, 4), Point(3, 3)]
        self.assertEqual(test_move, expected_outcome)

        # Test bad inputs:
        # 1. Move outside the board
        # 2. Move that targets an occupied square
        # 3. Argument is None instead of a point
        outside_board_test = game.get_valid_flips(Point(100, 100))
        occupied_board_test = game.get_valid_flips(Point(2, 2))
        none_point_test = game.get_valid_flips(None)
        self.assertEqual(outside_board_test, [])
        self.assertEqual(occupied_board_test, [])
        self.assertEqual(none_point_test, [])
示例#2
0
    def test_determine_ai_move(self):
        # Test that computer finds the only valid move on a board
        game = Othello(4)
        game.active_color = 'white'
        game.board.positions = [['white', 'black', 'black', 'black'],
                                ['black', 'black', 'white', 'black'],
                                ['black', 'white', 'black', 'black'],
                                ['black', 'black', 'black', 0]]
        ai_move = game.determine_ai_move()
        self.assertEqual(ai_move, [Point(3, 3), Point(2, 2), Point(1, 1)])

        # Test that computer picks a valid move from multiple possibilities
        game = Othello(4)
        game.active_color = 'white'
        game.board.positions = [[0, 0, 'white', 0], [0, 'black', 'white', 0],
                                [0, 'white', 'black', 0], [0, 0, 0, 'white']]
        ai_move = game.determine_ai_move()
        # Use get_valid_flips function to test that computer's move is valid
        move_test = (True if game.get_valid_flips(ai_move[0]) else False)
        self.assertTrue(move_test)

        # Test computer behavior when it cannot make a valid move
        game = Othello(4)
        game.active_color = 'white'
        game.board.positions = [['black', 'black', 'black', 'black'],
                                ['black', 'black', 'white', 'black'],
                                ['black', 'white', 'white', 'black'],
                                ['black', 'black', 'black', 0]]
        ai_move = game.determine_ai_move()
        self.assertIsNone(ai_move)