def search(self): out('#searching...') """ Finds and prints the best move from the current position """ pdb.set_trace() maxRes = search.AlphaBetaSearch(self.zboard, 4) move = maxRes.moveList[0] out('# ' + str(maxRes)) self.zboard.MakeMove(move) ZeitchessEngine.printMove(move) zlog.log(self.zboard.ToString(True))
def search (self): out('#searching...') """ Finds and prints the best move from the current position """ pdb.set_trace() maxRes = search.AlphaBetaSearch(self.zboard, 4) move = maxRes.moveList[0] out('# ' + str(maxRes)) self.zboard.MakeMove(move) ZeitchessEngine.printMove(move) zlog.log(self.zboard.ToString(True))
def isTerminalState(zboard, curDepth, cutoffDepth): global nodeCount global lastTime global failHighCount global failLowCount global branchings if datetime.now() - lastTime > timedelta(seconds=1): diff = datetime.now() - lastTime t = float(diff.microseconds) / 1000000.0 + diff.seconds rate = nodeCount / t zlog.log("%s nodes per second" % str(rate)) # zlog.log('ave branching = %s' % str(sum(branchings) / (len(branchings) + 1))) # branchings = [] lastTime = datetime.now() nodeCount = 0 else: nodeCount += 1 if curDepth > cutoffDepth: return True if zboard.kings[0] == 0 or zboard.kings[1] == 0: return True return False
def isTerminalState(zboard, curDepth, cutoffDepth): global nodeCount global lastTime global failHighCount global failLowCount global branchings if datetime.now() - lastTime > timedelta(seconds=1): diff = datetime.now() - lastTime t = float(diff.microseconds) / 1000000.0 + diff.seconds rate = nodeCount / t zlog.log('%s nodes per second' % str(rate)) #zlog.log('ave branching = %s' % str(sum(branchings) / (len(branchings) + 1))) #branchings = [] lastTime = datetime.now() nodeCount = 0 else: nodeCount += 1 if curDepth > cutoffDepth: return True if zboard.kings[0] == 0 or zboard.kings[1] == 0: return True return False
def TestZLog(): zlog.log('aaaa ' * 100) zlog.log('bbbb ' * 100) zlog.log('1234 ' * 100) zlog.log('9876 ' * 100) zlog.Flush()
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)
def out(msg, dbg=False): print msg zlog.log(msg) sys.stdout.flush()
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 )