def bkingsafety(): safety = 0 k = board.blackKingAt for sq in [fwd(k), dfr(k), rgt(k), dbr(k), bak(k), dbl(k), lft(k), dfl(k)]: if whiteAttacks(sq): safety = safety + kingSafeWt return safety
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 moves(): assert manAt(board.whiteKingAt) == king assert manAt(board.blackKingAt) == -king legalmoves = [] for mv in trial.trialmoves(): make(mv) assert manAt(board.whiteKingAt) == king assert manAt(board.blackKingAt) == -king if blackToMove(): if isOO(mv): if not blackAttacks(e1) and not blackAttacks(f1) and not blackAttacks(g1): legalmoves.append(mv) elif 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 whiteToMove(): if isOO(mv): if not whiteAttacks(e8) and not whiteAttacks(f8) and not whiteAttacks(g8): legalmoves.append(mv) elif 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) 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 state(legals): if len(legals)==0: if blackToMove() and whiteAttacks(board.blackKingAt): return 'whiteMates' elif whiteToMove() and blackAttacks(board.whiteKingAt): return 'blackMates' else: return 'stalemate' else: return 'continue'
def displaywhiteattacks(): for r in xrange(7,-1,-1): print "\nMap of squares under attack by white" for f in xrange(0,8): i = r*8+f if attack.whiteAttacks(i): print "WA", else: print "--", if hfile(i): print
def mate(legals): assert False # do not call if len(legals)==0: if board.blackToMove() and attack.whiteAttacks(board.blackKingAt): return True elif board.whiteToMove() and attack.blackAttacks(board.whiteKingAt): return True else: return False else: return False
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")
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")