【问题标题】:Re-assign value to variable if rand() repeats a number如果 rand() 重复一个数字,则将值重新分配给变量
【发布时间】:2019-06-21 05:16:00
【问题描述】:

我正在制作一个用户与计算机竞争的井字游戏。每当人们在 1 到 9 之间选择一个点时,计算机也需要选择一个。为此,我正在使用 rand()。但是,如果该位置已经被占用,我需要计算机计算一个新位置。我尝试过使用 while 和 do-while 循环,但是当我应用它们时,cmd 停止工作并且不允许我继续游戏。

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

typedef struct symbol{
    int marcado;
    char simbolo;
} SPOT;

SPOT casilla1 = {0,'1'};
SPOT casilla2 = {0,'2'};
SPOT casilla3 = {0,'3'};

void table();
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{

        do{
            board();
            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(casilla1.marcado == 1){
                            printf("\noccupied\n");
                        }

                        else if(casilla1.marcado == 0){
                            casilla1.marcado = 1;
                            casilla1.simbolo = 'X';

                            AI();
                        }
                    }
            break;

            case '2': if(choice == '2'){

                        system("cls");

                        if(casilla2.marcado == 1){
                            printf("\noccupied\n");
                        }

                        else if(casilla2.marcado == 0){
                            casilla2.marcado = 1;
                            casilla2.simbolo = 'X';

                            AI();
                        }   
                    }   
            break;

            case '3': if(choice == '3'){

                        system("cls");

                        if(casilla3.marcado == 1){
                            printf("\noccupied");
                        }

                        else if(casilla3.marcado == 0){
                            casilla3.marcado = 1;
                            casilla3.simbolo = 'X';

                            AI();
                        }   
                    }   
            break;  

    }while(Check() != 0 && Check() != 1);
}

这是计算机的功能。我在“else if”语句中遇到了麻烦,因为我不知道该放什么。

void AI(){

    int random;

    srand(time(NULL));

    random = rand() % 3 + 1;

    if (random == 1){
        if(casilla1.marcado == 0){      
            casilla1.simbolo = 'O';
            casilla1.marcado = 1;
        }

        else if(casilla1.marcado == 1){
            random = rand() % 3 + 1
        }
    }

        if (random == 2){
            if(casilla2.marcado == 0){      
                casilla2.simbolo = 'O';
                casilla2.marcado = 1;
            }

            else if(casilla2.marcado == 1){
                random = rand() % 3 + 1;
            }
        }

        if (random == 3){
            if(casilla3.marcado == 0){      
                casilla3.simbolo = 'O';
                casilla3.marcado = 1;
            }

            else if(casilla3.marcado == 1){
                random = rand() % 3 + 1;
            }
        }   
}

正如我之前所说,我尝试将整个 AI() 放入不同类型的循环中,仅将 rand() 放入其中等等,但仍然无法使其工作。

【问题讨论】:

    标签: c


    【解决方案1】:

    首先,更好地选择数据结构。而不是:

    SPOT casilla1 = {0,'1'};
    SPOT casilla2 = {0,'2'};
    SPOT casilla3 = {0,'3'};
    

    使用

    SPOT casilla[3] = { {0,'1'}, {0,'2'}, {0,'3'} };
    

    因此,不再需要switch 构造。而不是:

    if(casilla1.marcado == 0){
    if(casilla2.marcado == 0){
    if(casilla3.marcado == 0){
    

    使用:

    if(casilla[random-1].marcado == 0){
    

    此人在 1 到 9 之间选择一个位置

    random = rand() % 9 + 1;
    

    您只有 3 个casilla。其他 6 个在哪里?


    我尝试过使用 while 和 do-while 循环

    AI() 中没有循环。也许您可以向我们展示一个带有循环的代码?


    printf("\n\nChoose a spot: ");
    fflush(stdin);
    

    你可能想fflush()stdout

    【讨论】:

    • 上面写的是9,因为我修改了大部分代码,去掉了6个点,所以这里不会那么长,是个错误。我会在凌晨 2 点修改其余部分,我现在无法继续思考代码。
    • 正如您在我的示例中看到的,您有一个structs 数组。我没有触及那部分。再次阅读答案,您甚至会发现如何访问数组元素的结构成员;)
    • 删除 6 个元素不会让你的程序变得更好。请把它们放回去:)
    • 对不起,我的意思是修改,所以它不会像实际代码那么长! (因为我在stackoverflow和一般编码方面都很陌生)。我看到你保留了结构,我明天也将更改它以使代码更短且更易于遵循,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2011-08-27
    • 2019-06-29
    • 2021-04-07
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多