Ejemplo n.º 1
0
def evaluate_board(board: SimpleGoBoard):
    init_score_cache(board)
    # current_player = board.current_player
    # opponent_player = GoBoardUtil.opponent(current_player)
    for row in range(1, board.size + 1):
        for col in range(1, board.size + 1):
            p = _constrained_index_2d(board, row, col)
            m = coord_to_point(row, col, board.size)
            if p == EMPTY:
                board.board[m] = WHITE
                score_color[WHITE][row][col] = evaluate_point_dir(
                    board, row, col, WHITE, None)
                board.board[m] = BLACK
                score_color[BLACK][row][col] = evaluate_point_dir(
                    board, row, col, BLACK, None)
                board.board[m] = EMPTY
            elif p == WHITE:
                score_color[WHITE][row][col] = evaluate_point_dir(
                    board, row, col, WHITE, None)
                score_color[BLACK][row][col] = 0
            elif p == BLACK:
                score_color[WHITE][row][col] = 0
                score_color[BLACK][row][col] = evaluate_point_dir(
                    board, row, col, BLACK, None)
    return
Ejemplo n.º 2
0
 def test_size_2(self):
     goboard = SimpleGoBoard(2)
     self.assertEqual(goboard.size, 2)
     self.assertEqual(goboard.NS, 3)
     self.assertEqual(goboard.WE, 1)
     self.assertEqual(goboard.ko_recapture, None)
     self.assertEqual(goboard.current_player, BLACK)
     self.assertEqual(goboard.maxpoint, 13)
     self.assertEqual(goboard.board[0], BORDER)
     self.assertEqual(goboard.board[goboard.pt(1,1)], EMPTY)
     self.assertEqual(goboard.board[goboard.pt(1,2)], EMPTY)
     self.assertEqual(goboard.board[goboard.pt(2,1)], EMPTY)
     self.assertEqual(goboard.board[goboard.pt(2,2)], EMPTY)
Ejemplo n.º 3
0
def run(sim, move_select, sim_rule, move_filter):
    """
    start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(Nogo(sim, move_select, sim_rule, move_filter), board)
    con.start_connection()
Ejemplo n.º 4
0
    def genmove(self, board):

        result, winner = board.check_game_end_gomoku(self)
        assert not result

        simulate_moves = board.get_empty_point()
        simulate_moves_num = len(simulate_moves)
        score = [0] * simulate_moves_num
        for i in range(simulate_moves_num):
            move = simulate_moves[i]
            score[i] = self.mc_simulate(board, move)

        best_move_index = score.index(max(score))
        best_move = simulate_moves[best_move_index]
        assert best_move in simulate_moves
        return best_move
Ejemplo n.º 5
0
def run():
    """
    start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(Go0(), board)
    con.start_connection()
Ejemplo n.º 6
0
def run(sim, selection):
    """
    Start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(Nogo(sim, selection), board)
    con.start_connection()
Ejemplo n.º 7
0
def run(sim, sim_rule, move_filter, in_tree_knowledge):
    """
    Start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnectionGo3(Go5(num_sim, sim_rule, move_filter, in_tree_knowledge), board)
    con.start_connection()
Ejemplo n.º 8
0
def run():
    """
    start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection2(Go3Player(num_simulation), board)
    con.start_connection()
Ejemplo n.º 9
0
def run(sim_rule, move_filter):
    """
    Start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnectionGo3(PolicyPlayer(sim_rule, move_filter), board)
    con.start_connection()
Ejemplo n.º 10
0
def run():
    """
    start the gtp connection and wait for commands
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(Gomoku3(sim=10, sim_rule="rule_based"), board)
    con.start_connection()
def run():
    """
    start the gtp connection and wait for commands.
    """
    board = SimpleGoBoard(7)
    con = GtpConnection(GomokuMCTSPlayer(n_playout=4000), board)
    con.start_connection()
Ejemplo n.º 12
0
def run():
    """
      start the gtp connection and wait for commands.
      """
    #random.seed() no need to seed because we use np.random
    board = SimpleGoBoard(7)
    con = GtpConnection(Gomoku(), board)
    con.start_connection()
Ejemplo n.º 13
0
 def do_test_pointsets(self, size):
     goboard = SimpleGoBoard(size)
     count = count_colors(goboard)
     self.assertEqual(count[EMPTY], size * size)
     self.assertEqual(count[BLACK], 0)
     self.assertEqual(count[WHITE], 0)
     num_border = 3 * (size + 1)
     self.assertEqual(count[BORDER], num_border)
Ejemplo n.º 14
0
def update_dir(board: SimpleGoBoard, row, col, direction):
    current = _constrained_index_2d(board, row, col)
    m = coord_to_point(row, col, board.size)
    if current == EMPTY:
        board.board[m] = WHITE
        score_color[WHITE][row][col] = evaluate_point_dir(
            board, row, col, WHITE, direction)
        board.board[m] = BLACK
        score_color[BLACK][row][col] = evaluate_point_dir(
            board, row, col, BLACK, direction)
        board.board[m] = EMPTY
    elif current == BLACK or current == WHITE:
        oppponent = GoBoardUtil.opponent(current)
        score_color[current][row][col] = evaluate_point_dir(
            board, row, col, current, direction)
        score_color[oppponent][row][col] = 0
    return
Ejemplo n.º 15
0
def run(num_sim, sim_rule, in_tree_knowledge):
    #--------------------------
    use_pattern = None
    #--------------------------
    board = SimpleGoBoard(7)
    con = GtpConnection(
        Gomoku5(num_sim, use_pattern, sim_rule, in_tree_knowledge), board)
    con.start_connection()
Ejemplo n.º 16
0
def solve_alphabeta(board: SimpleGoBoard, color: int, board_is_evaluated=False):
    result, winner = board.check_game_end_gomoku()
    if result: 
        if winner == color:
            return 10*score.FIVE, None
        else: 
            return -10*score.FIVE, None
    # print('ok')
    alpha, beta = -10*score.FIVE, 10*score.FIVE
    if not board_is_evaluated:
        evaluate_board(board)
    moves = gen_possible_moves(board, color)
    print(list(map(lambda t: (t[0], gtp_connection.format_point(
        gtp_connection.point_to_coord(t[0], board.size)), t[1], t[2]), moves)))
    if len(moves) == 0: 
        return 0, None
    if (gtp_connection.total_steps == 0 or gtp_connection.total_steps == 1) and board.board[36] == EMPTY: 
        return 1, 36

    # best_move = None
    global best_move
    best_move = PASS
    for m in moves:
        move = m[0]

        # move_coord = gtp_connection.point_to_coord(move, board.size)
        # move_as_string = gtp_connection.format_point(move_coord)
        
        board.board[move] = color
        update_board(board, move)
        result = -alphabeta_search(board, move, GoBoardUtil.opponent(color), 40, -beta, -alpha)
        # print("trying move:", move_as_string, "score:", result)

        board.board[move] = EMPTY
        update_board(board, move)
        if result > alpha: 
            alpha = result
            best_move = move
        if result >= beta: 
            return beta, move
    return alpha, best_move
Ejemplo n.º 17
0
 def mc_simulate(self, board, move):
     stats = [0] * 3
     board.play(move)
     moveNr = board.moveNumber()
     for _ in range(self.numSim):
         winner = board.simulate()
         stats[winner] += 1
         board.resetToMoveNumber(moveNr)
     assert sum(stats) == self.numSim
     board.undo_move()
     eval = (stats[BLACK] + 0.5 * stats[EMPTY]) / self.numSim
     if board.current_player == WHITE:
         eval = 1 - eval
     return eval
Ejemplo n.º 18
0
def boolean_negamax(board: SimpleGoBoard, last_move: int, current_color: int) -> ('result', 'move'):
    if debug: 
        print_board(board)
    if last_move is None: 
        evaluate_board(board)

    if last_move is not None and check_winning_condition(board, last_move, board.board[last_move]):
        # print('game ends')
        return -1, None

    opponent_color = GoBoardUtil.opponent(current_color)
    moves = gen_possible_moves(board, current_color)
    if len(moves) == 0:
        return 0, None

    best_result = -1
    best_move = moves[0][0]
    if debug:
        print_moves(moves, board.size)

    for m in moves:
        move = m[0]
        board.board[move] = current_color
        update_board(board, move)
        # print('play', gtp_connection.point_to_str(move, board.size))
        result, _ = boolean_negamax(board, move, opponent_color)
        result = -result
        board.board[move] = EMPTY
        update_board(board, move)
        if result > 0:
            # print("win")
            return 1, move
        if result > best_result:
            best_result = result
            best_move = move

    return best_result, best_move
Ejemplo n.º 19
0
def alphabeta_search(
    board: SimpleGoBoard, 
    last_move: int, 
    current_color: int, 
    depth: int, 
    alpha: int, beta: int) -> int:
    # if last_move is None:
    #     evaluate_board(board)
    # last_move is not None
    if check_winning_condition(board, last_move, board.board[last_move]):
        return -10 * score.FIVE

    if depth <= 0:
        return point_estimation(board, current_color)
    opponent_color = GoBoardUtil.opponent(current_color)
    moves = gen_possible_moves(board, current_color)
    if len(moves) == 0:
        return 0
    depth -= min(10, len(moves))

    if debug:
        print_moves(moves, board.size)

    for m in moves:
        move = m[0]
        board.board[move] = current_color
        update_board(board, move)
        result = -alphabeta_search(board, move, opponent_color, depth, -beta, -alpha)
        board.board[move] = EMPTY
        update_board(board, move)
        if result > alpha:
            alpha = result
        if result >= beta:
            return beta
        
    return alpha
Ejemplo n.º 20
0
 def test_size_2_play_move(self):
     size = 2
     goboard = SimpleGoBoard(size)
     goboard.play_move(goboard.pt(1,1), BLACK)
     count = count_colors(goboard)
     self.assertEqual(count, [size * size - 1, 1, 0, 3 * (size + 1)])
Ejemplo n.º 21
0
def playgame():
    board = SimpleGoBoard(7)
Ejemplo n.º 22
0
def gen_possible_moves(board: SimpleGoBoard, color):
    fives = []
    fours = {BLACK: [], WHITE: []}
    blocked_fours = {BLACK: [], WHITE: []}
    two_threes = {BLACK: [], WHITE: []}
    threes = {BLACK: [], WHITE: []}
    twos = {BLACK: [], WHITE: []}
    others = []

    current = color
    opponent = GoBoardUtil.opponent(current)

    empty_points = board.get_empty_points()
    for point in empty_points:
        row, col = point_to_coord(point, board.size)
        current_score = score_color[current][row][col]
        opponent_score = score_color[opponent][row][col]

        move = coord_to_point(row, col, board.size)

        # always consider current side, then the opposite side
        if current_score >= score.FIVE:
            fives.append((move, current_score, opponent_score))
        elif opponent_score >= score.FIVE:
            fives.append((move, current_score, opponent_score))
        elif current_score >= score.FOUR:
            fours[current].append((move, current_score, opponent_score))
        elif opponent_score >= score.FOUR:
            fours[opponent].append((move, current_score, opponent_score))
        elif current_score >= score.BLOCKED_FOUR:
            blocked_fours[current].append(
                (move, current_score, opponent_score))
        elif opponent_score >= score.BLOCKED_FOUR:
            blocked_fours[opponent].append(
                (move, current_score, opponent_score))
        elif current_score >= 2 * score.THREE:
            two_threes[current].append((move, current_score, opponent_score))
        elif opponent_score >= 2 * score.THREE:
            two_threes[opponent].append((move, current_score, opponent_score))
        elif current_score >= score.THREE:
            threes[current].append((move, current_score, opponent_score))
        elif opponent_score >= score.THREE:
            threes[opponent].append((move, current_score, opponent_score))
        elif current_score >= score.TWO:
            twos[current] = [(move, current_score, opponent_score)
                             ] + twos[current]
        elif opponent_score >= score.TWO:
            twos[opponent] = [(move, current_score, opponent_score)
                              ] + twos[opponent]
        else:
            others.append((move, current_score, opponent_score))

    # if debug:
    #     print('fives:', fives)
    #     print('current fours:', fours[current])
    #     print('current blocked fours:', blocked_fours[current])
    #     print('current two threes:', two_threes[current])
    #     print('current threes:', threes[current])

    if len(fives) > 0:
        # form our fives or block other's fives
        return fives

    if len(fours[current]) > 0:
        # form our fours
        return fours[current]

    if len(fours[opponent]) > 0 and len(blocked_fours[current]) <= 0:
        # other has fours, but we don't even have blocked four
        return fours[opponent]

    # others have fours, but we have blocked fours, then we can take those blocked fours
    total_fours = fours[current] + fours[opponent]
    total_blocked_fours = blocked_fours[current] + blocked_fours[opponent]
    if len(total_fours) > 0:
        return total_fours + total_blocked_fours

    # two threes, either block or form
    result = two_threes[current] + two_threes[opponent] + blocked_fours[
        current] + blocked_fours[opponent] + threes[current] + threes[opponent]
    if len(two_threes[current]) > 0 or len(two_threes[opponent]) > 0:
        return result

    # twos, not very useful...
    total_twos = twos[current] + twos[opponent]
    # total_twos.sort(key=lambda t: max(t[1],t[2]), reverse=True)
    total_twos.sort(reverse=True)
    if len(total_twos) > 0:
        result += total_twos
    else:
        result += others
    # remember to add other points

    return result[:20]
Ejemplo n.º 23
0
#!/usr/bin/python3
import os, sys

utilpath = sys.path[0] + "/../util/"
sys.path.append(utilpath)
utilpath = sys.path[0] + "/../Go4/"
sys.path.append(utilpath)
from simple_board import SimpleGoBoard
import numpy as np
from Go5 import Go5Player
import time
from board_util_go4 import BLACK, WHITE

board = SimpleGoBoard(4)
player = Go5Player(num_simulation=200, limit=100, exploration=np.sqrt(2))
player.MCTS.komi = 6.5
player.num_nodes = 5
player.MCTS.simulation_policy = 'random'
cboard = board.copy()
print("\nrunning playout 200 times\n")
player.sample_run(cboard, BLACK, print_info=True)

time.sleep(30)  # sleeping
player.num_simulation = 300
print("\nrunning it 300 more times\n")
cboard = board.copy()
player.sample_run(cboard, BLACK, print_info=True)

time.sleep(30)
print("\nrunning it 300 more times\n")
cboard = board.copy()
Ejemplo n.º 24
0
 def test_size_2_legal_moves(self):
     size = 2
     goboard = SimpleGoBoard(size)
     moves = GoBoardUtil.generate_legal_moves(goboard, BLACK)
     self.assertEqual(moves, [goboard.pt(1,1), goboard.pt(1,2), 
                              goboard.pt(2,1), goboard.pt(2,2)])
Ejemplo n.º 25
0
 def __init__(self, value_fn, n_playout = 10000):
     self.root = TreeNode(None, SimpleGoBoard(7))
     self.value_fn = value_fn
     self.n_playout = n_playout
Ejemplo n.º 26
0
 def test_size_2(self):
     board = SimpleGoBoard(2)
     con = GtpConnection(Go0(), board)
     con.start_connection()