简体   繁体   中英

making a random move in tic tac toe -in C

I am doing a tic tac toe game in which the user competes against the computer. When the user chooses a spot, the computer should also make a move. However, I can't seem to get that to work. Whenever the user makes a move, it instantly asks me to make another one instead of filling one of the spots with the computer symbol (O)

I did the computer part in a different function from the user's (AI() and User()) and tried calling it inside each "else if" statement of the switch, however, that only keeps the game blinking and doesn't allow me to continue.

EDIT: I also tried to, instead of making it a function, do the whole AI() process inside "else if" statements, but it only seemed to work when the user chose 1, not for the rest of the options.

EDIT #2: I changed (k = 1) to (k == 1) in the code.

To make the following code easier to read I made it 1x3 instead of 3x3:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct symbol{
    int marked;
    char symbol;
} SPOT;

SPOT spot1 = {0,'1'};
SPOT spot2 = {0,'2'};
SPOT spot3 = {0,'3'};

char symbol = 'X';
char compu = 'O';

void table();
int Check();
void User();
void AI();

int main(){
    system("cls");
    User();
    AI();
    Check();
    return 0;
}


void table(){
    printf("\n        %c   |   %c   |   %c ",spot1.symbol,spot2.symbol,spot3.symbol);
}


void User(){    
    char choice;

    do{
        loop++;
        do{
            table();
            printf("\n\nChoose a spot: ");
            fflush(stdin);
            scanf("%c",&choice);
        } while(choice < '1' || choice > '3');

        switch(choice){
            case '1': 
                if(choice == '1'){
                        system("cls");
                        if(spot1.marked == 1){
                            printf("\nThat spot is taken\n");
                        }
                        else if(spot1.marked == 0){
                            spot1.marked = 1;
                            spot1.symbol = symbol;
                        }
                }
                break;

            case '2': 
                if(choice == '2'){
                        system("cls");
                        if(spot2.marked == 1){
                            printf("\nThat spot is taken\n");
                        }
                        else if(spot2.marked == 0){
                            spot2.marked = 1;
                            spot2.symbol = symbol;
                        }   
                }   
                break;

            case '3': 
                if(choice == '3'){
                        system("cls");
                        if(spot3.marked == 1){
                            printf("\nThat spot is taken\n");
                        }
                        else if(spot3.marked == 0){
                            spot3.marked = 1;
                            spot3.symbol = symbol;
                        }   
                }   
                break;
        }while(Check() != 0 && Check() != 1);
    }

void AI(){
    int random,k;
    srand(time(NULL));
    do{
        random = rand() % 4;
        k = 0;
        if(spot1.symbol == symbol || spot2.symbol == symbol || spot3.symbol == symbol){
            k = 1;
        }
    }while(k == 1);

    switch(random){
        case '1': 
            if (random == 1){
                spot1.symbol = compu;
                spot1.marked = 1;
            }
            break;

        case '2': 
            if (random == 2){
                spot2.symbol = compu;
                spot2.marked = 1;
            }
            break;

        case '3': 
            if (random == 3){
                spot3.symbol = compu;
                spot3.marked = 1;
            }
            break;
    }
}

int Check() {
    if(spot1.marked == spot2.marked && spot2.marked == spot3.marked){
        if(spot1.symbol == symbol){
            return 1;
            printf("You won!");
        }
        else if(spot1.symbol == compu){
            return 0;
            printf("You lost!");
        }
    }
    else {
        return 2;
    }
}

That's because each function in your main is called one after another. Your program is stuck in the User() call

Try changing your main to :

int main(){
    system("cls");
    do{
        User();
        AI();
    }while(Check() != 0 && Check() != 1);
    return 0;
}

Removing the do{...}while(Check() != 0 && Check() != 1); in the User function

and change while(k = 1); to while(k == 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM