示例#1
0
def wtrialpawn(h, sq):
    assert board.whiteToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == man.pawn
    global mvs
    # pawn may move 2 squares on first move
    if rank(sq) == 2 and board.empty(fwd(sq)) and board.empty(fwd(fwd(sq))):
        mvs.append((h, "-", sq, fwd(fwd(sq))))
    # pawn moves only one square fwd if beyond original position and square is empty
    if rank(sq) < 7:
        if board.manAt(fwd(sq)) == 0:
            mvs.append((h, "-", sq, fwd(sq)))
        if man.isBlack(board.manAt(dfr(sq))):
            mvs.append((h, "x", sq, dfr(sq), board.manAt(dfr(sq))))
        if man.isBlack(board.manAt(dfl(sq))):
            mvs.append((h, "x", sq, dfl(sq), board.manAt(dfl(sq))))
    # pawn promotes on reaching last rank
    if rank(sq) == 7:
        if board.manAt(fwd(sq)) == 0:
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "-/", sq, fwd(sq), 0, pmn))
        if man.isBlack(board.manAt(dfr(sq))):
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "x/", sq, dfr(sq), board.manAt(dfr(sq)), pmn))
        if man.isBlack(board.manAt(dfl(sq))):
            for pmn in [man.queen, man.rook, man.bishop, man.knight]:
                mvs.append((h, "x/", sq, dfl(sq), board.manAt(dfl(sq)), pmn))
    # en passant
    if len(board.history) > 0:
        mv = board.lastMove()
        if tag(mv) == "-" and board.manAt(nsq(mv)) == -man.pawn and rank(
                osq(mv)) == 7 and rank(nsq(mv)) == 5:
            if nsq(mv) in [rgt(sq), lft(sq)]:
                mvs.append((h, "xep", sq, fwd(nsq(mv))))
示例#2
0
def umkcapture(mv):
    assert tag(mv) == "x"
    assert man.isMan(mn(mv))
    assert not color(mv) == 'white' or man.isBlack(mn(mv))
    assert not color(mv) == 'black' or man.isWhite(mn(mv))
    assert board.valid()
    umkdash(mv)
    # board.theBoard[nsq(mv)] = cmn(mv)  # WHAT ???
    # note the theBoard will be invalid at this point
    if color(mv) == 'white':
        assert man.isBlack(board.manAt(nsq(mv)))
        board.blackMen.add(nsq(mv))
    else:
        assert man.isWhite(board.manAt(nsq(mv)))
        board.whiteMen.add(nsq(mv))
    # after the above board maintennance it will be valid again
    assert board.valid()
示例#3
0
def btrialrook(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.rook
    global mvs
    for dir in [fwd, rgt, bak, lft]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
示例#4
0
def btrialbishop(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.bishop
    global mvs
    for dir in [dfr, dfl, dbr, dbl]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
示例#5
0
def btrialking(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.king
    global mvs
    for dir in [fwd, dfr, rgt, dbr, bak, dbl, lft, dfl]:
        s = dir(sq)
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        elif man.isWhite(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
        elif man.isBlack(board.manAt(s)):
            pass
    if board.blackAllowOO and board.empty(f8) and board.empty(g8):
        mvs.append((h, "OO", e8))
    if board.blackAllowOOO and board.empty(d8) and board.empty(
            c8) and board.empty(b8):
        mvs.append((h, "OOO", e8))
示例#6
0
def wtrialking(h, sq):
    assert board.whiteToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == man.king
    global mvs
    for dir in [fwd, dfr, rgt, dbr, bak, dbl, lft, dfl]:
        s = dir(sq)
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        elif man.isBlack(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
        elif man.isWhite(board.manAt(s)):
            pass
    if board.whiteAllowOO and board.empty(f1) and board.empty(g1):
        mvs.append((h, "OO", e1))
    if board.whiteAllowOOO and board.empty(d1) and board.empty(
            c1) and board.empty(b1):
        mvs.append((h, "OOO", e1))
示例#7
0
def wtrialknight(h, sq):
    assert board.whiteToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == man.knight
    global mvs
    for s in [
            dfr(fwd(sq)),
            dfr(rgt(sq)),
            dbr(rgt(sq)),
            dbr(bak(sq)),
            dbl(bak(sq)),
            dbl(lft(sq)),
            dfl(lft(sq)),
            dfl(fwd(sq))
    ]:
        if board.empty(s):
            mvs.append((h, "-", sq, s))
        if man.isBlack(board.manAt(s)):
            mvs.append((h, "x", sq, s, board.manAt(s)))
示例#8
0
def btrialqueen(h, sq):
    assert board.blackToMove()
    assert h == len(board.history)
    assert board.manAt(sq) == -man.queen
    global mvs
    for dir in [fwd, rgt, bak, lft, dfr, dfl, dbr, dbl]:
        s = dir(sq)
        done = False
        while not s == None and not done:
            if board.empty(s):
                mvs.append((h, "-", sq, s))
                s = dir(s)
            elif man.isWhite(board.manAt(s)):
                mvs.append((h, "x", sq, s, board.manAt(s)))
                done = True
            elif man.isBlack(board.manAt(s)):
                done = True
            else:
                print "error in btrialqueen"
                sys.exit(1)
示例#9
0
def btrials(h, sqset):
    assert board.blackToMove()
    assert h == len(board.history)
    global mvs
    mvs = []
    for sq in sqset:
        assert man.isBlack(board.manAt(sq))
        if board.manAt(sq) == -man.pawn:
            btrialpawn(h, sq)
        elif board.manAt(sq) == -man.knight:
            btrialknight(h, sq)
        elif board.manAt(sq) == -man.bishop:
            btrialbishop(h, sq)
        elif board.manAt(sq) == -man.rook:
            btrialrook(h, sq)
        elif board.manAt(sq) == -man.queen:
            btrialqueen(h, sq)
        elif board.manAt(sq) == -man.king:
            btrialking(h, sq)
        else:
            print "btrials: square not occupied"
    return mvs
示例#10
0
def mkcapture(mv):
    assert tag(mv) == "x"
    assert not color(mv) == 'white' or man.isBlack(board.manAt(nsq(mv)))
    assert not color(mv) == 'black' or man.isWhite(board.manAt(nsq(mv)))
    assert board.valid()
    assert not man.isKing(board.manAt(nsq(mv)))
    mkdash(mv)
    if color(mv) == 'white':
        board.blackMen.remove(nsq(mv))  # <<--------
        # capturing rooks on original squares affects castling
        if nsq(mv) == h8:
            board.blackAllowOO = False
        elif nsq(mv) == a8:
            board.blackAllowOOO = False
    else:
        board.whiteMen.remove(nsq(mv))
        # capturing rooks on original squares affects castling
        if nsq(mv) == h1:
            board.whiteAllowOO = False
        elif nsq(mv) == a1:
            board.whiteAllowOOO = False
    assert board.valid()
示例#11
0
def valid():
    #return True # disabled to speed up testing
    if not hash == zobrist.hashBoard():
        dump()
    assert hash == zobrist.hashBoard()
    assert theBoard[whiteKingAt] == man.king
    # debug
    if theBoard[blackKingAt] != -man.king:
        print "board 136: dumping"
        dump()
    assert theBoard[blackKingAt] == -man.king
    for sq in xrange(0, 64):
        if manAt(sq) == 0:
            assert (sq in whiteMen) == False
            assert (sq in blackMen) == False
        elif man.isWhite(manAt(sq)):
            assert (sq in whiteMen) == True
            # debug
            if (sq in blackMen):
                print "board 147: manAt(sq)=", manAt(sq)
                print "board 148: sq=", sq
                print "board 149: blackMen=", blackMen
                dump()
            assert (sq in blackMen) == False
        elif man.isBlack(manAt(sq)):
            # debug
            if (sq in whiteMen):
                print "board 155: manAt(sq)=", manAt(sq)
                print "board 156: sq=", sq
                print "board 157: whiteMen=", whiteMen
                dump()
            assert (sq in whiteMen) == False
            assert (sq in blackMen) == True
        else:
            assert False
    return True