📄 twogtp.py
字号:
to_play = self.first_to_play self.moves = [] passes = 0 won_by_resignation = "" while passes < 2: if to_play == "B": move = self.blackplayer.genmove("black") if move[:5] == "ERROR": # FIXME: write_sgf sys.exit(1) if move[:6] == "resign": if verbose >= 1: print "Black resigns" won_by_resignation = "W+Resign" break else: self.moves.append(move) if string.lower(move[:4]) == "pass": passes = passes + 1 if verbose >= 1: print "White passes" else: passes = 0 self.whiteplayer.black(move) if verbose >= 1: print "Black plays " + move to_play = "W" else: move = self.whiteplayer.genmove("white") if move[:5] == "ERROR": # FIXME: write_sgf sys.exit(1) if move[:6] == "resign": if verbose >= 1: print "White resigns" won_by_resignation = "B+Resign" break else: self.moves.append(move) if string.lower(move[:4]) == "pass": passes = passes + 1 if verbose >= 1: print "Black passes" else: passes = 0 self.blackplayer.white(move) if verbose >= 1: print "White plays " + move to_play = "B" if verbose >= 2: print self.whiteplayer.showboard() + "\n" if won_by_resignation == "": self.resultw = self.whiteplayer.final_score() self.resultb = self.blackplayer.final_score() else: self.resultw = won_by_resignation; self.resultb = won_by_resignation; # if self.resultb == self.resultw: # print "Result: ", self.resultw # else: # print "Result according to W: ", self.resultw # print "Result according to B: ", self.resultb # FIXME: $self->writesgf($sgffile) if defined $sgffile; if sgffile != "": self.writesgf(sgffile) def result(self): return (self.resultw, self.resultb) def cputime(self): cputime = {} cputime["white"] = self.whiteplayer.cputime() cputime["black"] = self.blackplayer.cputime() return cputime def quit(self): self.blackplayer.quit() self.whiteplayer.quit()class GTP_match: # Class members: # black # white # size # komi # handicap # handicap_type def __init__(self, whitecommand, blackcommand, size, komi, handicap, handicap_type, streak_length, endgamefilelist): self.white = whitecommand self.black = blackcommand self.size = size self.komi = komi self.handicap = handicap self.handicap_type = handicap_type self.streak_length = streak_length self.endgamefilelist = endgamefilelist def endgame_contest(self, sgfbase): results = [] i = 1 for endgamefile in self.endgamefilelist: game1 = GTP_game(self.white, self.black, self.size, self.komi, 0, "", endgamefile) game2 = GTP_game(self.black, self.white, self.size, self.komi, 0, "", endgamefile) if verbose: print "Replaying", endgamefile print "Black:", self.black print "White:", self.white game1.play("") result1 = game1.result()[0] if result1 != "0": plain_result1 = re.search(r"([BW]\+)([0-9]*\.[0-9]*)", result1) result1_float = float(plain_result1.group(2)) else: plain_result1 = re.search(r"(0)", "0") result1_float = 0.0 if result1[0] == "B": result1_float *= -1 if verbose: print "Result:", result1 print "Replaying", endgamefile print "Black:", self.white print "White:", self.black game2.play("") result2 = game2.result()[1] if verbose: print "Result:", result2 if result2 != "0": plain_result2 = re.search(r"([BW]\+)([0-9]*\.[0-9]*)", result2) result2_float = float(plain_result2.group(2)) else: plain_result2 = re.search(r"(0)", "0") result2_float = 0.0 if result2[0] == "B": result2_float *= -1 results.append(result1_float - result2_float) if (result1 != result2): print endgamefile+ ":", plain_result1.group(), \ plain_result2.group(), "Difference:", print result1_float - result2_float else: print endgamefile+": Same result:", plain_result1.group() sgffilename = "%s%03d" % (sgfbase, i) game1.writesgf(sgffilename + "_1.sgf") game2.writesgf(sgffilename + "_2.sgf") game1.quit() game2.quit() i += 1 return results def play(self, games, sgfbase): last_color = "" last_streak = 0 game = GTP_game(self.white, self.black, self.size, self.komi, self.handicap, self.handicap_type, "") results = [] for i in range(games): sgffilename = "%s%03d.sgf" % (sgfbase, i + 1) game.play(sgffilename) result = game.result() if result[0] == result[1]: print "Game %d: %s" % (i + 1, result[0]) else: print "Game %d: %s %s" % (i + 1, result[0], result[1]) if result[0][0] == last_color: last_streak += 1 elif result[0][0] != "0": last_color = result[0][0] last_streak = 1 if last_streak == self.streak_length: if last_color == "W": self.handicap += 1 if self.handicap == 1: self.handicap = 2 print "White wins too often. Increasing handicap to %d" \ % (self.handicap) else: if self.handicap > 0: self.handicap -= 1 if self.handicap == 1: self.handicap = 0 print "Black wins too often. Decreasing handicap to %d" \ % (self.handicap) else: self.handicap = 2 game.swap_players() print "Black looks stronger than white. Swapping colors and setting handicap to 2" game.set_handicap(self.handicap) last_color = "" last_streak = 0 results.append(result) cputime = game.cputime() game.quit() return results, cputime# ================================================================# Main program## Default values#white = ""black = ""komi = ""size = "19"handicap = 0handicap_type = "fixed"streak_length = -1endgame_start_at = 0games = 1sgfbase = "twogtp"verbose = 0helpstring = """Run with:twogtp --white \'<path to program 1> --mode gtp [program options]\' \\ --black \'<path to program 2> --mode gtp [program options]\' \\ [twogtp options]Possible twogtp options: --verbose 1 (to list moves) or --verbose 2 (to draw board) --komi <amount> --handicap <amount> --free-handicap <amount> --adjust-handicap <length> (change handicap by 1 after <length> wins in a row) --size <board size> (default 19) --games <number of games to play> (default 1) --sgfbase <filename> (create sgf files with sgfbase as basename) --endgame <moves before end> (endgame contest - add filenames of games to be replayed after last option)"""def usage(): print helpstring sys.exit(1)try: (opts, params) = getopt(sys.argv[1:], "", ["black=", "white=", "verbose=", "komi=", "boardsize=", "size=", "handicap=", "free-handicap=", "adjust-handicap=", "games=", "sgfbase=", "endgame=", ])except: usage();for opt, value in opts: if opt == "--black": black = value elif opt == "--white": white = value elif opt == "--verbose": verbose = int(value) elif opt == "--komi": komi = value elif opt == "--boardsize" or opt == "--size": size = value elif opt == "--handicap": handicap = int(value) handicap_type = "fixed" elif opt == "--free-handicap": handicap = int(value) handicap_type = "free" elif opt == "--adjust-handicap": streak_length = int(value) elif opt == "--games": games = int(value) elif opt == "--sgfbase": sgfbase = value elif opt == "--endgame": endgame_start_at = int(value)if endgame_start_at != 0: endgame_filelist = paramselse: endgame_filelist = [] if params != []: usage() if black == "" or white == "": usage()if komi == "": if handicap > 1 and streak_length == -1: komi = "0.5" else: komi = "7.5"match = GTP_match(white, black, size, komi, handicap, handicap_type, streak_length, endgame_filelist)if endgame_filelist != []: results = match.endgame_contest(sgfbase) win_black = 0 win_white = 0 for res in results: print res if res > 0.0: win_white += 1 elif res < 0.0: win_black += 1 print "%d games, %d wins for black, %d wins for white." \ % (len(results), win_black, win_white)else: results, cputimes = match.play(games, sgfbase) i = 0 for resw, resb in results: i = i + 1 if resw == resb: print "Game %d: %s" % (i, resw) else: print "Game %d: %s %s" % (i, resb, resw) if (cputimes["white"] != "0"): print "White: %ss CPU time" % cputimes["white"] if (cputimes["black"] != "0"): print "Black: %ss CPU time" % cputimes["black"]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -