Example #1
0
 def select_move(bot_name):
     content = request.json
     board_size = content['board_size']
     game_state = goboard.GameState.new_game(board_size)
     # replay the game up to this point.
     for move in content['moves']:
         if move == 'pass':
             next_move = goboard.Move.pass_turn()
         elif move == 'resign':
             next_move = goboard.Move.resign()
         else:
             next_move = goboard.Move.play(point_from_coords(move))
         game_state = game_state.apply_move(next_move)
     bot_agent = bot_map[bot_name]
     bot_move = bot_agent.select_move(game_state)
     if bot_move.is_pass:
         bot_move_str = 'pass'
     elif bot_move.is_resign:
         bot_move_str = 'resign'
     else:
         bot_move_str = coords_from_point(bot_move.point)
     return jsonify({
         'bot_move': bot_move_str,
         'diagnostics': bot_agent.diagnostics()
     })
Example #2
0
def main():
    while True:
        board_size = 19
        try:
            board_size = int(board_size)
        except ValueError:
            print('Invalid Entry')
            continue
        if board_size in range(5, 20):
            break
        else:
            print('Invalid Entry')
    game = goboard.GameState.new_game(board_size)
    bot = bot_1_from_file

    while not game.is_over():
        # print(chr(27) + "[2J")        # We decided not to clear the screen, so we can see the history of the board
        compute_score(game.board)
        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)
Example #3
0
def main():
    while True:
        board_size = input(
            'Enter the dimensions of the square Go board. (Integer between 5 and 19):'
        )
        try:
            board_size = int(board_size)
        except ValueError:
            print('Invalid Entry')
            continue
        if board_size in range(5, 20):
            break
        else:
            print('Invalid Entry')
    game = goboard.GameState.new_game(board_size)
    bot = agent.RandomBot()

    while not game.is_over():
        print(chr(27) + "[2J")
        compute_score(game.board)
        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)
Example #4
0
def main():

    # Define board size
    board_size = 9
    # Start a new game and store it in the game variable
    game = goboard.GameState.new_game(board_size)
    # Declare the players from the naive bot agent
    bot = RandomBot()

    # Game loop
    while not game.is_over():

        # Before each move, we clear the screen. This way the board is always
        # printed to the same position on the line command
        print(chr(27) + "[2J")
        # Print the board
        print_board(game.board)

        # Tell the bot to select a move
        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 the next move
        print_move(game.next_player, move)
        # Apply the move
        game = game.apply_move(move)
Example #5
0
def main():
    board_size = 9
    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)
    black_score = scoring.evaluate_territory(game.board).num_black_stones
    black_score = black_score + scoring.evaluate_territory(
        game.board).num_black_territory
    white_score = scoring.evaluate_territory(game.board).num_white_stones
    white_score = white_score + scoring.evaluate_territory(
        game.board).num_white_territory
    if black_score > white_score:
        print("\nPlayer Black Wins: ")
        print(black_score)
    elif white_score > black_score:
        print("\nPlayer White Wins: ")
        print(white_score)
Example #6
0
def main():
    board_size = 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('Your turn: ')
            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,numberOfCaptures = game.apply_move(move)

        if len(numberOfCaptures) > 0:
            if game.next_player == gotypes.Player.black:
                captures[gotypes.Player.black] += numberOfCaptures
            else:
                captures[gotypes.Player.white] += numberOfCaptures

        # time.sleep(1)

    winner,score = game.winner(captures)

    if winner == gotypes.Player.black:
        # print("Black is the WINNER!!!!")
    else:
Example #7
0
    def select_move(self, game_state):
        move = goboard.Move.pass_turn()
        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_state.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")
        return move
Example #8
0
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('-- ')
            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():
    game = goboard.GameState.new_game(BOARD_SIZE)
    bot = minimax.AlphaBetaAgent(3, capture_diff)

    while not game.is_over():
        print_board(game.board)
        if game.next_player == gotypes.Player.white:
            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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
    def select_move(bot_name):
        content = request.json
        board_size = content['board_size']
        game_state = goboard.GameState.new_game(board_size)
        board_ext = goboard.Board_Ext(game_state.board)  #Nail
        # Replay the game up to this point.
        for move in content['moves']:
            if move == 'pass':
                next_move = goboard.Move.pass_turn()
            elif move == 'resign':
                next_move = goboard.Move.resign()
            else:
                pm = point_from_coords(move)
                next_move = goboard.Move.play(pm)
            game_state = game_state.apply_move(next_move)

            p = next_move.point  # Nail
            #board_ext.place_stone_ext(game_state.board, game_state.next_player.other.name[0], p)  #Nail
        bot_agent = bot_map[bot_name]

        bot_move  = bot_agent.select_move(game_state)  #,board_ext)  # Nail
        if bot_move.is_pass:
            bot_move_str = 'pass'
        elif bot_move.is_resign:
            bot_move_str = 'resign'
        else:
            bot_move_str = coords_from_point(bot_move.point)

        result_scoring, territory_black, territory_white = gr(game_state)  # Nail
        winner = result_scoring.winner.name
        if winner == 'white':
            winner = 'Белые'
        else:
            winner = 'Черные'
        score = str(result_scoring.winning_margin)
        result_scoring = result_scoring.winner.name + ' : ' + str(result_scoring.winning_margin)
        print(' Current Result = ', result_scoring, ' Bot_move = ', bot_move_str) #Nail
        print('territory_black=', territory_black, '  territory_white=', territory_white)
        return jsonify({
            'score': score,
            'winner': winner,
            'territory_black': territory_black,
            'territory_white': territory_white,
            'bot_move': bot_move_str,
            'diagnostics': bot_agent.diagnostics()
        })
Example #13
0
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)
Example #14
0
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)
Example #15
0
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)
Example #16
0
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)
Example #18
0
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)
Example #19
0
def main():
    game = goboard.GameState.new_game(BOARD_SIZE)
    bot = mcts.MCTSAgent(500, temperature=1.4)

    s = get_usb_port()  #grab a port
    print("USB Port: " + str(s))  #print it if you got
    if s:
        ser = serial.Serial(port=s,
                            baudrate=9600,
                            parity=serial.PARITY_NONE,
                            stopbits=serial.STOPBITS_ONE,
                            bytesize=serial.EIGHTBITS,
                            timeout=0.01)  #auto-connects already I guess?
        print("Serial Connected!")
        if ser.isOpen():
            print(ser.name + ' is open...')
    else:
        print("No Serial Device :/ Check USB cable connections/device!")
        exit()

    while not game.is_over():
        print(chr(27) + "[2J")
        print_board(game.board)
        if game.next_player == gotypes.Player.black:
            while True:
                # human_move = input('-- ')
                read_data = ser.read(
                    1)  #read the buffer (99/100 timeout will hit)
                if read_data != b'':  #if not nothing there.
                    fpga_move = read_data.hex()
                    if fpga_move == "ff":
                        move = goboard.Move.pass_turn()
                        fpga_passed = True
                        break
                    else:
                        fpga_passed = False
                    fpga_row = 10 - (int(fpga_move[0], base=16) + 1)
                    fpga_col = int(fpga_move[1], base=16)
                    col = COLS[fpga_col]
                    print(col, fpga_row)
                    print("RXd: ")
                    print(fpga_move)
                    point = point_from_coords(col + str(fpga_row))
                    move = goboard.Move.play(point)
                    fpga_move = move
                    break
        else:
            move = bot.select_move(game)
            if move.is_pass:
                low_nibble = 15
                high_nibble = 15
            else:
                low_nibble = move.point.col - 1
                high_nibble = move.point.row
            time.sleep(.5)
            if fpga_passed:
                move = goboard.Move.pass_turn()
            if move.is_pass:
                write_data = (255).to_bytes(1, byteorder="big")
            else:
                write_data = (abs((int(high_nibble) - 9)) * 16 +
                              (int(low_nibble))).to_bytes(1, byteorder="big")
            print(write_data)
            ser.write(write_data)

        print_move(game.next_player, move)
        game = game.apply_move(move)
Example #20
0
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()
Example #21
0
def translate_label_to_point(label: str) -> Optional[Point]:
    return None if not label or label.lower() == 'pass' else point_from_coords(
        label)