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 addMan(m, sq): global theBoard, whiteKingAt, blackKingAt, whiteMen, blackMen if not man.isMan(m): print "addman: bad value for man: %s" % m elif not square.isq(sq): print "addman: bad value for square: %s" % sq elif man.isPawn(m) and (square.lastrank(sq) or square.firstrank(sq)): print "addman: pawns not allowed on rank" % rank(sq) elif occupied(sq): print "addman: square %s is occupied" % sq elif man.isWhiteKing(m) and not (whiteKingAt == 999): print "addman: only one white king allowed" elif man.isBlackKing(m) and not (blackKingAt == 999): print "addman: only one black king allowed" else: #assert hash == zobrist.hashBoard() if man.isWhite(m): if man.isKing(m): whiteKingAt = sq whiteMen.add(sq) else: if man.isKing(m): blackKingAt = sq blackMen.add(sq) zobrist.PlaceMan(sq, m)
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()
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
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
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))
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))
def btrialknight(h, sq): assert board.blackToMove() 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)) elif man.isWhite(board.manAt(s)): mvs.append((h, "x", sq, s, board.manAt(s)))
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)
def wtrials(h, sqset): assert board.whiteToMove() assert h == len(board.history) global mvs mvs = [] for sq in sqset: assert man.isWhite(board.manAt(sq)) if board.manAt(sq) == man.pawn: wtrialpawn(h, sq) elif board.manAt(sq) == man.knight: wtrialknight(h, sq) elif board.manAt(sq) == man.bishop: wtrialbishop(h, sq) elif board.manAt(sq) == man.rook: wtrialrook(h, sq) elif board.manAt(sq) == man.queen: wtrialqueen(h, sq) elif board.manAt(sq) == man.king: wtrialking(h, sq) else: print "wtrials: square not occupied" return mvs
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()
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