/* A very silly random search 'algorithm' */
void randomSearch() {
    int queen, iter = 0;
    int optimum = (nqueens-1)*nqueens/2;

    while (evaluateBuffer() != optimum) {
        printf("iteration %d: evaluation=%d\n", iter++, evaluateBuffer());
        if (iter == MAXITER) break;  /* give up */
        /* generate a (new) random state: for each queen do ...*/
        for (queen=0; queen < nqueens; queen++) {
            int pos, newpos;
            /* position (=column) of queen */
            pos = columnOfQueen(queen);
            /* change in random new location */
            newpos = pos;
            while (newpos == pos) {
                newpos = random() % nqueens;
            }
            moveQueen(queen, newpos);
        }
    }
    if (iter < MAXITER) {
        printf ("Solved puzzle. ");
    }
    printf ("Final state is");
    printState();
}
void hillClimbing() {
    int iterations = 0;
    int optimum = (nqueens-1)*nqueens/2;
    
    int current_state[nqueens], i;
    for (i=0; i<nqueens; i++) {
        current_state[i] = queens_buffer[i];
    }
    
    while ((iterations < MAXITER) && (evaluateBuffer() != optimum)) {
        int best_move_evaluation = evaluateBuffer(); // Value to be maximized. Initialize with the current evaluation.
        nsuccessors = 0;
        
        // Loops through all possible moves
        int i, j;
        for (i=0; i<nqueens; i++) {
            for (j=0; j<nqueens; j++) {
                moveQueen(i,j);
                int successor_evaluation = evaluateBuffer();
                if (successor_evaluation > best_move_evaluation) {
                    saveToSuccessors(0);
                    nsuccessors = 1;
                    best_move_evaluation = successor_evaluation;
                }
                else if (successor_evaluation == best_move_evaluation) {
                    saveToSuccessors(nsuccessors);
                    nsuccessors++;
                }
                
                // Undo last move
                setBuffer(current_state);
            }
        }
        
        // Select randomly the best successor
        if (nsuccessors != 0) {
            int random_successor = rand() % nsuccessors;
            setBuffer(successors[random_successor]);
        }
        
        // Update current state
        for (i=0; i<nqueens; i++) {
            current_state[i] = queens_buffer[i];
        }
        
        iterations++;
    }
    if (evaluateBuffer() == optimum) {
        printf ("Solved puzzle.\n");
        printf ("Solution:");
        printState();
        solutions_found++;
    }
    else {
        printf("Puzzle not solved (maximum number of iterations). Final state:\n");
        printState();
    }
}
void simulatedAnnealing() {
    clock_t clock_start;
    int optimum = (nqueens-1)*nqueens/2;
    int iterations = 0;
    double current_temperature = INITIAL_TEMPERATURE;
    
    // Starts the clock counter
    clock_start = clock();
    while (evaluateBuffer() != optimum && current_temperature > 0.005) {
        // Save current state
        int current_evaluation = evaluateBuffer();
        int current_state[nqueens], i;
        for (i=0; i<nqueens; i++) {
            current_state[i] = queens_buffer[i];
        }
        setBuffer(current_state);
        
        // Make a random move
        int random_queen = rand() % nqueens;
        int random_column = rand() % nqueens;
        moveQueen(random_queen, random_column);
        int successor_evaluation = evaluateBuffer();
        
        int delta = successor_evaluation - current_evaluation;
        // If the chosen successor is better than the current state
        if (delta > 0) {
            // do nothing
        }
        else {
            double probability_of_change = exp(delta/current_temperature);
            double random_number = (rand() % 100) / (double)100;
            if (random_number < probability_of_change) {
                // do nothing
            }
            else { // Else, revert state to the original one
                setBuffer(current_state);
            }
        }
        
        // Update temperature
        current_temperature = timeToTemperature(clock_start);
        iterations++;
    }
    if (evaluateBuffer() == optimum) {
        printf ("Solved puzzle.\n");
        printf ("Solution:");
        printState();
        solutions_found++;
    }
    else {
        printf("Not solved in this iteration. Final state:");
        printState();
    }
}
示例#4
0
int menacedPiecesSingleType(Board * actualBoard, int pieceColor, int pieceType){

	int piecesPosition[8], menacedValue;

	//Select the proper took color according to pieceColor
	BitMap piecesBitmap=0;

	if(pieceColor==BLACK)
	{
		piecesBitmap=actualBoard->blackPieces[pieceType];
	}
	else
	{
		piecesBitmap=actualBoard->whitePieces[pieceType];

	}


	int ppSuccess=getPiecePosition(&piecesBitmap,pieceType,piecesPosition);

	//Check if a table is empty return 0: cannot menace anyone!
	if(ppSuccess == NO_PIECES_IN_BOARD)
	{
		return 0;
	}
	if ( ppSuccess != SUCCESS ) {
		//logChess(WARN, "The piece position could not be retrieved.");
	}

	//for each position in the table let's calculate the menaced pieces
	int i;
	for(i=0; i<8; i++)
	{
		if((piecesPosition[i] == -9) ||( piecesPosition[i] ==- 1))
		{
			break;//exit from the for cycle if there are no extra movement to search
		}
		else
		{
			int col = piecesPosition[i] % 8;
			int row = (piecesPosition[i] / 8);

			BitMap aux_board = 0x8000000000000000;
			aux_board >>= piecesPosition[i];

			int generalCounter=0;

			Board tempBoard;
			memcpy(&tempBoard,actualBoard, sizeof(Board));

			switch (pieceType) {
			case King:
				moveKing(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			case Queens:
				moveQueen(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			case Rooks:
				moveRook(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			case Bishops:
				moveBishop(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			case Knights:
				moveKnight(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			case Pawns:
				movePawn(&aux_board, col, row, &generalCounter, &tempBoard);
				break;
			default:
				logChess(WARN, "Incorrect piece type.");
				break;
			}

			int j;
			for(j=0; j<MAX_NUM_MOVES; j++)
			{
				if((tempBoard.boardState[j].killedTypePiece > -1) && (tempBoard.boardState[j].killedTypePiece < 6))
				{
					menacedValue+=menacedHeuristicValue[tempBoard.boardState[j].killedTypePiece];
				}
				else
					break;
			}
		}
	}

	return menacedValue;

}
void AI::allPossibleMoves(boardState* nodeOrig, possibleMovesStruct* possibleMoves, int playerid){
	vector<fakePiece> piecesOnBoard;
	findPiecesOnBoard(nodeOrig, piecesOnBoard); // Creates a vector of all pieces on the board with their file rank type and owner
	
	if (piecesOnBoard.size() >= 1){
		for (int pos = 0; pos < piecesOnBoard.size(); pos++){
			bool myPiece = false;
			bool skip = false;
			fakePiece piecetoMove;

			//Check if creating a new possibleMovesStruct and checking it's size returns 0 
			while (!myPiece && !skip){
				if (pos < piecesOnBoard.size()){
					piecetoMove = piecesOnBoard[pos];
				}
				else{
					if (possibleMoves->availiableMoves.size() == 0){
						return;
					}
					else if (pos >= possibleMoves->availiableMoves.size()){
						skip = true;
						return;
					}
				}
				if (piecetoMove.owner() == playerid){
					myPiece = true;
				}
				else{
					skip = true;
				}
			}
			if (!skip){
				int typeofPiece = piecetoMove.type();
				std::pair<int, int>* position = new std::pair<int, int>;
				position->first = piecetoMove.file();
				position->second = piecetoMove.rank();

				switch (piecetoMove.type()) {
				case 'K':
					moveKing(possibleMoves, position, playerid, nodeOrig);
					break;
				case 'Q':
					moveQueen(possibleMoves, position, playerid, nodeOrig);
					break;
				case 'B':
					moveBishop(possibleMoves, position, playerid, nodeOrig);
					break;
				case 'N':
					moveKnight(possibleMoves, position, playerid, nodeOrig);
					break;
				case 'R':
					moveRook(possibleMoves, position, playerid, nodeOrig);
					break;
				case 'P':
					movePawn(possibleMoves, position, playerid, nodeOrig);
					break;
				default: 
					break;
				}
				delete position;
			}
		}

	}
	return;
}
int findMoves(char pieceID,int currentPosition[2],char board[12][12],int moveList[1000][6])
{
    //Returns which piece is occupying a given square and all the possible moves for that piece

    switch(pieceID)
    {
    case 'o':
        break;
    case 'x':
        break;
    case 'P':
        printf("This is a white pawn, and these are it's possible moves: \n");
        movePawn(currentPosition,board,moveList);
        break;
    case 'p':
        printf("This is a black pawn and these are it's possible moves: \n");
        movePawn(currentPosition,board,moveList);
        break;
    case 'R':
        printf("This is a white rook and these are it's possible moves: \n");
        moveRook(currentPosition,board,moveList);
        break;
    case 'r':
        printf("This is a black rook and these are it's possible moves: \n");
        moveRook(currentPosition,board,moveList);
        break;
    case 'N':
        printf("This is a white knight and these are it's possible moves: \n");
        moveKnight(currentPosition,board);
        break;
    case 'n':
        printf("This is a black knight and these are it's possible moves: \n");
        moveKnight(currentPosition,board);
        break;
    case 'B':
        printf("This is a white bishop and these are it's possible moves: \n");
        moveBishop(currentPosition,board,moveList);
        break;
    case 'b':
        printf("This is a black bishop and these are it's possible moves: \n");
        moveBishop(currentPosition,board,moveList);
        break;
    case 'Q':
        printf("This is a white queen and these are it's possible moves: \n");
        moveQueen(currentPosition,board);
        break;
    case 'q':
        printf("This is a black queen and these are it's possible moves: \n");
        moveQueen(currentPosition,board);
        break;
    case 'A':
        printf("This is a white king and these are it's possible moves: \n");
        moveKing(currentPosition,board);
        break;
    case 'a':
        printf("This is a black king and these are it's possible moves: \n");
        moveKing(currentPosition,board);
        break;
    }
    return pieceID;
}