示例#1
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)
示例#2
0
    def test_init(self):
        point = Point(0, 3.5)
        self.assertEqual(point.x, 0)
        self.assertEqual(point.y, 3.5)

        point = Point(-6.2, -3)
        self.assertEqual(point.x, -6.2)
        self.assertEqual(point.y, -3)
示例#3
0
 def test_eq(self):
     point_one = Point(2.1, -6.5)
     point_two = Point(2.1, -6.5)
     point_three = Point(2.1, 5)
     point_four = Point(6, -6.5)
     point_five = Point(8, 8)
     self.assertEqual(point_one, point_two)
     self.assertNotEqual(point_one, point_three)
     self.assertNotEqual(point_one, point_four)
     self.assertNotEqual(point_one, point_five)
示例#4
0
    def test_get_initial_tiles_to_draw(self):
        expected_outcome = [(Point(-4, -25), 'black'), (Point(46,
                                                              -25), 'white'),
                            (Point(46, 25), 'black'), (Point(-4, 25), 'white')]
        board = Board(4)
        tiles = board.get_initial_tiles_to_draw()
        self.assertEqual(tiles, expected_outcome)

        board = Board(8)
        tiles = board.get_initial_tiles_to_draw()
        self.assertEqual(tiles, expected_outcome)
示例#5
0
    def test_find_center(self):
        board = Board(4)
        center = board.find_center()
        self.assertEqual(center, Point(1, 1))

        board = Board(8)
        center = board.find_center()
        self.assertEqual(center, Point(3, 3))

        board = Board(12)
        center = board.find_center()
        self.assertEqual(center, Point(5, 5))
示例#6
0
    def test_init(self):
        board = Board(4)
        self.assertEqual(board.size, 4)
        self.assertEqual(board.center, Point(1, 1))
        self.assertEqual(board.positions, INITIALIZED_4X4_BOARD)
        self.assertEqual(board.black_tiles, 2)
        self.assertEqual(board.white_tiles, 2)

        board = Board(8)
        self.assertEqual(board.size, 8)
        self.assertEqual(board.center, Point(3, 3))
        self.assertEqual(board.positions, INITIALIZED_8X8_BOARD)
        self.assertEqual(board.black_tiles, 2)
        self.assertEqual(board.white_tiles, 2)
示例#7
0
    def test_str(self):
        point = Point(3, -6)
        expected_output = '(3, -6)'
        self.assertEqual(point.__str__(), expected_output)

        point = Point(0, 2.63)
        expected_output = '(0, 2.63)'
        self.assertEqual(point.__str__(), expected_output)
示例#8
0
    def test_eq(self):
        # Set up test boards with different attributes
        board_one = Board(4)
        board_two = Board(4)
        board_three = Board(8)
        board_four = Board(4)
        board_four.center = Point(3, 3)
        board_five = Board(4)
        board_five.positions = INITIALIZED_8X8_BOARD
        board_six = Board(4)
        board_six.black_tiles = 10
        board_seven = Board(4)
        board_seven.white_tiles = 10

        # Test for equality or lack thereof
        self.assertEqual(board_one, board_two)
        self.assertNotEqual(board_one, board_three)
        self.assertNotEqual(board_one, board_four)
        self.assertNotEqual(board_one, board_five)
        self.assertNotEqual(board_one, board_six)
        self.assertNotEqual(board_one, board_seven)
示例#9
0
    def test_find_flips_se(self):
        # Set up move validators for black and white
        board = Board(8)
        board.record_tile(Point(5, 3), 'white')
        board.record_tile(Point(5, 2), 'white')
        board.record_tile(Point(6, 1), 'black')
        board.record_tile(Point(6, 0), 'white')
        board.record_tile(Point(7, 5), 'white')
        flips_w = flipFinder(board.size, board.positions, 'white')
        flips_b = flipFinder(board.size, board.positions, 'black')

        # Test good input (1+ tiles flipped)
        one_flip = flips_w.find_flips_se(Point(3, 5))
        three_flips = flips_b.find_flips_se(Point(2, 5))
        self.assertEqual(one_flip, [Point(4, 4)])
        self.assertEqual(three_flips, [Point(3, 4), Point(4, 3), Point(5, 2)])

        # Test bad input (moves with no valid flips:
        #   1. Hits friendly tile without finding opposing tiles, or
        #   2. Hits edge of board or empty square without finding friendly tile
        #   3. Move is outside the board
        friendly_tile = flips_b.find_flips_se(Point(3, 5))
        empty_space = flips_w.find_flips_se(Point(2, 4))
        edge_south = flips_b.find_flips_se(Point(5, 1))
        edge_east = flips_b.find_flips_se(Point(6, 6))
        outside = flips_w.find_flips_se(Point(-100, 100))
        self.assertEqual(friendly_tile, [])
        self.assertEqual(empty_space, [])
        self.assertEqual(edge_south, [])
        self.assertEqual(edge_east, [])
        self.assertEqual(outside, [])
示例#10
0
    def test_find_flips_all(self):
        # Set up a testable board
        board = Board(8)
        board.record_tile(Point(2, 2), 'white')
        board.record_tile(Point(5, 4), 'black')
        board.record_tile(Point(6, 5), 'black')
        board.record_tile(Point(7, 5), 'white')
        board.record_tile(Point(6, 6), 'white')
        board.record_tile(Point(5, 6), 'black')
        board.record_tile(Point(5, 7), 'black')
        board.record_tile(Point(4, 6), 'black')
        flips_b = flipFinder(board.size, board.positions, 'white')

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

        # Test behavior for a move outside of the board
        outside_board_test = flips_b.find_flips_all(Point(100, 100))
        self.assertEqual(outside_board_test, [])
示例#11
0
    def test_is_a_square(self):
        board = Board(4)
        # Test for inside and outside of board range
        self.assertTrue(board.is_a_square(Point(0, 0)))
        self.assertTrue(board.is_a_square(Point(3, 3)))
        self.assertTrue(board.is_a_square(Point(1, 2)))
        self.assertFalse(board.is_a_square(Point(5, 0)))
        self.assertFalse(board.is_a_square(Point(-1, 0)))
        self.assertFalse(board.is_a_square(Point(0, 4)))
        self.assertFalse(board.is_a_square(Point(0, -1)))

        board = Board(8)
        # Test for inside and outside of board range
        self.assertTrue(board.is_a_square(Point(0, 0)))
        self.assertTrue(board.is_a_square(Point(7, 7)))
        self.assertTrue(board.is_a_square(Point(1, 2)))
        self.assertFalse(board.is_a_square(Point(8, 0)))
        self.assertFalse(board.is_a_square(Point(-1, 0)))
        self.assertFalse(board.is_a_square(Point(0, 8)))
        self.assertFalse(board.is_a_square(Point(0, -1)))
示例#12
0
    def test_is_empty_square(self):
        # Set up a board with various tiles
        board = Board(4)
        board.record_tile(Point(3, 1), 'black')
        board.record_tile(Point(2, 2), 'white')
        board.record_tile(Point(2, 0), 'black')

        # Test for empty and non-empty sauares
        self.assertTrue(board.is_empty_square(Point(0, 2)))
        self.assertTrue(board.is_empty_square(Point(2, 3)))
        self.assertTrue(board.is_empty_square(Point(1, 3)))
        self.assertFalse(board.is_empty_square(Point(3, 1)))
        self.assertFalse(board.is_empty_square(Point(2, 2)))
        self.assertFalse(board.is_empty_square(Point(2, 0)))

        # Test for postions outside of board
        self.assertFalse(board.is_empty_square(Point(5, 0)))
        self.assertFalse(board.is_empty_square(Point(-1, 0)))
        self.assertFalse(board.is_empty_square(Point(0, 4)))
        self.assertFalse(board.is_empty_square(Point(0, -1)))
示例#13
0
    def test_record_tile(self):
        board = Board(4)
        # Inputs that result in a tile placement
        board.record_tile(Point(1, 3), 'white')
        board.record_tile(Point(0, 2), 'black')
        board.record_tile(Point(1, 2), 'black')
        # Inputs that don't result in a tile placement
        board.record_tile(Point(4, 3), 'white')
        board.record_tile(Point(1, 1), 'black')
        board.record_tile(Point(-1, 3), 'black')
        board.record_tile(Point(0, 5), 'black')
        board.record_tile(Point(0, -1), 'white')
        board.record_tile(Point(1, 2), 'red')
        expected_output = [[0, 0, 'black', 0], [0, 'black', 'black', 'white'],
                           [0, 'white', 'black', 0], [0, 0, 0, 0]]
        self.assertEqual(board.positions, expected_output)
        self.assertEqual(board.black_tiles, 4)
        self.assertEqual(board.white_tiles, 2)

        board = Board(8)
        # Inputs that result in a tile placement
        board.record_tile(Point(6, 2), 'white')
        board.record_tile(Point(0, 7), 'black')
        board.record_tile(Point(3, 4), 'black')
        # Inputs that don't result in a tile placement
        board.record_tile(Point(3, 3), 'black')
        board.record_tile(Point(8, 3), 'white')
        board.record_tile(Point(-1, 3), 'black')
        board.record_tile(Point(0, 10), 'black')
        board.record_tile(Point(0, -1), 'white')
        board.record_tile(Point(2, 4), 'green')
        expected_output = [[0, 0, 0, 0, 0, 0, 0, 'black'],
                           [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 'black', 'black', 0, 0, 0],
                           [0, 0, 0, 'white', 'black', 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 'white', 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0]]
        self.assertEqual(board.positions, expected_output)
        self.assertEqual(board.black_tiles, 4)
        self.assertEqual(board.white_tiles, 2)
示例#14
0
    def test_convert_turtle_to_square(self):
        board = Board(4)
        # Testing function normally
        # Good inputs
        square_one = board.convert_turtle_to_square(Point(-27, -74))
        square_two = board.convert_turtle_to_square(Point(0, 0))
        # Bad inputs - point is outside of the board in various ways
        square_three = board.convert_turtle_to_square(Point(-101, 50))
        square_four = board.convert_turtle_to_square(Point(101, 50))
        square_five = board.convert_turtle_to_square(Point(50, 101))
        square_six = board.convert_turtle_to_square(Point(50, -101))
        self.assertEqual(square_one, Point(1, 0))
        self.assertEqual(square_two, Point(2, 2))
        self.assertIsNone(square_three)
        self.assertIsNone(square_four)
        self.assertIsNone(square_five)
        self.assertIsNone(square_six)

        # Testing reverse version of function
        # Good inputs
        turtle_one = board.convert_turtle_to_square(Point(0, 0), 'r')
        turtle_two = board.convert_turtle_to_square(Point(3, 1), 'r')
        # Bad inputs - point is outside of the board in various ways
        turtle_three = board.convert_turtle_to_square(Point(4, 0), 'r')
        turtle_four = board.convert_turtle_to_square(Point(-1, 0), 'r')
        turtle_five = board.convert_turtle_to_square(Point(0, 4), 'r')
        turtle_six = board.convert_turtle_to_square(Point(0, -1), 'r')
        self.assertEqual(turtle_one, Point(-54, -75))
        self.assertEqual(turtle_two, Point(96, -25))
        self.assertIsNone(turtle_three)
        self.assertIsNone(turtle_four)
        self.assertIsNone(turtle_five)
        self.assertIsNone(turtle_six)

        board = Board(8)
        # Testing function normally
        # Good inputs
        square_one = board.convert_turtle_to_square(Point(199, -101))
        square_two = board.convert_turtle_to_square(Point(-199, 158))
        # Bad inputs - point is outside of the board in various ways
        square_three = board.convert_turtle_to_square(Point(201, 0))
        square_four = board.convert_turtle_to_square(Point(-201, 0))
        square_five = board.convert_turtle_to_square(Point(0, 201))
        square_six = board.convert_turtle_to_square(Point(0, -201))
        self.assertEqual(square_one, Point(7, 1))
        self.assertEqual(square_two, Point(0, 7))
        self.assertIsNone(square_three)
        self.assertIsNone(square_four)
        self.assertIsNone(square_five)
        self.assertIsNone(square_six)

        # Testing reverse version of function
        # Good inputs
        turtle_one = board.convert_turtle_to_square(Point(7, 5), 'r')
        turtle_two = board.convert_turtle_to_square(Point(5, 7), 'r')
        # Bad inputs - point is outside of the board in various ways
        turtle_three = board.convert_turtle_to_square(Point(8, 1), 'r')
        turtle_four = board.convert_turtle_to_square(Point(-1, 0), 'r')
        turtle_five = board.convert_turtle_to_square(Point(6, 8), 'r')
        turtle_six = board.convert_turtle_to_square(Point(6, -1), 'r')
        self.assertEqual(turtle_one, Point(196, 75))
        self.assertEqual(turtle_two, Point(96, 175))
        self.assertIsNone(turtle_three)
        self.assertIsNone(turtle_four)
        self.assertIsNone(turtle_five)
        self.assertIsNone(turtle_six)
示例#15
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, [])