def genmove_plain(self, color, remove_opponent_dead=False, pass_allowed=True): if config.always_remove_opponent_dead: remove_opponent_dead = True self.check_side2move(color) if config.use_opening_library and not self.engine.opening_tree: opening_file = cgos_opening_tree.find_opening_from_web( string.upper(color[0])) log("Using opening file: %s\n" % opening_file) #self.engine.opening_tree0 = self.opening_tree[string.upper(color[0]) + "0"] #self.engine.opening_tree0.game = self.engine #self.engine.opening_tree = self.opening_tree[string.upper(color[0])] self.engine.opening_tree = cgos_opening_tree.OpeningTree( opening_file, self.engine) if self.engine.opening_tree.hash_tree: log("Opening file OK\n") #self.engine.opening_tree.game = self.engine #old code #move = self.engine.generate_move(remove_opponent_dead, pass_allowed) ''' move = () move = RESIGN_MOVE move = string.lower(simple_go.move_as_string(move, self.engine.size)) return move ''' #print self.engine.move_history if self.engine.is_end(by_score=True) == True: print 'resign:' move = () move = RESIGN_MOVE move = string.lower( simple_go.move_as_string(move, self.engine.size)) return move move = () if self.randomcount > len(self.engine.move_history): #print 'action by randomcount:' move = self.engine.generate_move(remove_opponent_dead, pass_allowed) else: move = self.m_jobmgr.genmove(self.engine) # movescore = self.engine.score_move(move) #print self.engine.move_history #print 'move : ' + str(move[0]) + ',' + str(move[1]) + ' movescore : ' + str(movescore) self.move_generated = True #print 'genmove 100 : ' + str(move[0]) + ',' + str(move[1]) move = string.lower(simple_go.move_as_string(move, self.engine.size)) #print 'genmove 101 : ' + str(move[0]) + ',' + str(move[1]) self.play_plain(color, move) ##print 'genmove 102' return move
def genmove_plain(self, color, remove_opponent_dead=False, pass_allowed=True): if config.always_remove_opponent_dead: remove_opponent_dead = True self.check_side2move(color) if config.use_opening_library and not self.engine.opening_tree: opening_file = cgos_opening_tree.find_opening_from_web(string.upper(color[0])) log("Using opening file: %s\n" % opening_file) #self.engine.opening_tree0 = self.opening_tree[string.upper(color[0]) + "0"] #self.engine.opening_tree0.game = self.engine #self.engine.opening_tree = self.opening_tree[string.upper(color[0])] self.engine.opening_tree = cgos_opening_tree.OpeningTree(opening_file, self.engine) if self.engine.opening_tree.hash_tree: log("Opening file OK\n") #self.engine.opening_tree.game = self.engine #old code #move = self.engine.generate_move(remove_opponent_dead, pass_allowed) ''' move = () move = RESIGN_MOVE move = string.lower(simple_go.move_as_string(move, self.engine.size)) return move ''' #print self.engine.move_history if self.engine.is_end(by_score=True) == True: print 'resign:' move = () move = RESIGN_MOVE move = string.lower(simple_go.move_as_string(move, self.engine.size)) return move move = () if self.randomcount > len(self.engine.move_history): #print 'action by randomcount:' move = self.engine.generate_move(remove_opponent_dead, pass_allowed) else: move = self.m_jobmgr.genmove(self.engine) # movescore = self.engine.score_move(move) #print self.engine.move_history #print 'move : ' + str(move[0]) + ',' + str(move[1]) + ' movescore : ' + str(movescore) self.move_generated = True #print 'genmove 100 : ' + str(move[0]) + ',' + str(move[1]) move = string.lower(simple_go.move_as_string(move, self.engine.size)) #print 'genmove 101 : ' + str(move[0]) + ',' + str(move[1]) self.play_plain(color, move) ##print 'genmove 102' return move
def place_free_handicap(self, count): self.set_handicap(count) result = [] for move in self.engine.place_free_handicap(count): move = simple_go.move_as_string(move, self.engine.size) result.append(move) return self.ok(string.join(result))
def genmove_plain(self, color, pass_allowed=1): t0 = time.time() move = self.engine.exec_cmd("reg_genmove " + color) if move[0]=="=": move = move[2:] while move and move[-1]=="\n": move = move[:-1] if string.upper(move[:4])=="PASS" or move[:2]=="??": move = self.slave.exec_cmd("reg_genmove " + color) if move[0]=="=": move = move[2:] while move and move[-1]=="\n": move = move[:-1] log("overruled pass with: " + move + "\n") result = self.play_plain(color, move) if result[0]=="?": move = self.slave.exec_cmd("reg_genmove " + color) if move[0]=="=": move = move[2:] while move and move[-1]=="\n": move = move[:-1] log("overruled illegal move with gnugo3.6: " + move + "\n") result = self.play_plain(color, move) if result[0]=="?": move = self.simple_engine.generate_move() move = simple_go.move_as_string(move, self.size) log("overruled illegal move with simplego: " + move + "\n") self.play_plain(color, move) time_used = time.time() - t0 time_per_move = self.time / 30.0 time_not_yet_used = time_per_move - time_used self.log_fp.write("Time left: %.2f\n" % self.time) self.log_fp.write("Time used: %.2f\n" % time_used) self.log_fp.write("Time/move: %.2f\n" % time_per_move) self.log_fp.write("Unused time: %.2f\n" % time_not_yet_used) self.log_fp.flush() if self.time_delay and time_not_yet_used > 0.0: self.log_fp.write("Sleeping: %.2f\n" % time_not_yet_used) self.log_fp.flush() time.sleep(time_not_yet_used) return move
def genmove_plain(self, color, pass_allowed=1): move = self.engine.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] if string.upper(move[:4]) == "PASS" or move[:2] == "??": move = self.slave.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] log("overruled pass with: " + move + "\n") result = self.play_plain(color, move) if result[0] == "?": move = self.slave.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] log("overruled illegal move with gnugo3.6: " + move + "\n") result = self.play_plain(color, move) if result[0] == "?": move = self.simple_engine.generate_move() move = simple_go.move_as_string(move, self.size) log("overruled illegal move with simplego: " + move + "\n") self.play_plain(color, move) return move
def place_free_handicap(self, count): self.handicap = count result = [] for move in self.engine.place_free_handicap(count): move = simple_go.move_as_string(move, self.engine.size) result.append(move) return self.ok(string.join(result))
def final_status_list(self, status): self.save_sgf("status") self.clock.reset() lst = self.engine.final_status_list(status) str_lst = [] for pos in lst: str_lst.append(simple_go.move_as_string(pos, self.engine.size)) return self.ok(string.join(str_lst, "\n"))
def final_status_list(self, status): fp = open("status%03i.sgf" % self.sgf_number, "w") fp.write(str(self.engine)) fp.close() lst = self.engine.final_status_list(status) str_lst = [] for pos in lst: str_lst.append(simple_go.move_as_string(pos, self.engine.size)) return self.ok(string.join(str_lst, "\n"))
def genmove_plain(self, color, remove_opponent_dead=True, pass_allowed=True): self.check_side2move(color) move = self.engine.select_random_no_eye_fill_move( remove_opponent_dead, pass_allowed) move = simple_go.move_as_string(move, self.engine.size) self.play_plain(color, move) return move
def genmove_plain(self, color, remove_opponent_dead=False, pass_allowed=True): if config.always_remove_opponent_dead: remove_opponent_dead = True self.check_side2move(color) move = self.engine.generate_move(remove_opponent_dead, pass_allowed) move = simple_go.move_as_string(move, self.engine.size) self.play_plain(color, move) return move
def test_time_usage(file_name, color): g = load_file(file_name) moves = g.move_history[:] while g.undo_move(): pass t0 = time.time() for move in moves: move_no = len(g.move_history) + 1 if g.current_board.side==simple_go.BLACK: print "Black to move" else: print "White to move" print "Current move:", move_no, simple_go.move_as_string(move) if g.current_board.side==color: print "Generated move:", move_no, simple_go.move_as_string(g.generate_move()) g.make_move(move) print print "="*60 print t1 = time.time() print "Total time usage:", t1-t0
def test_time_usage(file_name, color): g = load_file(file_name) moves = g.move_history[:] while g.undo_move(): pass t0 = time.time() for move in moves: move_no = len(g.move_history) + 1 if g.current_board.side == simple_go.BLACK: print "Black to move" else: print "White to move" print "Current move:", move_no, simple_go.move_as_string(move) if g.current_board.side == color: print "Generated move:", move_no, simple_go.move_as_string( g.generate_move()) g.make_move(move) print print "=" * 60 print t1 = time.time() print "Total time usage:", t1 - t0
def genmove_plain(self, color, pass_allowed=1): t0 = time.time() move = self.engine.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] if string.upper(move[:4]) == "PASS" or move[:2] == "??": move = self.slave.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] log("overruled pass with: " + move + "\n") result = self.play_plain(color, move) if result[0] == "?": move = self.slave.exec_cmd("reg_genmove " + color) if move[0] == "=": move = move[2:] while move and move[-1] == "\n": move = move[:-1] log("overruled illegal move with gnugo3.6: " + move + "\n") result = self.play_plain(color, move) if result[0] == "?": move = self.simple_engine.generate_move() move = simple_go.move_as_string(move, self.size) log("overruled illegal move with simplego: " + move + "\n") self.play_plain(color, move) time_used = time.time() - t0 time_per_move = self.time / 30.0 time_not_yet_used = time_per_move - time_used self.log_fp.write("Time left: %.2f\n" % self.time) self.log_fp.write("Time used: %.2f\n" % time_used) self.log_fp.write("Time/move: %.2f\n" % time_per_move) self.log_fp.write("Unused time: %.2f\n" % time_not_yet_used) self.log_fp.flush() if self.time_delay and time_not_yet_used > 0.0: self.log_fp.write("Sleeping: %.2f\n" % time_not_yet_used) self.log_fp.flush() time.sleep(time_not_yet_used) return move
def final_status_list(self, status): lst = self.engine.final_status_list(status) str_lst = [] for pos in lst: str_lst.append(simple_go.move_as_string(pos, self.engine.size)) return self.ok(string.join(str_lst, "\n"))
def play(self, seed=1, print_flag = False): if self.size in (5, 7): random.seed(seed) self.random_engine.current_board.init_hash() if self.size==5 and int(random.random() * 26)!=13: return START_HEURISTICS if self.size==7: if int(random.random() * 50)!=25: return START_HEURISTICS if int(random.random() * 48) not in (17, 18, 23, 24, 29, 30, 31): return START_HEURISTICS random.seed(seed) self.boardsize(self.size) score = "" while not self.random_engine.has_2_passes(): if self.random_engine.current_board.side==simple_go.BLACK: move_tuple = self.random_engine.select_random_move() self.random_engine.make_move(move_tuple) move = simple_go.move_as_string(move_tuple) edge_flag = False if self.size==5: if len(self.random_engine.move_history) <= 1: for coord in move_tuple: if coord in (2, self.size-1): edge_flag = True if len(self.random_engine.move_history) <= 3: if self.random_engine.current_board.pos_near_edge(move_tuple): edge_flag = True if len(self.random_engine.move_history) <= 7: if move_tuple==simple_go.PASS_MOVE: edge_flag = True else: # self.size >= 9: if len(self.random_engine.move_history) <= 3: for coord in move_tuple: if coord in (2, self.size-1): edge_flag = True if len(self.random_engine.move_history) <= 7: if self.random_engine.current_board.pos_near_edge(move_tuple): edge_flag = True if len(self.random_engine.move_history) <= 15: if move_tuple==simple_go.PASS_MOVE: edge_flag = True if edge_flag and not print_flag: return EDGE_HEURISTICS result = self.engine.exec_cmd("play black " + move) if result[0]=="?": raise ValueError, "engine didn't accept move" self.estimate_engine.exec_cmd("play black " + move) else: move = self.engine.exec_cmd("genmove white") move = move.split()[1] move_tuple = simple_go.string_as_move(move) if not self.random_engine.make_move(move_tuple): return SUPER_KO self.estimate_engine.exec_cmd("play white " + move) #if len(self.random_engine.move_history)%8==0: score = self.estimate_engine.exec_cmd("estimate_score") if print_flag: print score if print_flag: print move, len(self.random_engine.move_history) print self.random_engine.current_board else: score_lst = score.split() #if score[:8]=="= W+81.0": if self.size==5: #if score: # print score, score_lst[1][0], float(score_lst[4][:-1]) if score and (score_lst[1][0]=="W" or abs(float(score_lst[4][:-1]))!=25): return ESTIMATED_WIN elif self.size==7: if score and score_lst[1][0]=="W" and float(score_lst[1][1:]) >= 10.0: return ESTIMATED_WIN else: if score and score_lst[1][0]=="W" and float(score_lst[1][1:]) >= 20.0: return ESTIMATED_WIN elif score[:3]=="= B" and len(self.random_engine.move_history) >= 20: return BLACK_POSITIVE if print_flag: print self.random_engine print simple_go.move_list_as_string(self.random_engine.move_history) return GAME_FINISHED
def genmove_plain(self, color, pass_allowed=1): while 1: move = simple_go.move_as_string(self.engine.generate_move(), self.engine.size) if pass_allowed or move!="PASS": break self.play_plain(color, move) return move
def test_string_move_functions(self): ''' test string/move conversion functions ''' test = {(1, 1): "A1", (2, 3): "B3", (5, 5): "E5"} for pos, string in test.items(): self.assertEqual(simple_go.move_as_string(pos, self.size), string) self.assertEqual(simple_go.string_as_move(string, self.size), pos)
def genmove_plain(self, color, remove_opponent_dead=False, pass_allowed=True): self.check_side2move(color) move = self.engine.generate_move(remove_opponent_dead, pass_allowed) move = simple_go.move_as_string(move, self.engine.size) self.play_plain(color, move) return move
def genmove_plain(self, color, remove_opponent_dead=True, pass_allowed=True): self.check_side2move(color) move = self.engine.select_random_no_eye_fill_move(remove_opponent_dead, pass_allowed) move = simple_go.move_as_string(move, self.engine.size) self.play_plain(color, move) return move
def play(self, seed=1, print_flag=False): if self.size in (5, 7): random.seed(seed) self.random_engine.current_board.init_hash() if self.size == 5 and int(random.random() * 26) != 13: return START_HEURISTICS if self.size == 7: if int(random.random() * 50) != 25: return START_HEURISTICS if int(random.random() * 48) not in (17, 18, 23, 24, 29, 30, 31): return START_HEURISTICS random.seed(seed) self.boardsize(self.size) score = "" while not self.random_engine.has_2_passes(): if self.random_engine.current_board.side == simple_go.BLACK: move_tuple = self.random_engine.select_random_move() self.random_engine.make_move(move_tuple) move = simple_go.move_as_string(move_tuple) edge_flag = False if self.size == 5: if len(self.random_engine.move_history) <= 1: for coord in move_tuple: if coord in (2, self.size - 1): edge_flag = True if len(self.random_engine.move_history) <= 3: if self.random_engine.current_board.pos_near_edge( move_tuple): edge_flag = True if len(self.random_engine.move_history) <= 7: if move_tuple == simple_go.PASS_MOVE: edge_flag = True else: # self.size >= 9: if len(self.random_engine.move_history) <= 3: for coord in move_tuple: if coord in (2, self.size - 1): edge_flag = True if len(self.random_engine.move_history) <= 7: if self.random_engine.current_board.pos_near_edge( move_tuple): edge_flag = True if len(self.random_engine.move_history) <= 15: if move_tuple == simple_go.PASS_MOVE: edge_flag = True if edge_flag and not print_flag: return EDGE_HEURISTICS result = self.engine.exec_cmd("play black " + move) if result[0] == "?": raise ValueError, "engine didn't accept move" self.estimate_engine.exec_cmd("play black " + move) else: move = self.engine.exec_cmd("genmove white") move = move.split()[1] move_tuple = simple_go.string_as_move(move) if not self.random_engine.make_move(move_tuple): return SUPER_KO self.estimate_engine.exec_cmd("play white " + move) #if len(self.random_engine.move_history)%8==0: score = self.estimate_engine.exec_cmd("estimate_score") if print_flag: print score if print_flag: print move, len(self.random_engine.move_history) print self.random_engine.current_board else: score_lst = score.split() #if score[:8]=="= W+81.0": if self.size == 5: #if score: # print score, score_lst[1][0], float(score_lst[4][:-1]) if score and (score_lst[1][0] == "W" or abs(float(score_lst[4][:-1])) != 25): return ESTIMATED_WIN elif self.size == 7: if score and score_lst[1][0] == "W" and float( score_lst[1][1:]) >= 10.0: return ESTIMATED_WIN else: if score and score_lst[1][0] == "W" and float( score_lst[1][1:]) >= 20.0: return ESTIMATED_WIN elif score[:3] == "= B" and len( self.random_engine.move_history) >= 20: return BLACK_POSITIVE if print_flag: print self.random_engine print simple_go.move_list_as_string( self.random_engine.move_history) return GAME_FINISHED