Example #1
0
def paranoid(node, searching_player, alpha=-sys.maxsize, beta=sys.maxsize):
    # If the max depth or goal state has been reached.
    if node.depth == DIFFICULTY or not any(node.board[BOARD_SIZE*node.turn+1:(node.turn+1)*BOARD_SIZE-1]):
        return assign_paranoid_value(node.board, searching_player), None
    
    global NODES_VISITED
    best_move = None
    searcher = node.turn == searching_player
    v = sys.maxsize*(-1 if searcher else 1)
    
    for m in POSSIBLE_MOVES:
        result = do_move(m, node.turn, deepcopy(node.board))
        if result == None: continue
        
        val, _ = paranoid(Node(result, node.depth+1), searching_player, alpha, beta)
        NODES_VISITED += 1
        
        if searcher:
            if val > alpha: best_move = m
            v = max(v, val)
            alpha = max(alpha, v)
        else:
            if val < beta: best_move = m
            v = min(v, val)
            beta = min(beta, v)
        
        if beta <= alpha: break
        
    return v, best_move
Example #2
0
def max_n(node, searching_player):
    # If the max depth or goal state has been reached.
    if node.depth == DIFFICULTY or not any(node.board[BOARD_SIZE*node.turn+1:(node.turn+1)*BOARD_SIZE-1]):
        return [assign_maxn_value(node.board, p) for p in PLAYERS], None

    global NODES_VISITED
    best_move = None
    best_result = [sys.maxsize*(-1)]*NUM_PLAYERS

    for m in POSSIBLE_MOVES:
        result = do_move(m, node.turn, deepcopy(node.board))
        if result == None: continue
        
        values, _ = max_n(Node(result, node.depth+1), searching_player)
        NODES_VISITED += 1

        if values[searching_player.row] > best_result[searching_player.row]:
            best_result = values; best_move = m
        
    return best_result, best_move
Example #3
0
def min_max(node, searching_player):
    # If the max depth or goal state has been reached.
    if node.depth == DIFFICULTY or not any(node.board[BOARD_SIZE*node.turn+1:(node.turn+1)*BOARD_SIZE-1]):
        return assign_value(node.board, searching_player), None
    
    global NODES_VISITED
    best_move = None
    searcher = node.turn == searching_player
    v = sys.maxsize*(-1 if searcher else 1)
    
    for m in POSSIBLE_MOVES:
        result = do_move(m, node.turn, deepcopy(node.board))
        if result == None: continue
        
        val, _ = min_max(Node(result, node.depth+1), searching_player)
        NODES_VISITED += 1
        
        if searcher and val > v or not searcher and val < v:
            v = val; best_move = m
        
    return v, best_move
Example #4
0
def cooperative(node, searching_player):
    b, row_size = node.board, BOARD_SIZE
    # If the max depth or goal state has been reached.
    if node.depth == DIFFICULTY or not any(node.board[BOARD_SIZE*node.turn+1:(node.turn+1)*BOARD_SIZE-1]):
        return [assign_maxn_value(node.board, p) for p in PLAYERS], None

    global NODES_VISITED
    best_move = None
    best_result = [sys.maxsize*(-1)]*NUM_PLAYERS
    best_value = sys.maxsize*(-1)

    for m in POSSIBLE_MOVES:
        result = do_move(m, node.turn, deepcopy(node.board))
        if result == None: continue
        
        values, _ = cooperative(Node(result, node.depth+1), searching_player)
        NODES_VISITED += 1

        this_val = values[searching_player.row]*2 + values[PREVIOUS_PLAYER.row]
        if this_val > best_value:
            best_value, best_result, best_move = this_val, values, m
        
    return best_result, best_move