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 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
def hashBoard(): h = 0 for sq in board.whiteMen: if not man.isMan(board.theBoard[sq]): print "sq=", sq board.dump() assert man.isMan(board.theBoard[sq]) h = XOR(h, board.theBoard[sq], sq) for sq in board.blackMen: assert man.isMan(board.theBoard[sq]) h = XOR(h, board.theBoard[sq], sq) #print "hashBoard=",h return h
def main(): global autoshow, verbose alphabeta.silent = False board.reset() show() line = raw_input(prompt) toks = line.split(" ") cmd = toks[0] while cmd != "quit": board.valid() # agressive validation # find and execute command if cmd in ["h", "help"]: help() elif cmd in ["s", "show"]: show() elif cmd in ["l", "legals"]: legals() elif cmd in ["a", "allow"]: allow() elif cmd in ["n", "new"]: new() elif cmd in ["", "b", "best"]: mybest() elif cmd in ["r", "random"]: rand() elif cmd in ["hammer"]: hammer() elif cmd in ["showoff"]: autoshow = False elif cmd in ["showon"]: autoshow = True elif cmd in ["verboseon"]: verbose = True elif cmd in ["verboseoff"]: verbose = False elif cmd in ["find"]: if len(toks) == 1: s = raw_input("enter move> ") else: s = toks[1] find(s) elif cmd in ["u", "undo"]: undo() elif cmd in ["d", "dump"]: board.dump() else: trymv(cmd) # get next command line = raw_input(prompt) toks = line.split(" ") cmd = toks[0]
def mkOO(mv): assert tag(mv) == "OO" assert board.valid() if color(mv) == 'white': assert osq(mv) == e1 assert board.whiteAllowOO zobrist.PlaceMan(e1, 0) #board.theBoard[e1] = 0 zobrist.PlaceMan(f1, man.rook) #board.theBoard[f1] = man.rook zobrist.PlaceMan(g1, man.king) #board.theBoard[g1] = man.king zobrist.PlaceMan(h1, 0) #board.theBoard[h1] = 0 board.whiteMen.remove(e1) board.whiteMen.add(f1) board.whiteMen.add(g1) board.whiteMen.remove(h1) board.whiteKingAt = g1 board.whiteAllowOO = False # not need since board.popFlags() will fix board.whiteAllowOOO = False # not need since board.popFlags() will fix else: assert color(mv) == 'black' assert osq(mv) == e8 assert board.blackAllowOO zobrist.PlaceMan(e8, 0) #board.theBoard[e8] = 0 zobrist.PlaceMan(f8, -man.rook) #board.theBoard[f8] = -man.rook zobrist.PlaceMan(g8, -man.king) #board.theBoard[g8] = -man.king zobrist.PlaceMan(h8, 0) #board.theBoard[h8] = 0 board.blackMen.remove(e8) board.blackMen.add(f8) board.blackMen.add(g8) if not h8 in board.blackMen: print "move.271:" board.dump() board.blackMen.remove(h8) board.blackKingAt = g8 board.blackAllowOO = False # not need since board.popFlags() will fix board.blackAllowOOO = False # not need since board.popFlags() will fix board.history[hmc(mv)] = mv assert board.valid()
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
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
#!/usr/bin/python # $Id: test.py 7 2010-01-13 12:16:47Z mark $ # #------------------------------------------------------------------------------- # test #------------------------------------------------------------------------------- import man, board, move from square import * print "\nClear board:" board.clear() board.dump() print "\nInitial position:" board.reset() board.dump() print "\nClear board (again):" board.clear() board.dump() print "\nInitial position (again):" board.reset() board.dump() print "try to add 2nd king (two times)" board.addMan(man.king,27) board.addMan(-man.king,28) BP= -man.pawn