Beispiel #1
0
def selfplay():
    """ Start a game sunfish vs. sunfish """
    wa, ba = (['C', 'N', 'E', 'R', 'T',
               'A'])[random.randint(0, 5)], (['c', 'n', 'e', 'r', 't',
                                              'a'])[random.randint(0, 5)]
    #pos = xboard.parseFEN('{}{} 33 rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'.format(wa, ba))
    pos = xboard.parseFEN(
        'Ca 33 rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
    print(' '.join(pos.board))
    print('#' * 10)
    for d in range(100):
        move, score = sunfish.search(pos, maxn=1e3)
        pos = pos.move(move)
        # Always print the board from the same direction
        board = pos.rotate().board if pos.color else pos.board
        #board = pos.board
        print(' '.join(board))
        print("{}. {}: {}".format(d + 1, (['w', 'b'])[not pos.color],
                                  xboard.mrender(pos, move)))
        print("\n" + str(xboard.printFEN(pos)))
        if pos.second:
            print("second initiated")
            move, score = sunfish.search(pos, maxn=1e2)
            if move:
                print("move found")
                board = pos.rotate().board if pos.color else pos.board
                print(' '.join(board))
                print("{}. {}: {}".format(d + 1, (['w', 'b'])[not pos.color],
                                          xboard.mrender(pos, move)))
                print("\n" + str(xboard.printFEN(pos)))
                pos = pos.move(move)
            else:
                score = 0
                pos = sunfish.Position(pos.board, pos.color, False, pos.score,
                                       pos.wa, pos.ba, pos.ws, pos.bs, pos.wc,
                                       pos.bc, pos.ep, pos.kp)
                pos = pos.rotate()
        if score <= -30000:
            print("Game over. White wins.")
            break
        if score >= 30000:
            print("Game over. Black wins.")
            break
        print('#' * 10)
Beispiel #2
0
def selfplay():
    """ Start a game sunfish vs. sunfish """
    wa, ba = (['C', 'N', 'E', 'R', 'T', 'A'])[random.randint(0, 5)], (['c', 'n', 'e', 'r', 't', 'a'])[random.randint(0, 5)]
    #pos = xboard.parseFEN('{}{} 33 rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'.format(wa, ba))
    pos = xboard.parseFEN('Ca 33 rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
    print(' '.join(pos.board))
    print('#' * 10)
    for d in range(100):
        move, score = sunfish.search(pos, maxn=1e3)
        pos = pos.move(move)
        # Always print the board from the same direction
        board = pos.rotate().board if pos.color else pos.board
        #board = pos.board
        print(' '.join(board))
        print("{}. {}: {}".format(d+1, (['w', 'b'])[not pos.color], xboard.mrender(pos, move)))
        print("\n" + str(xboard.printFEN(pos)))
        if pos.second:
            print("second initiated")
            move, score = sunfish.search(pos, maxn=1e2)
            if move:
                print("move found")
                board = pos.rotate().board if pos.color else pos.board
                print(' '.join(board))
                print("{}. {}: {}".format(d+1, (['w', 'b'])[not pos.color], xboard.mrender(pos, move)))
                print("\n" + str(xboard.printFEN(pos)))
                pos = pos.move(move)
            else:
                score = 0
                pos = sunfish.Position(
                        pos.board, pos.color, False, pos.score,
                        pos.wa, pos.ba, pos.ws, pos.bs,
                        pos.wc, pos.bc, pos.ep, pos.kp)
                pos = pos.rotate()
        if score <= -30000:
            print("Game over. White wins.")
            break
        if score >= 30000:
            print("Game over. Black wins.")
            break
        print('#' * 10)
Beispiel #3
0
def perft(pos, depth, divide=False):
    if depth == 0:
        return 1
    res = 0
    for m in pos.genMoves():
        pos1 = pos.move(m)
        # Make sure the move was legal
        if not any(pos1.value(m) >= sunfish.MATE_VALUE for m in pos1.genMoves()):
            sub = perft(pos1, depth-1, False)
            if divide:
                print(" "*depth+xboard.mrender(m), sub)
            res += sub
    return res
Beispiel #4
0
def selfplay(maxn=200):
    """ Start a game sunfish vs. sunfish """
    pos = xboard.parseFEN(xboard.FEN_INITIAL)
    for d in range(200):
        # Always print the board from the same direction
        board = pos.board if d % 2 == 0 else pos.rotate().board
        print(' '.join(board))
        m, _ = sunfish.search(pos, maxn)
        if m is None:
            print("Game over")
            break
        print("\nmove", xboard.mrender(d%2, pos, m))
        pos = pos.move(m)
Beispiel #5
0
def selfplay():
    """ Start a game sunfish vs. sunfish """
    pos = xboard.parseFEN('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
    for d in range(200):
        # Always print the board from the same direction
        board = pos.board if d % 2 == 0 else pos.rotate().board
        print(' '.join(board))

        m, _ = sunfish.search(pos, maxn=200)
        if m is None:
            print("Game over")
            break
        print("\nmove", xboard.mrender(d%2, pos, m))
        pos = pos.move(m)
Beispiel #6
0
def perft(pos, depth, divide=False):
    if depth == 0:
        return 1
    res = 0
    for m in pos.genMoves():
        pos1 = pos.move(m)
        # Make sure the move was legal
        if not any(
                pos1.value(m) >= sunfish.MATE_VALUE for m in pos1.genMoves()):
            sub = perft(pos1, depth - 1, False)
            if divide:
                print(" " * depth + xboard.mrender(m), sub)
            res += sub
    return res
Beispiel #7
0
def selfplay(maxn=200):
    """ Start a game sunfish vs. sunfish """
    pos = xboard.parseFEN(
        'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
    for d in range(200):
        # Always print the board from the same direction
        board = pos.board if d % 2 == 0 else pos.rotate().board
        print(' '.join(board))

        m, _ = sunfish.search(pos, maxn)
        if m is None:
            print("Game over")
            break
        print("\nmove", xboard.mrender(d % 2, pos, m))
        pos = pos.move(m)