Exemple #1
0
def play_random(board, moving_plr, num_moves):
    """Take board, and play a random game for # moves"""
    import helpers, random
    for i in range(num_moves):
        if moving_plr is None:
            return board, None
        move = random.choice(helpers.legal_moves(board, moving_plr))
        helpers.make_move(board, moving_plr, move)
        moving_plr = helpers.next_player(board, moving_plr)
    return board, moving_plr
Exemple #2
0
def run_game(weights):
    """Run a game of the current AI vs the old AI"""
    import helpers, random, heuristics
    helpers.SQUARE_WEIGHTS = weights
    black_moves = []
    white_moves = []
    black_ai = duv_ai_wrapper
    white_ai = djones_ai_wrapper
    # keep track of whether or not we switched the starting order
    black_ai_type = 'training'
    if random.random() < 0.5:
        black_ai, white_ai = white_ai, black_ai
        black_ai_type = 'nottraining'
    board = array.array('B', [helpers.EMPTY] * 64)
    board[0o33] = helpers.BLACK
    board[0o44] = helpers.BLACK
    board[0o34] = helpers.WHITE
    board[0o43] = helpers.WHITE
    player = helpers.BLACK
    PLR2AI = {helpers.BLACK: black_ai, helpers.WHITE: white_ai}
    player = play_random(board, player, 8)[1]
    while player is not None:
        # depth of 2, we want this to be FAST
        move = PLR2AI[player](board, player, 2)
        if move not in helpers.legal_moves(board, player):
            raise RuntimeError(
                f"Illegal move for {helpers.PLAYERS[player]}: {move}")
        if player == BLACK:
            black_moves.append(move)
        else:
            white_moves.append(move)
        helpers.make_move(board, player, move)
        player = helpers.next_player(board, player)
        # print_board(board)
        # print()
    if black_ai_type == 'training':
        didWin = heuristics.count_colors(board, BLACK) > 0
    else:
        didWin = heuristics.count_colors(board, WHITE) > 0
    if heuristics.count_colors(board, BLACK) > 0:
        return black_moves, white_moves, heuristics.count_colors(board,
                                                                 BLACK), didWin
    elif heuristics.count_colors(board, WHITE) > 0:
        return white_moves, black_moves, heuristics.count_colors(board,
                                                                 WHITE), didWin
    else:  # it was a tie
        return [], [], 0, False
Exemple #3
0
func = cfunc_caller.generate_function(code, ctypes.c_int, ctypes.c_char_p,
                                      ctypes.c_char, ctypes.c_char_p)

board = helpers.from_tournament_format(
    list(
        '???????????........??........??........??...o@...??...@o...??........??........??........???????????'
    ))
board = helpers.from_tournament_format(
    list(
        '???????????@@@@@oo@??@@@@@@@@??@@o@o@@@??@@@o@@o@??o@@@@@o@??o@@@ooo@??o@@@@oo@??..@@@@@@???????????'
    ))
SQUARE_WEIGHTS = [
    120, -20, 20, 5, 5, 20, -20, 120, -20, -40, -5, -5, -5, -5, -40, -20, 20,
    -5, 15, 3, 3, 15, -5, 20, 5, -5, 3, 3, 3, 3, -5, 5, 5, -5, 3, 3, 3, 3, -5,
    5, 20, -5, 15, 3, 3, 15, -5, 20, -20, -40, -5, -5, -5, -5, -40, -20, 120,
    -20, 20, 5, 5, 20, -20, 120
]
buf = array.array('b', SQUARE_WEIGHTS)


def array_pointer(arr):
    return ctypes.cast(arr.buffer_info()[0], ctypes.c_char_p)


print("Function result: " +
      str(func(array_pointer(board), 1, array_pointer(buf))))
print("Arr: " + str(board))
print("Buf: " + str(buf))
print("Legal moves: " + str(helpers.legal_moves(board, 1)))