예제 #1
0
    def test_init(self):
        # none
        patterns = [m(), M]
        for pattern in patterns:
            self.assertIsNone(pattern._Move__x)
            self.assertIsNone(pattern._Move__y)
            self.assertEqual(pattern._Move__str, '')
            self.assertEqual(pattern._Move__case, LOWER)

        # x, y, str
        patterns = [m(0, 1), m('a2')]
        for pattern in patterns:
            self.assertEqual(pattern._Move__x, 0)
            self.assertEqual(pattern._Move__y, 1)
            self.assertEqual(pattern._Move__str, 'a2')

        # case
        move = m(case='upper')
        self.assertEqual(move._Move__case, 'upper')

        # put_disc
        board = Board()
        board.put_disc('black', *m('f5'))
        self.assertEqual(board.get_bitboard_info(), (34829500416, 68719476736))
        board.put_disc('white', *m('d6'))
        self.assertEqual(board.get_bitboard_info(), (34561064960, 68988960768))
예제 #2
0
    def test_greedy(self):
        greedy = Greedy()
        board = Board()
        board.put_disc('black', 5, 4)
        board.put_disc('white', 3, 5)
        board.put_disc('black', 2, 4)
        board.put_disc('white', 5, 3)
        board.put_disc('black', 3, 6)
        board.put_disc('white', 6, 4)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 6)
        board.put_disc('black', 6, 3)
        board.put_disc('white', 3, 7)

        self.assertTrue(greedy.next_move('black', board) in [(7, 4), (1, 7)])
예제 #3
0
    def test_unselfish(self):
        unselfish = Unselfish()
        board = Board()
        board.put_disc('black', 5, 4)
        board.put_disc('white', 3, 5)
        board.put_disc('black', 2, 4)
        board.put_disc('white', 5, 3)
        board.put_disc('black', 3, 6)
        board.put_disc('white', 6, 4)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 6)
        board.put_disc('black', 6, 3)
        board.put_disc('white', 3, 7)

        self.assertTrue(
            unselfish.next_move('black', board) in [(7, 5), (4, 6)])
예제 #4
0
    def test_randomopening_next_move(self):
        class TestStrategy:
            def next_move(self, color, board):
                return (0, 0)

        board = Board()
        randomopening = RandomOpening(depth=4, base=TestStrategy())

        # depth = 1 - 4
        for _ in range(2):
            for color in ('black', 'white'):
                move = randomopening.next_move(color, board)
                self.assertNotEqual(move, (0, 0))
                board.put_disc(color, *move)

        # depth = 5
        move = randomopening.next_move('black', board)
        self.assertEqual(move, (0, 0))
예제 #5
0
    def test_switch_next_move(self):
        class Put_5_4(AbstractStrategy):
            def next_move(self, color, board):
                return (5, 4)

        class Put_5_5(AbstractStrategy):
            def next_move(self, color, board):
                return (5, 5)

        class Put_4_5(AbstractStrategy):
            def next_move(self, color, board):
                return (4, 5)

        switch = Switch(
            turns=[0, 1, 2],
            strategies=[Put_5_4(), Put_5_5(), Put_4_5()],
        )

        board = Board()
        move = switch.next_move('black', board)
        self.assertEqual(move, (5, 4))
        board.put_disc('black', *move)

        move = switch.next_move('white', board)
        self.assertEqual(move, (5, 5))
        board.put_disc('white', *move)

        move = switch.next_move('black', board)
        self.assertEqual(move, (4, 5))
        board.put_disc('black', *move)
예제 #6
0
    def test_table_force_import_error(self):
        import os
        import importlib
        import reversi

        # -------------------------------
        # switch environ and reload module
        os.environ['FORCE_TABLEMETHODS_IMPORT_ERROR'] = 'RAISE'
        importlib.reload(reversi.strategies.TableMethods)
        self.assertTrue(reversi.strategies.TableMethods.SLOW_MODE)
        # -------------------------------

        # size8
        table = Table(8)
        board = Board(8)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)
        self.assertEqual(table.get_score(board), -22)

        # -------------------------------
        # recover environment and reload module
        del os.environ['FORCE_TABLEMETHODS_IMPORT_ERROR']
        importlib.reload(reversi.strategies.TableMethods)
        self.assertFalse(reversi.strategies.TableMethods.SLOW_MODE)
예제 #7
0
    def test_to_xy(self):
        patterns = [
            ('a1', (0, 0)),
            ('A1', (0, 0)),  # upper case is also ok.
            ('a2', (0, 1)),
            ('a8', (0, 7)),
            ('b1', (1, 0)),
            ('b2', (1, 1)),
            ('h1', (7, 0)),
            ('h8', (7, 7)),
            ('z1', (25, 0)),
            ('a26', (0, 25)),
            ('z26', (25, 25)),
        ]
        for pattern, expected in patterns:
            self.assertEqual(M.to_xy(pattern), expected)

        board = Board()
        board.put_disc('black', *M.to_xy('f5'))
        self.assertEqual(board.get_bitboard_info(), (34829500416, 68719476736))
        board.put_disc('white', *M.to_xy('d6'))
        self.assertEqual(board.get_bitboard_info(), (34561064960, 68988960768))
예제 #8
0
    def test_slowstarter(self):
        slowstarter = SlowStarter()
        board = Board()
        board.put_disc('black', 5, 4)  # 5
        board.put_disc('white', 3, 5)  # 6
        board.put_disc('black', 2, 4)  # 7
        board.put_disc('white', 5, 3)  # 8
        board.put_disc('black', 3, 6)  # 9

        # unselfish
        self.assertTrue(
            slowstarter.next_move('white', board) in [(6, 4), (1, 5), (
                2, 5), (5, 5), (6, 5), (2, 6)])

        board.put_disc('white', 6, 4)  # 10

        # greedy
        self.assertTrue(slowstarter.next_move('black', board) in [(7, 4)])
예제 #9
0
    def test_number_scorer(self):
        board = Board()
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)

        scorer = coord.NumberScorer()
        self.assertEqual(scorer.get_score(None, board, None, None), -6)
예제 #10
0
    def test_opening_scorer(self):
        board = Board()
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)

        scorer = coord.OpeningScorer()
        self.assertEqual(scorer.get_score(None, board, None, None), -8.25)
예제 #11
0
    def test_winlose_scorer(self):
        board = Board()
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)

        scorer = coord.WinLoseScorer()
        self.assertEqual(scorer.get_score(None, board, None, None), -10006)

        possibility_b = board.get_legal_moves('black')
        possibility_w = board.get_legal_moves('white')
        self.assertEqual(
            scorer.get_score(None, board, possibility_b, possibility_w), None)
예제 #12
0
    def test_possibility_scorer(self):
        board = Board()
        scorer = coord.PossibilityScorer()

        # initial value
        self.assertEqual(scorer._W, 5)
        possibility_b = board.get_bit_count(
            board.get_legal_moves_bits('black'))
        possibility_w = board.get_bit_count(
            board.get_legal_moves_bits('white'))
        self.assertEqual(
            scorer.get_score(None, None, possibility_b, possibility_w), 0)
        board.put_disc('black', 5, 4)
        possibility_b = board.get_bit_count(
            board.get_legal_moves_bits('black'))
        possibility_w = board.get_bit_count(
            board.get_legal_moves_bits('white'))
        self.assertEqual(
            scorer.get_score(None, None, possibility_b, possibility_w), 0)

        # check
        board = Board(8)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)

        possibility_b = board.get_bit_count(
            board.get_legal_moves_bits('black'))
        possibility_w = board.get_bit_count(
            board.get_legal_moves_bits('white'))

        self.assertEqual(
            scorer.get_score(None, None, possibility_b, possibility_w), 5)
예제 #13
0
    def test_table8_score(self):
        table = Table(8)
        board = Board(8)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 2)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 4, 2)
        board.put_disc('black', 1, 1)
        board.put_disc('white', 0, 0)

        self.assertEqual(table.get_score(board), -22)
        self.assertEqual(table.next_move('black', board), (5, 2))
        self.assertEqual(table.next_move('white', board), (2, 5))

        table.next_move('black', Board(4))
        init = [
            [50, -20, -20, 50],  # noqa: E201
            [-20, -1, -1, -20],
            [-20, -1, -1, -20],
            [50, -20, -20, 50],  # noqa: E201
        ]

        self.assertEqual(table.table, init)