def btrialpawn(h, sq): assert board.blackToMove() assert h == len(board.history) assert board.manAt(sq) == -man.pawn global mvs # pawn may move 2 squares on first move if rank(sq) == 7 and board.empty(bak(sq)) and board.empty(bak(bak(sq))): mvs.append((h, "-", sq, bak(bak(sq)))) # pawn moves only one square if beyond original position if rank(sq) > 2: if board.manAt(bak(sq)) == 0: mvs.append((h, "-", sq, bak(sq))) if man.isWhite(board.manAt(dbr(sq))): mvs.append((h, "x", sq, dbr(sq), board.manAt(dbr(sq)))) if man.isWhite(board.manAt(dbl(sq))): mvs.append((h, "x", sq, dbl(sq), board.manAt(dbl(sq)))) # pawn promotes on reaching last rank if rank(sq) == 2: if board.manAt(bak(sq)) == 0: for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]: mvs.append((h, "-/", sq, bak(sq), 0, pmn)) if man.isWhite(board.manAt(dbr(sq))): for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]: mvs.append((h, "x/", sq, dbr(sq), board.manAt(dbr(sq)), pmn)) if man.isWhite(board.manAt(dbl(sq))): for pmn in [-man.queen, -man.rook, -man.bishop, -man.knight]: mvs.append((h, "x/", sq, dbl(sq), board.manAt(dbl(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)) == 2 and rank(nsq(mv)) == 4: if nsq(mv) in [rgt(sq), lft(sq)]: mvs.append((h, "xep", sq, bak(nsq(mv))))
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))))
def moves(): assert manAt(board.whiteKingAt) == king assert manAt(board.blackKingAt) == -king legalmoves = [] for mv in trial.trialmoves(): if not (move.tag(mv) in ["OO","OOO"]) and man.isKing(manAt(move.nsq(mv))): print "legal.19: mv=",mv board.dump() move.make(mv) assert manAt(board.whiteKingAt) == king assert manAt(board.blackKingAt) == -king if board.blackToMove(): if move.isOO(mv): if not blackAttacks(e1) and not blackAttacks(f1) and not blackAttacks(g1): legalmoves.append(mv) elif move.isOOO(mv): if not blackAttacks(e1) and not blackAttacks(d1) and not blackAttacks(c1): legalmoves.append(mv) elif not blackAttacks(board.whiteKingAt): legalmoves.append(mv) elif board.whiteToMove(): if move.isOO(mv): if not whiteAttacks(e8) and not whiteAttacks(f8) and not whiteAttacks(g8): legalmoves.append(mv) elif move.isOOO(mv): if not whiteAttacks(e8) and not whiteAttacks(d8) and not whiteAttacks(c8): legalmoves.append(mv) elif not whiteAttacks(board.blackKingAt): legalmoves.append(mv) move.umake(mv) assert manAt(board.whiteKingAt) == king assert manAt(board.blackKingAt) == -king assert noKingCapture(legalmoves) # you never see a king captured in any legal move return legalmoves
def prdashPiece(mv, mvs): assert tag(mv) == "-" assert isPiece(manAt(osq(mv))) # simplest form - pln p = man.pr(manAt(osq(mv))) ln = square.pr(nsq(mv)) s = "%s%s" % (p, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # disabiguate by file - pfln f = "abcdefgh"[col(osq(mv))] s = "%s%s%s" % (p, f, ln) found = algebraic.find(s, mvs) if (len(found)) == 1: return s # disambiguate by rank - prln r = "12345678"[row(osq(mv))] s = "%s%s%s" % (p, r, ln) found = algebraic.find(s, mvs) if (len(found)) == 1: return s # disambiguate by both -rare but possible - prfln s = "%s%s%s%s" % (p, r, f, ln) if (len(found)) == 1: return s # there should be no other cases given that move is legal assert False
def prcapturePiece(mv, mvs): assert tag(mv) in ["x", "x/", "xep"] assert isPiece(manAt(osq(mv))) # simplest form - pxln p = man.pr(manAt(osq(mv))) ln = square.pr(nsq(mv)) s = "%sx%s" % (p, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # disambiguate by file - pfxln f = "abcdefgh"[col(osq(mv))] s = "%s%sx%s" % (p, f, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # disambiguate by rank - prxln r = "12345678"[row(osq(mv))] s = "%s%sx%s" % (p, r, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # disambiguate by both - prfxln s = "%s%s%sx%s" % (p, r, f, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # if move is legal there should be no other cases assert False
def prcapturePawn(mv, mvs): assert tag(mv) in ["x", "x/", "xep"] assert isPawn(manAt(osq(mv))) # simplest form - fxln f = "abcdefgh"[col(osq(mv))] ln = square.pr(nsq(mv)) if tag(mv) == "x/": q = man.pr(pmn(mv)) s = "%sx%s=%s" % (f, ln, q) else: s = "%sx%s" % (f, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # disambiguate by both - rfxln rf = square.pr(osq(mv)) if tag(mv) == "x/": s = "%sx%s=%s" % (rf, ln, q) else: s = "%sx%s" % (rf, ln) found = algebraic.find(s, mvs) if len(found) == 1: return s # if move is legal there should be no other cases assert False
def noKingCapture(mvs): for mv in mvs: if move.tag(mv) in ["x","x/"]: if man.isKing(move.cmn(mv)): print "error (king capture): ",mv return False if man.isKing(manAt(move.nsq(mv))): print "error (king at nsq): ",mv return False return True
def prdashPawn(mv, mvs): assert tag(mv) == "-" or tag(mv) == "-/" assert isPawn(manAt(osq(mv))) # simplest form - ln ln = square.pr(nsq(mv)) if tag(mv) == "-/": q = man.pr(pmn(mv)) s = "%s=%s" % (ln, q) else: s = ln found = algebraic.find(s, mvs) if len(found) == 1: return s
'P': man.pawn } atbl = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8} ntbl = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8} #------------------------------------------------------------------------------ # Each pattern corresponds to a function we can use to filter the moves. # The function is given as a lambda expression that operates on # a move tuple mv evaluating to truth if that tuple matches the move string s. # See header comment in move.py for the format of the move tuples #------------------------------------------------------------------------------ fm = { "ln": lambda s, mv: tag(mv) == "-" and nsq(mv) == rd(s) and isPawn(manAt(osq(mv)) ), "pln": lambda s, mv: tag(mv) == "-" and nsq(mv) == rd(s[1:3]) and isKind( manAt(osq(mv)), kind[s[0]]), "pxln": lambda s, mv: tag(mv) == "x" and nsq(mv) == rd(s[2:4]) and isKind( manAt(osq(mv)), kind[s[0]]), "lxln": lambda s, mv: tag(mv) in ["x", "xep"] and nsq(mv) == rd(s[2:4]) and file( osq(mv)) == atbl[s[0]] and isPawn(manAt(osq(mv))), "lnxln": lambda s, mv: tag(mv) in ["x", "xep"] and nsq(mv) == rd(s[3:5]) and osq(mv) == rd(s[0:2]), "O-O": lambda s, mv: tag(mv) == "OO",