def TestCastlingMoveGen(): zboard = fen.ZBoardFromFen(FEN_BlackCanQSCastle) moves = MoveGen.GetMoves(zboard) moves = filter(lambda m: m.meta in [KING_CASTLE, QUEEN_CASTLE], [MoveInfo.FromInt32(move) for move in moves]) print '---black QS castle:' for move in moves: testMakeUnmakeMove(zboard, move.int32) pdb.set_trace() zboard = fen.ZBoardFromFen(FEN_BlackCanCastle) moves = MoveGen.GetMoves(zboard) moves = filter(lambda m: m.meta in [KING_CASTLE, QUEEN_CASTLE], [MoveInfo.FromInt32(move) for move in moves]) print '---black castling moves:' for move in moves: print move testMakeUnmakeMove(zboard, move.int32) zboard = fen.ZBoardFromFen(FEN_WhiteCanCastle) moves = MoveGen.GetMoves(zboard) moves = filter(lambda m: m.meta in [KING_CASTLE, QUEEN_CASTLE], [MoveInfo.FromInt32(move) for move in moves]) print '---white castling moves:' for move in moves: testMakeUnmakeMove(zboard, move.int32) zboard = fen.ZBoardFromFen(FEN_BlackBlockingWhitesCastle) moves = MoveGen.GetMoves(zboard) moves = filter(lambda m: m.meta in [KING_CASTLE, QUEEN_CASTLE], [MoveInfo.FromInt32(move) for move in moves]) print '---white has blocked castling:' for move in moves: testMakeUnmakeMove(zboard, move.int32)
def testFENImport(): FEN_pos1 = "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2" FEN_pos2 = "r1b1k2r/ppp1npb1/2n4p/3BP2q/3P1ppP/2N2N2/PPP3P1/R1BQ2KR w kq - 0 12" FEN_pos3 = "6r1/2p1k2b/p2b1p1p/P1nPp1r1/1pB5/1P3PP1/R5NP/2N1KR2 w - - 5 24" zboard = fen.ZBoardFromFen(FEN_pos2) print BoardSquaresToString(zboard.squares) pdb.set_trace() zboard = fen.ZBoardFromFen(FEN_pos3) print BoardSquaresToString(zboard.squares) pdb.set_trace()
def TestMakeAllMoves(): import random random.seed(0) for j in range(len(allfens)): zboard = fen.ZBoardFromFen(allfens[j]) moves = MoveGen.GetMoves(zboard) for i in range(len(moves)): testMakeUnmakeMove(zboard, moves[i])
def TestPromoMoveGen(): zboard = fen.ZBoardFromFen(FEN_CapPromo) moves = MoveGen.GetMoves(zboard) promoMoves = filter(lambda m: m.meta & 0b1100 == 0b1100, [MoveInfo.FromInt32(move) for move in moves]) assert len(promoMoves) > 0 print 'promo moves:' for move in promoMoves: testMakeUnmakeMove(zboard, move.int32)
def TestRandomMoves(): import random random.seed(0) for i in range(20): for j in range(len(allfens)): #x = random.randint(0,len(allfens) - 1) zboard = fen.ZBoardFromFen(allfens[j]) move = BDebug.RandomMoves(zboard)[i] testMakeUnmakeMove(zboard, move)
def TestEnPassantMoveGen(): zboard = fen.ZBoardFromFen(FEN_WhiteCanEPCapTwoWays) pdb.set_trace() moves = MoveGen.GetMoves(zboard) ep_moves = filter(lambda m: m.meta == EP_CAPTURE, [MoveInfo.FromInt32(move) for move in moves]) assert len(ep_moves) == 2 print 'ep moves:' for move in ep_moves: print move testMakeUnmakeMove(zboard, move.int32)
def DebugDeepSearch(d=3, debug=True): #todo: improve speed by improving move ordering function. #todo: verify that pruning works in the min function #todo: write is_checked #todo: modify MoveGen to only gen legal moves f = FEN_WhiteShouldMateWithPawn #f = FEN_blackShouldMoveKnight print f utils.FenToXBoard(f) zboard = fen.ZBoardFromFen(f) zboard.debug = debug res = AlphaBetaSearch(zboard, d) print res
def __init__(self, fen=FEN_starting): self.zboard = fen_utils.ZBoardFromFen(fen) #pdb.set_trace() self.isForced = False self.isAnalyzeMode = False self.features = [ ("setboard", '1'), ("analyze", '0'), ("usermove", '1'), ("reuse", '0'), ("draw", '1'), ("sigterm", '0'), ("colors", '0'), ("variants", 'normal'), ("myname", 'zeitchess'), ]
def TestRandomMoveSequence(): import random random.seed(0) print 'aoue' zboard = fen.ZBoardFromFen(FEN_starting) firstHash = BDebug.Hash(zboard) for run in range(2): for i in range(100): if run == 0: moves = BDebug.RandomMoves(zboard) if not moves: print "no moves left" break move = moves[random.randint(0, len(moves) - 1)] zboard.MakeMove(move) elif run == 1: zboard.UnmakeMove() lastHash = BDebug.Hash(zboard) print 'aoeu' assert firstHash == lastHash
def TestZBoardToFEN(): for _fen in allfens: zboard = fen.ZBoardFromFen(_fen) print 'orig: ' + _fen print 'redn: ' + fen.ZBoardToFen(zboard) + '\n'
def testInitialBoardMoveGen(): zboard = fen.ZBoardFromFen(FEN_starting) testMoveGenerationBreakdown(zboard)
def testMoveGen2(): zboard = fen.ZBoardFromFen(FEN_pos2) testMoveGenerationBreakdown(zboard)
def TestMakeUnmakeMove(): zboard = fen.ZBoardFromFen(FEN_pos2) #Nc6-b4 mInfo = MoveInfo.From8x8((2, 5), (1, 3), KNIGHT, EMPTY, QUIET_MOVE) testMakeUnmakeMove(zboard, mInfo.ToInt32())
def TestAlphaBetaSearch(d=5): zboard = fen.ZBoardFromFen(FEN_blackShouldMoveKnight) res = AlphaBetaSearch(zboard, d) print res if len(res.moveList) != d - 1: pdb.set_trace()
def run(self): while True: msg = '' try: msg = raw_input() except KeyboardInterrupt, k: out('#keyboard int') out(str(k)) continue if not msg.strip(): continue zlog.log(msg) tokens = msg.split() if tokens[0] == "protover": strFeatures = [ "%s=%s" % (feature[0], feature[1]) for feature in self.features ] s = "feature %s done=1" % " ".join(strFeatures) out(s) elif tokens[0] == 'new': self.engineColor = const.BLACK #self.zboard.SetColor(const.BLACK) #elif tokens[0] == "sd": # self.sd = int(tokens[1]) # self.skipPruneChance = max(0, (5-self.sd)*0.02) # if self.sd >= 5: # print "If the game has no timesettings, you probably don't want\n"+\ # "to set a search depth much greater than 4" #elif tokens[0] == "egtb": # # This is a crafty command interpreted a bit loose # self.useegtb = True # #elif tokens[0] == "level": # moves = int(tokens[1]) # self.increment = int(tokens[3]) # minutes = tokens[2].split(":") # self.mytime = int(minutes[0])*60 # if len(minutes) > 1: # self.mytime += int(minutes[1]) # print "Playing %d moves in %d seconds + %d increment" % \ # (moves, self.mytime, self.increment) # #elif tokens[0] == "time": # self.mytime = int(tokens[1])/100. # ##elif tokens[0] == "otim": ## self.optime = int(tokens[1]) elif tokens[0] == "quit": sys.exit() elif tokens[0] == "result": # We don't really care what the result is at the moment. sys.exit() elif tokens[0] == "force": if not self.forced and not self.analyzing: self.forced = True self.__stopSearching() elif tokens[0] == "go": self.playingAs = self.zboard.color self.forced = False #pdb.set_trace() self.search() elif tokens[0] == "undo": self.stopSearching() self.zboard.UnmakeMove() if self.analyzing: self.analyze() elif tokens[0] == "?": self.stopSearching() elif tokens[0] in ("black", "white"): newColor = tokens[0] == "black" and BLACK or WHITE if self.playingAs != newColor: self.stopSearching() self.engineColor = newColor self.zboard.setColor(newColor) self.zboard.setEnpassant(None) if self.analyzing: self.analyze() elif tokens[0] == "analyze": self.engineColor = self.zboard.color self.analyzing = True self.analyze() elif tokens[0] == "draw": if self.scr <= 0: out("offer draw") elif tokens[0] == "random": #leval.random = True pass elif tokens[0] == "setboard": self.stopSearching() self.zboard = fen_utils.ZBoardFromFen(" ".join(tokens[1:])) if self.analyzing: self.analyze() elif tokens[0] in ("xboard", "otim", "hard", "easy", "nopost", "post", "accepted", "rejected", 'level', 'time'): pass elif tokens[0] == "usermove" or len(tokens) == 1 and re.match( r'[a-h][1-8][a-h][1-8][pnbrqk]{0,1}', tokens[0]): if self.engineColor == self.zboard.color: pdb.set_trace() out('# received a move') if tokens[0] == "usermove": moveStr = tokens[1] else: moveStr = tokens[0] move = ParseMove(self.zboard, moveStr) if move < 0: out('#skipping') out("#Illegal move, " + moveStr) out("Illegal move, " + moveStr) continue out('#about to make move') self.zboard.MakeMove(move) out('#move made') self.engineColor = self.zboard.color if not self.isForced and not self.isAnalyzeMode: out('#searching') self.search() elif self.isAnalyzeMode: out('#analyzing') self.analyze() else: out('#why do nothing?') else: out("Warning (unknown command): " + msg)