Beispiel #1
0
def test_Assemble_ReturnsCorrectPreOrderTraversalTuple_1():
    moves1 = [
        BlackMove(0, 0),
        WhiteMove(0, 1),
        BlackMove(0, 2),
    ]
    moves2 = [
        BlackMove(1, 0),
        WhiteMove(1, 1),
        BlackMove(0, 2),
    ]

    assembler = Assembler(moves1)
    assembler.assemble(moves2)

    actual = to_tuple(assembler)

    expected = (
        Root(),
        BlackMove(0, 0),
        WhiteMove(0, 1),
        BlackMove(0, 2),
        BlackMove(1, 0),
        WhiteMove(1, 1),
        BlackMove(0, 2),
    )

    assert actual == expected
Beispiel #2
0
def test_Assemble_C6FlagEnabled_ReturnsCorrectTuple_case7():
    moves1 = [
        BlackMove(9, 9),
        WhiteMove(9, 8),
        WhiteMove(8, 9),
        BlackMove(8, 8),
        BlackMove(7, 10)
    ]
    moves2 = [
        BlackMove(9, 9),
        WhiteMove(9, 8),
        WhiteMove(8, 9),
        BlackMove(8, 8),
        BlackMove(10, 7)
    ]
    assembler = Assembler(moves1, merge_symmetric_moves=True)
    assembler.assemble(moves2)

    actual = to_tuple(assembler)
    expected = (
        Root(),
        BlackMove(9, 9),
        WhiteMove(8, 9),
        WhiteMove(9, 8),
        BlackMove(8, 8),
        BlackMove(7, 10),
    )
    assert actual == expected
Beispiel #3
0
def test_Assemble_ReturnsCorrectPreOrderTraversalTuple_4():
    assembler = Assembler()
    assembler.assemble([])

    actual = to_tuple(assembler)

    expected = (Root(), )

    assert actual == expected
Beispiel #4
0
def test_DumpTo_ReturnsCorrectSgf_0():
    moves1 = [
        BlackMove(9, 9),
        WhiteMove(8, 10),
        WhiteMove(10, 10),
    ]
    moves2 = [
        BlackMove(9, 9),
        WhiteMove(8, 8),
        WhiteMove(9, 10),
    ]

    assembler = Assembler(moves1,
                          "_sample_url_",
                          game_results="BWin",
                          merge_symmetric_moves=True)
    assembler.assemble(moves2, "_sample_url_", "WWin")

    # We use this object to emulate a `file`, so no additional file will be created when unit-testing
    file = io.StringIO()
    dump_to(assembler, file, editor_style=False)
    actual = file.getvalue()
    print(actual)
    expected = '''\
(;GM[511]C[Visit Count = 2
BWin count  = 1
WWin count  = 1
Draw count  = 0
];B[JJ]C[Visit Count = 2
BWin count  = 1
WWin count  = 1
Draw count  = 0
WinRate     = 50.00%
];W[II]C[Visit Count = 2
BWin count  = 1
WWin count  = 1
Draw count  = 0
WinRate     = 50.00%
](;W[IK]C[BWin count  = 1
WWin count  = 0
Draw count  = 0
WinRate     = 0.00%
Game urls   = _sample_url_
])(;W[JK]C[BWin count  = 0
WWin count  = 1
Draw count  = 0
WinRate     = 100.00%
Game urls   = _sample_url_
]))C[Visit Count = 2
BWin count  = 1
WWin count  = 1
Draw count  = 0
]\
'''

    assert actual == expected
Beispiel #5
0
def test_dumpTo_editorStyleEnabled_ReturnsCorrectSgf_0():
    moves1 = [BlackMove(9, 9), WhiteMove(0, 0), WhiteMove(0, 1)]
    moves2 = [BlackMove(9, 8), WhiteMove(0, 0), WhiteMove(0, 1)]
    assembler = Assembler(moves1,
                          game_results="WWin",
                          merge_symmetric_moves=True)
    assembler.assemble(moves2, game_results="BWin")

    file = io.StringIO()
    dump_to(assembler, file, editor_style=True)
    actual = file.getvalue()
    print(actual)
    expect = '''\
(;GM[511](;B[JJ]C[BWin count  = 0
WWin count  = 1
Draw count  = 0
WinRate     = 0.00%
];W[AA]C[BWin count  = 0
WWin count  = 1
Draw count  = 0
WinRate     = 100.00%
];W[AB]C[BWin count  = 0
WWin count  = 1
Draw count  = 0
WinRate     = 100.00%
Game urls   = _sample_url_
])(;B[IJ]C[BWin count  = 1
WWin count  = 0
Draw count  = 0
WinRate     = 100.00%
];W[AA]C[BWin count  = 1
WWin count  = 0
Draw count  = 0
WinRate     = 0.00%
];W[BA]C[BWin count  = 1
WWin count  = 0
Draw count  = 0
WinRate     = 0.00%
Game urls   = _sample_url_
]))C[Visit Count = 2
BWin count  = 1
WWin count  = 1
Draw count  = 0
]\
'''
    assert actual == expect
Beispiel #6
0
def test_topMoves_chooseTop2_ReturnListsOfMoves():
    moves1 = [
        BlackMove(9, 9),
        WhiteMove(0, 0),
        WhiteMove(0, 2),
        BlackMove(0, 3),
        BlackMove(0, 4)
    ]
    moves2 = [
        BlackMove(9, 9),
        WhiteMove(0, 0),
        WhiteMove(0, 2),
        BlackMove(0, 3),
        BlackMove(0, 4)
    ]
    moves3 = [BlackMove(9, 9), WhiteMove(0, 0), WhiteMove(0, 1)]
    moves4 = [BlackMove(9, 9), WhiteMove(0, 0), WhiteMove(0, 1)]
    moves5 = [BlackMove(9, 9), WhiteMove(0, 2), WhiteMove(8, 8)]

    assembler = Assembler(moves1, merge_symmetric_moves=True)
    assembler.assemble(moves2)
    assembler.assemble(moves3)
    assembler.assemble(moves4)
    assembler.assemble(moves5)

    actual = assembler.top_n_moves(2)
    expected = [';;B[JJ];W[AA];W[AC]', ';;B[JJ];W[AA];W[AB]']

    assert actual == expected
Beispiel #7
0
def test_Ctor_ReturnsCorrectPreOrderTraversalTuple():
    moves = [
        BlackMove(0, 0),
        WhiteMove(0, 1),
        BlackMove(0, 2),
    ]

    assembler = Assembler(moves)

    actual = to_tuple(assembler)
    expected = (Root(), BlackMove(0, 0), WhiteMove(0, 1), BlackMove(0, 2))

    assert actual == expected
    assert type(actual) == tuple
Beispiel #8
0
def test_Assemble_C6FlagEnabled_ReturnsCorrectTuple_case1():
    moves1 = [
        BlackMove(9, 9),
        WhiteMove(11, 7),
        WhiteMove(12, 6),
        BlackMove(11, 11),
        BlackMove(12, 12),
        WhiteMove(11, 3),
        WhiteMove(12, 3),
        BlackMove(9, 3),
        BlackMove(8, 3)
    ]

    moves2 = [
        BlackMove(9, 9),
        WhiteMove(7, 11),
        WhiteMove(6, 12),
        BlackMove(11, 11),
        BlackMove(12, 12),
        WhiteMove(11, 3),
        WhiteMove(12, 3),
        BlackMove(9, 3),
        BlackMove(8, 3)
    ]

    assembler = Assembler(moves1, merge_symmetric_moves=True)
    assembler.assemble(moves2)
    expected = (Root(), BlackMove(9, 9), WhiteMove(6, 6), WhiteMove(7, 7),
                BlackMove(6, 12), BlackMove(7, 11), WhiteMove(6, 3),
                WhiteMove(7, 3), BlackMove(9,
                                           3), BlackMove(10,
                                                         3), WhiteMove(15, 11),
                WhiteMove(15, 12), BlackMove(15, 8), BlackMove(15, 9))

    actual = to_tuple(assembler)

    assert actual == expected
Beispiel #9
0
def test_Assemble_mergeSymmetricMoves_ReturnsCorrectPreOrderTraversalTuple_0():
    moves1 = [BlackMove(0, 0), WhiteMove(2, 1), BlackMove(0, 2)]
    moves2 = [
        BlackMove(0, 18),
        WhiteMove(1, 16),
        BlackMove(2, 18),
    ]  # Rotate 90%
    moves3 = [
        BlackMove(18, 18),
        WhiteMove(16, 17),
        BlackMove(18, 16),
    ]  # Rotate 180%
    moves4 = [
        BlackMove(18, 0),
        WhiteMove(17, 2),
        BlackMove(16, 0),
    ]  # Rotate 270%
    moves5 = [
        BlackMove(0, 18),
        WhiteMove(2, 17),
        BlackMove(0, 16),
    ]  # Horizontal reflection

    assembler = Assembler(moves1, merge_symmetric_moves=True)
    assembler.assemble(moves2)
    assembler.assemble(moves3)
    assembler.assemble(moves4)
    assembler.assemble(moves5)

    actual = to_tuple(assembler)

    expected = (
        Root(),
        BlackMove(0, 0),
        WhiteMove(1, 2),
        BlackMove(2, 0),
    )

    assert actual == expected
Beispiel #10
0
    "Get common openings from the assembled tree and dump them to 'openings.txt'"
)

if __name__ == '__main__':
    args = parser.parse_args()

    if not os.path.exists(args.json_src):
        print(f"Error! The json src file {args.json_src} does not exist!")
        exit(-1)

    kifus = Extractor().extract(args.json_src, "kifu")
    urls = Extractor().extract(args.json_src, "url")
    game_results = Extractor().extract(args.json_src, "game_result")

    print("Assembling to a tree...")
    assembler = Assembler(
        merge_symmetric_moves=args.enable_symmetrical_assembling, )

    with tqdm.tqdm(total=len(kifus)) as pbar:
        number_of_skipped_moves = 0
        for kifu, url, game_results in zip(kifus, urls, game_results):
            # Use Kifuparser to parse the raw string into sequence of move
            moves = KifuParser.parse(kifu)

            if len(moves) < args.lower_bound:
                number_of_skipped_moves += 1
            else:
                assembler.assemble(moves, url, game_results)

            pbar.update(1)

        print(