double Probablistic_cell::getProbForOccupied(const bool reset)
{
    if(log_odds == 0.0)
        return -1;

    double occupied_prob = 1 - 1 / (1 + std::exp(log_odds));

    if(reset)
    {

        resetCell();
    }
    return occupied_prob;
}
Exemple #2
0
int evaluateMaxMove( unsigned int *board, int depth )
{
    int value, x, y;
    int max = MIN_INFINITY-1;
    int scores1[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
    int scores2[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
    
    // printf("DEPTH=%d\n", depth);
    boards_checked++;
    /*
     for (i = 0; i < depth; i++) printf(" ");
     printf("%d min %d %d\n", depth, last_move_x, last_move_y);
     */
    
    /* Player 1 (min) just made a move, so we evaluate that move here */
    if (checkPlayerWin(X_PLAYER, board)) return MIN_INFINITY;
    
    /* If Player 2 has a winning position, take it */
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++) {
            
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                scores1[x][y] = countScore( O_PLAYER, x, y, board, 4);
                if (scores1[x][y] == MAX_INFINITY) {
                    if (depth == 0) { computer_move_x = x; computer_move_y = y; }
                    return MAX_INFINITY;
                }
            }
        }
    
    /* If Player 1 has a winning position, block it */
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++)
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                
                scores2[x][y] = countScore( X_PLAYER, x, y, board, 4);
                if (scores2[x][y] == MAX_INFINITY) {
                    putCell( O_PLAYER, x, y, board );
                    if (depth == 0) { computer_move_x = x; computer_move_y = y; return 0; }
                    max = evaluateMinMove( board, depth+1 );
                    resetCell( x, y, board);
                    return max;
                }
            }
    
    /* Cutoff checking */
    if (depth >= CUTOFF) {
        // printf("depth %d scores\n", depth);
        for (x = 0; x < MAX_BOARD_SIZE; x++)
            for (y = 0; y < MAX_BOARD_SIZE; y++)
                if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                    value = (scores2[x][y])+(scores1[x][y]<<1);
                    // printf("max (%d %d) %d %d %d\n", x, y, value, scores1[x][y], scores2[x][y]);
                    if (value > max) {
                        max = value;
                    }
                }
        return max;
    }
    
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++)
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                
                putCell( O_PLAYER, x, y, board );
                value = evaluateMinMove( board, depth+1 );
                resetCell( x, y, board);
                
                // printf("depth=%d value = %d for %d %d\n", depth, value, x, y);
                if (value > max) {
                    max = value;
                    if (depth == 0) { 
                        computer_move_x = x; computer_move_y = y; 
                    }
                    if (max == MAX_INFINITY) return max;
                }
                
            }
    
    /* No move is possible -- draw */
    if (max == MIN_INFINITY-1) {
        return DRAW;
    }
    
    return max;
}
Exemple #3
0
int evaluateMinMove( unsigned int *board, int depth )
{
    int value, x, y;
    int min = MAX_INFINITY+1;
    int scores1[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
    int scores2[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
    
    // printf("DEPTH=%d\n", depth);
    boards_checked++;
    /*
     for (i = 0; i < depth; i++) printf(" ");
     printf("%d max %d %d\n", depth, last_move_x, last_move_y);
     */
    
    /* Player 2 (max) just made a move, so we evaluate that move here */
    if (checkPlayerWin(O_PLAYER, board)) return MAX_INFINITY;
    
    /* If Player 1 has a winning position, take it */
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++)
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                scores1[x][y] = countScore( X_PLAYER, x, y, board, 4);
                if (scores1[x][y] == MAX_INFINITY) return MIN_INFINITY;
            }
    
    /* If Player 2 has a winning position, block it */
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++)
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                scores2[x][y] = countScore( O_PLAYER, x, y, board, 4);
                if (scores2[x][y] == MAX_INFINITY) {
                    putCell( X_PLAYER, x, y, board );
                    min = evaluateMaxMove( board, depth+1 );
                    resetCell( x, y, board);
                    return min;
                }
            }
    
    /* Cutoff checking */
    if (depth >= CUTOFF) {
        // printf("depth %d scores\n", depth);
        for (x = 0; x < MAX_BOARD_SIZE; x++)
            for (y = 0; y < MAX_BOARD_SIZE; y++)
                if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                    // the value of position (x, y) is the sum of its score when the opponent is placed
                    // at that position plus the twice of the score when itself is placed at that position.
                    value = (scores2[x][y])+(scores1[x][y]<<1);
                    
                    // printf("min (%d %d) %d %d %d\n", x, y, value, scores1[x][y], scores2[x][y]);
                    
                    // the value is negated for min-move
                    value = -value;
                    if (value < min) {
                        min = value;
                    }
                }
        return min;
    }
    
    for (x = 0; x < MAX_BOARD_SIZE; x++)
        for (y = 0; y < MAX_BOARD_SIZE; y++)
            if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) {
                
                putCell( X_PLAYER, x, y, board );
                value = evaluateMaxMove( board, depth+1 );
                resetCell(x, y, board);
                
                // printf("depth=%d value = %d for %d %d\n", depth, value, x, y);
                if (value < min) {
                    min = value;
                    if (min == MIN_INFINITY) return min;
                }
            }
    
    /* No move is possible -- draw */
    if (min == MAX_INFINITY+1) {
        return DRAW;
    }
    
    return min;
}