mardi 16 février 2016

Random array selection

I'm making a tic tac toe(without matrix), and I have a question about this.

Say that my input here was "5":

printf(" 1 | 2 | 3 \n");
printf("___|___|___\n");
printf(" 4 | 5 | 6 \n");
printf("___|___|___\n");
printf(" 7 | 8 | 9 \n");
printf("   |   |   \n");
printf("\n");

I want that the A.I. select a random box, excluding my "5" input. I'm doing the random thing with:

srand ( time(NULL) );
int randIndex = rand() % 8; 
    if (movement == 5 && i == 0){
            xslot[randIndex] = 'O';
            steps = 1;
    } 

I don't know how to exclude the "5" from this random selection.

This is my array:

char xslot[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

Thank you

Edit: complete code:

main()
{
    int movimento,passos;
    bool passo1=false,passo2=false,passo3=false,passo4=false,passo5=false;
    char oslot[9] = "O";    
    char xslot[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    srand ( time(NULL) );

    printf("\n\n");

    printf(" 1 | 2 | 3 \n");
    printf("___|___|___\n");
    printf(" 4 | 5 | 6 \n");
    printf("___|___|___\n");
    printf(" 7 | 8 | 9 \n");
    printf("   |   |   \n");
    printf("\n");

    printf("Digite o numero do seu movimento:");
    for(int i =0;i<5;i++){
        scanf("%i",&movimento);

            if (movimento == 1){
                xslot[0] = 'X';
            } else if (movimento == 2){
                xslot[1] = 'X';
            } else if (movimento == 3){
                xslot[2] = 'X';
            } else if (movimento == 4){
                xslot[3] = 'X';
            } else if (movimento == 5){
                xslot[4] = 'X';
            } else if (movimento == 6){
                xslot[5] = 'X';
            } else if (movimento == 7){
                xslot[6] = 'X';
            } else if (movimento == 8){
                xslot[7] = 'X';
            } else if (movimento == 9){
                xslot[8] = 'X';
            }   

            //INTELIGENCIA ARTIFICIAL

            int randIndex = rand() % 8;

            if (movimento == 5 && i == 0){
                    xslot[randIndex] = 'O';
                    passos = 1;
            } 




Aucun commentaire:

Enregistrer un commentaire