コード例 #1
0
def AlphaBetaWithMemory(alpha, beta, d):
    if TT.has_key(board.hash):
        lowerbound, upperbound = TT[board.hash]
        if lowerbound >= beta:
            return lowerbound
        if upperbound <= alpha:
            return upperbound
        alpha = max(alpha, lowerbound)
        beta = min(beta, upperbound)
    if d == 0:
        g = static.eval()
    elif board.whiteToMove():
        g, a = -inf, alpha  # save original alpha value
        for mv in legal.moves():
            if g >= beta:
                break
            move.make(mv)
            g = max(g, AlphaBetaWithMemory(a, beta, d - 1))
            move.umake(mv)
            a = max(a, g)
    else:
        assert board.blackToMove()
        g, b = +inf, beta  #save original beta value
        for mv in legal.moves():
            if g <= alpha:
                break
            move.make(mv)
            g = min(g, AlphaBetaWithMemory(alpha, b, d - 1))
            move.umake(mv)
    # transition table storing of bounds
    # fail low implies an upper bound
    if g <= alpha:
        if TT.has_key(board.hash):
            lower, upper = TT[board.hash]
        else:
            lower, upper = -inf, +inf
        TT[board.hash] = lower, g
    # found an accurate minimax value - will not occur if called with zero window
    if g > alpha and g < beta:
        TT[board.hash] = g, g
    # fail high result implies a lower bound
    if g > beta:
        if TT.has_key(board.hash):
            lower, upper = TT[board.hash]
        else:
            lower, upper = -inf, +inf
        TT[board.hash] = g, upper
    return g
コード例 #2
0
ファイル: main_random.py プロジェクト: markcornwell/pychess
def legals():
    mvs = legal.moves()
    for mv in mvs:
        if board.whiteToMove():
            print "%s %s" % (move.num(mv), san.pr(mv, mvs))
        else:
            print "%s ...%s" % (move.num(mv), san.pr(mv, mvs))
コード例 #3
0
def mybest_static(silent=False):
    mvlist = legal.moves()
    if len(mvlist) > 0:
        if board.whiteToMove():
            mv, score = best.forWhite(mvlist)
        else:
            mv, score = best.forBlack(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                msg = "After %s %s [%s]" % (move.num(mv), san.pr(
                    mv, mvlist), score)
            else:
                msg = "After %s ...%s [%s]" % (move.num(mv), san.pr(
                    mv, mvlist), score)
        move.make(mv)
        if not silent:
            print msg
            auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
コード例 #4
0
ファイル: main_random.py プロジェクト: markcornwell/pychess
def trymv(s):
    mvs = legal.moves()
    found = algebraic.find(s, mvs)
    if len(found) == 0:
        print "not found"
    elif len(found) == 1:
        move.make(found[0])
        auto()
    elif len(found) > 1:
        print "ambiguous"
コード例 #5
0
ファイル: mediate1.py プロジェクト: markcornwell/pychess
def trymv(s):
    assert playing
    mvs = legal.moves()
    found = algebraic.find(s, mvs)
    if len(found) == 0:
        put_illegalmove(s, "not found")
    elif len(found) == 1:
        move.make(found[0])
        reply()  # sends the random response
    elif len(found) > 1:
        put_illegalmove(s, "ambiguous")
コード例 #6
0
def moves(d):
    mvlist = legal.moves()
    if d > 0:
        # sort captures to the front of the list
        mvlist.sort(cmp)
        return mvlist
    elif d > depth2:
        #  after depth limit reach, we only look at a captures
        return filter(lambda mv: move.tag(mv) in ["x", "x/", "xep"], mvlist)
    else:
        return []
コード例 #7
0
ファイル: main_random.py プロジェクト: markcornwell/pychess
def find(s):
    mvs = legal.moves()
    found = algebraic.find(s, mvs)
    if len(found) == 0:
        print " " * 13 + "no such move"
    if len(found) == 1:
        print " " * 13 + "found unique", san.pr(found[0], mvs)
    elif len(found) > 1:
        print " " * 13 + "ambiguous, choices are:"
        for mv in found:
            print " " * 17, san.pr(mv, mvs)
    return found
コード例 #8
0
ファイル: mediate.py プロジェクト: markcornwell/pychess
def report_result():
    assert len(legal.moves()) == 0
    if board.whiteToMove():
        if blackAttacks(board.whiteKingAt):
            put_result("1-0", "black mates")
        else:
            put_result("1/2-1/2", "stalemate")
    else:
        assert board.blackToMove()
        if whiteAttacks(board.blackKingAt):
            put_result("0-1", "white mates")
        else:
            put_result("1/2-1/2", "stalemate")
コード例 #9
0
ファイル: mediate.py プロジェクト: markcornwell/pychess
def opponent_move(s):
    assert engineColor in ['white', 'black', None]
    assert board.colorToMove() != engineColor
    mvs = legal.moves()
    found = algebraic.find(s, mvs)
    if len(found) == 0:
        put_illegalmove(s, "not found")
    elif len(found) == 1:
        assert board.colorToMove() != engineColor
        assert move.color(found[0]) != engineColor
        move.make(found[0])
        if board.colorToMove() == engineColor:
            assert board.colorToMove() == engineColor
            reply()
    elif len(found) > 1:
        put_illegalmove(s, "ambiguous")
コード例 #10
0
ファイル: mediate.py プロジェクト: markcornwell/pychess
def reply():
    #if not board.colorToMove()==engineColor:
    #    return None
    assert board.colorToMove() == engineColor
    mvs = legal.moves()
    #time.sleep(0.1)
    if len(mvs) > 0:
        if len(board.history) > 300:
            resign()
        else:
            mv, score, cnt, sec = alphabeta.best(mvs)
            s = san.pr(mv, mvs)
            put_move(s)
            move.make(mv)
    else:
        assert len(mvs) == 0
        report_result()
コード例 #11
0
def prline(mvs):
    s = ""
    c = 0
    for mv in mvs:
        legals = legal.moves()
        if move.color(mv) == 'white':
            s = s + ("%s %s" % (num(mv), san.pr(mv, legals)))
        else:
            if c == 0:
                s = "%s ...%s " % (num(mv), san.pr(mv, legals))
            else:
                s = s + (" %s " % san.pr(mv, legals))
        move.make(mv)
        c = c + 1
    while c > 0:
        move.umake(board.lastMove())
        c = c - 1
    return s
コード例 #12
0
ファイル: main_random.py プロジェクト: markcornwell/pychess
def rand(silent=False):
    mvlist = legal.moves()
    if len(mvlist) > 0:
        #n = random.randint(0,len(mvlist)-1)
        mv = random.choice(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                msg = "After %s %s" % (move.num(mv), san.pr(mv, mvlist))
            else:
                msg = "After %s ...%s" % (move.num(mv), san.pr(mv, mvlist))
        move.make(mv)
        if not silent:
            print msg
            auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
コード例 #13
0
def mybest():
    mvlist = legal.moves()
    if len(mvlist) > 0:
        #mv,score,cnt,sec = alphabeta.best(mvlist)
        mv, score, cnt, sec = searchfunction(mvlist)
        if not silent:
            if move.color(mv) == 'white':
                fmt = "After %s %s [%s] searched %s positions in %s sec"
            else:
                fmt = "After %s ...%s [%s] searched %s positions in %s sec"
            print fmt % (move.num(mv), san.pr(mv, mvlist), score, cnt, sec)
            print "ttHits=%s   |WTT|=%s  |BTT|=%s" % (
                alphabeta.ttHits, len(alphabeta.WTT), len(alphabeta.BTT))
        move.make(mv)
        auto()
        if verbose:
            board.dump()
            assert board.valid()  # aggressive validation
        return True
    else:
        print "no moves"
        return False
コード例 #14
0
def eval():
    mvs = legal.moves()
    s = legal.state(mvs)
    assert s in ['whiteMates', 'blackMates', 'stalemate', 'continue']
    if s == 'whiteMates':
        return +inf  # black has been mated
    elif s == 'blackMates':
        return -inf  # white has been mated
    elif s == 'stalemate':
        return 0
    else:
        assert s == 'continue'
        ma = wmaterial() + bmaterial()
        c = wcenter() + bcenter()
        p = wpenalty() + bpenalty()
        b = wbonus() + bbonus()
        mo = wmobility(mvs) + bmobility(mvs)
        k = wkingsafety() + bkingsafety()
        #print "58: k=%s m=%s eval=%s" % (k,m,k+m)
        tot = 3 * ma + c + p + b + mo
        #tot = ma
        return tot + 0.1 * random.randint(0, 9)
コード例 #15
0
ファイル: mediate1.py プロジェクト: markcornwell/pychess
def reply():
    global playing
    assert playing
    mvs = legal.moves()
    if len(mvs) > 0:
        mv = random.choice(mvs)
        s = san.pr(mv, mvs)  # use a different notation
        put_move(s)
        move.make(mv)
    else:
        assert len(mvs) == 0
        playing = False
        if board.whiteToMove():
            if blackAttacks(board.whiteKingAt):
                put_result("1-0", "white checkmated")
            else:
                put_result("1/2-1/2", "stalemate")
        else:
            assert board.blackToMove()
            if whiteAttacks(board.blackKingAt):
                put_result("0-1", "black checkmated")
            else:
                put_result("1/2-1/2", "stalemate")