Exemple #1
0
int AlphaBetaQId::evaluateMoveTreeQuiesce(int depth, int alpha, int beta) {
#ifdef USE_POSITION_KEY
	int a = alpha;	// remember argument
	int w;
	if (probe(w, alpha, beta, depth)) return w;
#endif
	std::list<Move> l = possibleMoves();

	if (l.empty())		 { return -mateValue(tree_depth-depth);	}	// game over, node is terminal. return mate in plies
	if (l.front().isQuiet()) { return evaluatePosition();		}	// no jumps, so quiesence search over. node is (at least) quiet

	Move best_move = NoMove;

	for (const auto& m: l) {						// play forced moves
		move(m);
		int v = -evaluateMoveTreeQuiesce(depth-1, -beta, -alpha);
		undo(m);

		if (v >= beta)  { store(v, a, beta, NoMove, depth); return v; }
		if (v >  alpha) { alpha = v; best_move = m; }
	}

	store(alpha, a, beta, best_move, depth);				// allow negative depthes in TT

	return alpha;
}
Exemple #2
0
Move getBestMove(board Game, Piece piece, Point point) {
    Move possibleMoves[26];
    int possibleMovesNumber = getPossibleMoves(Game,
                                               piece,
                                               possibleMoves,
                                               point);
    Move result = NULL;
    int bestValue = -INFINITY;

    for (int i = 0; i < possibleMovesNumber; i++) {
        doMove(Game, possibleMoves[i]);

        int moveValue = evaluatePosition(Game, aiPlayerNumber,
                                         possibleMoves[i]);

        if (moveValue > bestValue) {
            result = possibleMoves[i];
            bestValue = moveValue;
        }
        undoMove(Game, possibleMoves[i]);

    }

    for (int i=0; i<possibleMovesNumber; i++) {
        if (result != possibleMoves[i]) {
            cleanMoveData(possibleMoves[i]);
        }
    }

    return result;
}
Exemple #3
0
Move MCBoard::findBestMove() {
	Move best_move = NoMove;
	std::list<Move> l = possibleMoves();

	int best_value = -mateValue();
	for (const auto& m: l) {
		move(m);
		std::cerr << m.toString() << std::endl;
		int r = evaluatePosition(turn);
		if (r > best_value) {
			best_value = r;
			best_move = m;
		}
		std::cerr << r << " - " << best_value << std::endl;

		undo(m);
	}

	return best_move;
}
Exemple #4
0
pair chooseMoveABDepth (int* iboard, int player, int alpha, int beta) {
    ASSERT(player == XPL || player == OPL);
    
    pair myBest = {0,-1};
    pair reply;
    int i,j, move;
    int moveCount=0, moveList[9];

    //default score
    if(player == US){
        myBest.score = alpha;
    } else {
        myBest.score = beta;
    }


    timesCalled++;
    pair temp;
    //temp.score = evaluateBoard(iboard);
    temp.move = -1;

    // if(level == 0){
    //     temp.score = evaluateBoard(iboard);
    //     return temp;
    // }

    if(didHeWin(iboard,US)) {
        //printf("%c WON\n",US);
        //printboard(iboard);        
        //temp.score = evaluateBoard(iboard);
        temp.score = evaluatePosition(iboard,US);
        return temp;
    } else if(didHeWin(iboard,THEM)) {
        //printf("%c WON\n",THEM);
        //printboard(iboard);          
        //temp.score = evaluateBoard(iboard);
        temp.score = evaluatePosition(iboard,US);
        return temp;
    } else if(isBoardFull(iboard)) {
        //printf("FULL\n");
        //printboard(iboard);          
        temp.score = evaluatePosition(iboard,US);
        return temp;
    }
    
    // get legal moves in25
    for(i = 0; i < 9; i++) {
        if( iboard[convert9To25[i]] == EMPTY) {
            moveList[moveCount++] = convert9To25[i];
        }
    }

    // for(i = 0; i < 9; i++) {
    //     if( iboard[convert9To25[cellScoreOrdered[i]]] == EMPTY) {
    //         moveList[moveCount++] = convert9To25[cellScoreOrdered[i]];
    //     }
    // }

    myBest.move = moveList[0];
    for(i=0; i < moveCount; i++) {
        move = moveList[i];
        iboard[move] = player;
        
        reply = chooseMoveAB(iboard, otherPlayer(player),alpha,beta);

        iboard[move] = EMPTY;
        // maximum lower bound of possible solutions
        if(player == US && myBest.score < reply.score){
            alpha = reply.score;
            myBest.score = evaluatePosition(iboard,US);
            myBest.move = move;            
        }
        // minimum upper bound of possible solutions 
        else if (player == THEM && myBest.score > reply.score){
            beta = reply.score;
            myBest.score = reply.score;
            myBest.move = move;            
        }

        if(alpha >= beta){
            return myBest;
        }
    }
    return myBest;
}