def main(): board_size = 9 game = goboard_slow.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): time.sleep(0.3) print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 game = goboard_slow.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): #set timer for bot moves so you can actually see whats going on time.sleep(0.3) #before each move, clear the screen so we can always print to the same position on the command line. print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main_vs_bot(): board_size = 5 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: minimax.DepthPrunedAgent(1, capture_diff), gotypes.Player.white: minimax.DepthPrunedAgent(3, capture_diff), } while not game.is_over(): time.sleep(0.03) print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) print(game.winner())
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: naive.RandomBot(), } while not game.is_over(): time.sleep(0.3) tmp = sp.call('clear', shell=True) # method for clearing the command line prior to displaying board states utils.print_board(game.board) bot_move = bots[game.next_player].select_move(game) utils.print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bot = agent.RandomBot() while not game.is_over(): print(chr(27) + "[2J") print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def main(): board_size = 9 game = goboard_slow.GameState.new_game(board_size) bots = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: naive.RandomBot() } while not game.is_over(): time.sleep(0.3) # Sleep timer so we can observe bot moves print( chr(27) + "[2J" ) #Before each move, clear the screen, so the board is always printed to the same position on command line print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot() } while not game.is_over(): time.sleep(0.03) print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) # error: apply_move() missing 1 required positional argument: 'move' # game = game.apply_move(bot_move) game = game.apply_move(game.next_player, bot_move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bot = depthpruning.DepthPrunedAgent(5, evaluate_functions.capture_diff) while not game.is_over(): print(chr(27) + "[2J") print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: depthpruning.DepthPrunedAgent(2, evaluate_functions.capture_diff), gotypes.Player.white: depthpruning.DepthPrunedAgent(1, evaluate_functions.capture_diff), } while not game.is_over(): time.sleep(0.3) # <1> print(chr(27) + "[2J]") # <2> print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 game = goboard_slow.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): time.sleep(0.3) print( chr(27) + "[2J]" ) # clears screen and prints board at the same position on command line print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): # 観察できるようにスリープタイマーを設定する time.sleep(0.3) # <1> # 盤面が常に同じ位置に出力されるよう、画面をクリアする。 print(chr(27) + "[2J") # <2> print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def main(): board_size = 9 # we will play 9x9 for now game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot() } while not game.is_over(): time.sleep(0.1) # 300 milisecond delay, so we can observe the game print( chr(27) + "[2J" ) # clearing the screen after every move, so it doesn't spam the terminal print_board(game.board) bot_move = bots[game.next_player].select_move(game) game = game.apply_move(bot_move) print_move(game.next_player, bot_move)
def main(): game = goboard.GameState.new_game(BOARD_SIZE) max_depth = int(input('Depth search = ')) max_width = int(input('Width search = ')) step_change = int( input('Step where will be changed max_width and max_depth:')) agnt = my_predict.load_prediction_agent(h5py.File(path_model, 'r')) bot = minimax.AlphaBetaAgent(max_depth=max_depth, max_width=max_width, agnt=agnt, eval_fn=territory_diff) step = 0 while not game.is_over(): step += 1 print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ').upper() # Nail print('Step = ', step) point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: if step < step_change: bot = minimax.AlphaBetaAgent(max_depth=3, max_width=3, agnt=agnt, eval_fn=capture_diff) else: bot = minimax.AlphaBetaAgent(max_depth=max_depth, max_width=max_width, agnt=agnt, eval_fn=territory_diff) time_begin = time.time() move = bot.select_move(game, agnt) time_select = time.time() - time_begin print('Time selection move = ', time_select) print('Step = ', step, ' Depth = ', max_depth, ' Width = ', max_width) res, tb, tw = territory(game) print('Game current result = ', res) print_move(game.next_player, move) game = game.apply_move(move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): time.sleep(.01) # print("selecting a move...") bot_move = bots[game.next_player].select_move(game) game = game.apply_move(bot_move) # print("selected move:", end=" ") print("\n" * 80) #print(chr(27) + "[2J") print_board(game.board) print_move(game.next_player, bot_move)
def main(): game = goboard.GameState.new_game(BOARD_SIZE) bot = mcts.MCTSAgent(500, temperature=1.4) while not game.is_over(): print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ').upper().strip() if human_move == "PASS": move = goboard.Move.pass_turn() elif human_move == "RESIGN": move = goboard.Move.resign() else: point = point_from_coords(human_move) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def main(): start = time.process_time() start_perf = time.perf_counter_ns() board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } while not game.is_over(): time.sleep(0.3) print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) end = time.process_time() end_perf = time.perf_counter_ns() print("zbot: elapsed time is " + str(end - start)) print("zbot_perf: elapsed time is " + str(end_perf - start_perf))
def main(): board_size = 4 game = GameState.new_game(board_size) bot = MinimaxBot(5, capture_diff) while not game.is_over(): print(chr(27) + "[2J") print_board(game.board) if game.next_player == Player.black: valid = False while not valid: human_move = input('-- ') human_move = human_move.upper() point = point_from_coords(human_move.strip()) move = Move.play(point) valid = game.is_valid_move(move) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def generate_game(board_size, rounds, max_moves, temperature): boards, moves = [], [] encoder = get_encoder_by_name('oneplane', board_size) game = goboard.GameState.new_game(board_size) bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) move = bot.select_move(game) if move.is_play: boards.append(encoder.encode(game)) move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 moves.append(move_one_hot) print_move(game.next_player, move) game = game.apply_move(move) num_moves += 1 if num_moves > max_moves: break return np.array(boards), np.array(moves)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bot = naive.RandomBot() print_board(game.board) while not game.is_over(): # Since making a play is necessary for changing board state but also changes # next player we must save the current player player_before = game.next_player move = None if game.next_player == gotypes.Player.black: valid_move = False while not valid_move: try: human_move = input('-- ').upper() if match("PA(S)*", human_move): move = goboard.Move.pass_turn() elif match("RE(SIGN)*", human_move): move = goboard.Move.resign() else: point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) valid_move = game.is_valid_move(move) if not valid_move: print("Invalid move") except AssertionError: print("Invalid move") except ValueError: print("Invalid move") except IndexError: print("Invalid move") # end of human input loop else: move = bot.select_move(game) clear() game = game.apply_move(move) print_board(game.board) print_move(player_before, move)
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { # gotypes.Player.black: agent.RandomBot(), # gotypes.Player.white: agent.RandomBot(), gotypes.Player.black: minimax.MinimaxAgent(), gotypes.Player.white: minimax.MinimaxAgent(), } count = 0 while not game.is_over(): count += 1 time.sleep(0.3) print(chr(27) + "[2J") print_board(game.board) # <1> bot_move = bots[game.next_player].select_move(game) # bot_move = bots[ game.next_player ].select_move( game ) print_move(game.next_player, bot_move) game = game.apply_move(bot_move)
def generate_game(board_size, rounds, max_moves, temperature): # boardsにはエンコードされた盤の状態が格納され、movesにはエンコードされた着手が格納される boards, moves = [], [] # <1> # OnePlaneEncoderを指定された盤のサイズで初期化する encoder = get_encoder_by_name('oneplane', board_size) # <2> # サイズboard_sizeの新しいゲームがインスタンス化される game = goboard.GameState.new_game(board_size) # <3> # ラウンド数と温度が指定されたモンテカルロ木探索エージェントがボットになる bot = mcts.MCTSAgent(rounds, temperature) # <4> num_moves = 0 while not game.is_over(): print_board(game.board) # 次の着手がボットによって選択される move = bot.select_move(game) # <5> if move.is_play: # エンコードされた盤の状態がboardsに追加される boards.append(encoder.encode(game)) # <6> move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 # one-hotエンコードされた次の着手がmovesに追加される moves.append(move_one_hot) # <7> print_move(game.next_player, move) # その後、ボットの着手が盤に適用される game = game.apply_move(move) # <8> num_moves += 1 # 最大手数に達していない限り、次の手番を続ける if num_moves > max_moves: # <9> break return np.array(boards), np.array(moves) # <10>
def main(): board_size = 9 game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: agent.naive.RandomBot(), gotypes.Player.white: agent.naive.RandomBot(), } board_ext = Board_Ext(game.board) while not game.is_over(): time.sleep(3) # <1> print(chr(27) + "[2J") # <2> #print_board(game.board) print_board_ext(board_ext) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) p = bot_move.point board_ext.place_stone_ext(game.board, game.next_player, p) # for p in board_ext._grid_ext.keys(): # print('p= ', p, ':', board_ext._grid_ext[p]) game = game.apply_move(bot_move)
def main(): board_size = 9 komi = 3.5 game = goboard_slow.GameState.new_game(board_size) bots = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: naive.RandomBot(), } while not game.is_over(): time.sleep(.3) clear() print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) results = compute_game_result(game, komi) print("With a komi of %.1f" % komi) print("White Final Score: %d" % results.w) print("Black Final Score: %.1f" % (results.b - results.komi)) print(results.winner, "wins by %.1f points" % results.winning_margin)
def main(): while True: print("The board size(5-19)") board_size = int(input()) if 19 >= board_size >= 5: break else: print("Wrong size,please input 5-19") game = goboard.GameState.new_game(board_size) bot = naive.RandomBot() while not game.is_over(): print(chr(27) + "[2J") print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move) print(scoring.compute_game_result(game))
def main(): board_size = 0 while not 5 <= board_size <= 19: board_size = int(input("Enter Board Size (5X5 - 19X19) : ")) game = goboard.GameState.new_game(board_size) bot = agent.RandomBot() while not game.is_over(): #print(chr(27) + "[2J") print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') if human_move == 'pass': move = goboard.Move.pass_turn() elif human_move == 'resign': move = goboard.Move.resign() else: point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move)
def main(): board_size = 5 pygame.init() pygame.display.set_caption('Goban') game = GameState.new_game(board_size) bots = { gotypes.Player.black: RandomBot(), gotypes.Player.white: AlphaBetaAgent(2, capture_diff), } while not game.is_over(): #time.sleep(0.3) print(chr(27) + "[2J") print_board(game.board) GuiBoard.draw(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(game.next_player, bot_move) print("winner is:", game.winner()) print("score is is:", compute_game_result(game)) input("Press Enter to continue...")
def generate_game(board_size, rounds, max_moves, temperature): # In `boards` we store encoded board state, `moves` is for encoded moves. boards, moves = [], [] # We initialize a OnePlaneEncoder by name with given board size. encoder = get_encoder_by_name('oneplane', board_size) # An new game of size `board_size` is instantiated. game = goboard.GameState.new_game(board_size) # A Monte Carlo tree search agent with specified number of rounds and temperature will serve as our bot. bot = mcts.MCTSAgent(rounds, temperature) num_moves = 0 while not game.is_over(): print_board(game.board) # The next move is selected by the bot. move = bot.select_move(game) if move.is_play: boards.append(encoder.encode( game)) # The encoded board situation is appended to `boards`. move_one_hot = np.zeros(encoder.num_points()) move_one_hot[encoder.encode_point(move.point)] = 1 moves.append( move_one_hot ) # The one-hot-encoded next move is appended to `moves`. print_move(game.next_player, move) game = game.apply_move( move) # Afterwards the bot move is applied to the board. num_moves += 1 if num_moves > max_moves: # continue with the next move, unless the maximum number of moves has been reached break return np.array(boards), np.array(moves)
def main(): while True: print("The board size(5-19)") board_size = int(input()) if 19 >= board_size >= 5: break else: print("Wrong size,please input 5-19") game = goboard.GameState.new_game(board_size) bots = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: naive.RandomBot(), } start = timeit.default_timer() while not game.is_over(): time.sleep(0.3) print(chr(27) + "[2J") print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) stop = timeit.default_timer() print(scoring.compute_game_result(game)) print('Runtime:', stop - start, 'seconds')
def main(): print("******************************************************************") print("* *") print("* <3 <3 <3 <3 <3 WELCOME TO GAME GO <3 <3 <3 <3 <3 *") print("* *") print("******************************************************************") print("* *") print("* 1. Play game on terminal *") print("* *") print("* a. Human vs Bot AlphaBeta on Board 9x9 *") print("* b. Human vs Bot Depthprune on Board 9x9 *") print("* c. Human vs Bot MCTS on Board 9x9 *") print("* d. Bot AlphaBeta vs Bot MCTS on Board 9x9 *") print("* *") print("* 2. Play game on web *") print("* *") print("* a. Human vs Bot MCTS on Board 9x9 *") print("* b. Human vs Bot DeepLearning on Board 19x19 *") print("* *") print("******************************************************************") print(" ") print(" ***************************************** ") print(" ") choices_A = int(input(" Choose Terminal or Web: ")) choices_B = input(" Choose type bot: ") print(" ") print(" ***************************************** ") BOARD_SIZE = 9 game = goboard.GameState.new_game(BOARD_SIZE) if choices_A == 1: if choices_B == 'a': bot = minimax.AlphaBetaAgent(4, capture_diff) if choices_B == 'b': bot = minimax.DepthPrunedAgent(4, capture_diff) if choices_B == 'c': bot = mcts.MCTSAgent(500, temperature=1.4) if choices_B == 'd': bots = { gotypes.Player.black: minimax.AlphaBetaAgent(4, capture_diff), gotypes.Player.white: mcts.MCTSAgent(500, temperature=1.4), } while not game.is_over(): time.sleep(0.3) print_board(game.board) bot_move = bots[game.next_player].select_move(game) print_move(game.next_player, bot_move) game = game.apply_move(bot_move) if choices_B == 'a' or choices_B == 'b' or choices_B == 'c': while not game.is_over(): print_board(game.board) if game.next_player == gotypes.Player.black: human_move = input('-- ') point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) else: move = bot.select_move(game) print_move(game.next_player, move) game = game.apply_move(move) else: if choices_B == 'a': bot = mcts.MCTSAgent(700, temperature=1.4) web_app = get_web_app({'mcts': bot}) web_app.run()
def main(): options = get_options(argv[1:]) size = abs(options.size) human = options.human komi = abs(options.komi) if size < 5 or size > 19: print("Board size must be between 5 and 19") exit(-1) if komi < 0 or komi > 10: print("Komi must be between 0 and 10") exit(-1) game = goboard.GameState.new_game(size) players = {} if human is None: players = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: naive.RandomBot() } elif human == 'b': players = { gotypes.Player.black: human, gotypes.Player.white: naive.RandomBot() } elif human == 'w': players = { gotypes.Player.black: naive.RandomBot(), gotypes.Player.white: human } else: print(options) print("Invalid options error") exit(0) print_board(game.board) while not game.is_over(): # Since making a play is necessary for changing board state but also changes # next player we must save the current player player_before = game.next_player move = None if players[game.next_player] == human: valid_move = False while not valid_move: try: human_move = input('-- ').upper() if match("P(ASS)*$", human_move): move = goboard.Move.pass_turn() elif match("R(ESIGN)*$", human_move): move = goboard.Move.resign() else: point = point_from_coords(human_move.strip()) move = goboard.Move.play(point) valid_move = game.is_valid_move(move) if not valid_move: print("Invalid move") except AssertionError: print("Invalid move") except ValueError: print("Invalid move") except IndexError: print("Invalid move") # end of human input loop else: move = players[game.next_player].select_move(game) clear() game = game.apply_move(move) print_board(game.board) time.sleep(.1) print_move(player_before, move) # end of main game loop game.print_game_results(komi)