简体   繁体   English

c中的随机井字游戏

[英]random move tic tac toe in c

alright, so I'm really new at coding in C and I have a LOT of copy and paste in my code. 好吧,所以我真的很熟悉C语言编码,并且在我的代码中有很多复制和粘贴操作。 (it's my final project, so I really don't have time to optimize and shorten until the end if time permits). (这是我的最后一个项目,因此,如果时间允许,我真的没有时间进行优化和缩短直到结束)。 So anyway, I'm gonna avoid posting all my code for that reason. 因此,无论如何,出于这个原因,我将避免发布我的所有代码。 I'm only gonna post the relevant part which is what I'm trying to do. 我只想发布相关部分,这就是我想要做的。

I need the game to pick a random number from 1-9 and then use that number to select a move on the board. 我需要游戏从1-9中选择一个随机数,然后使用该数字选择棋盘上的棋步。 I added a printf in this part of the code to make sure a number is being picked, but then when i go to scanf, the game just gives a blinking prompt and does not continue. 我在代码的这一部分中添加了printf,以确保选择了一个数字,但是当我进入scanf时,游戏只会给出闪烁的提示,并且不会继续。

Does anyone see what is wrong or do i need to post any other section of code? 有人看到错了吗,还是我需要发布代码的其他任何部分? Any help will be really appreciated!! 任何帮助将不胜感激! I have only included the 1 player portion of the game and then when the computer is supposed to move. 我只包括了游戏的1个玩家部分,然后才包括应该移动计算机的部分。 I removed the win checks and the re printing of the board. 我删除了赢钱支票并重新打印了棋盘。 Let me know if you guys need to see anything else i just didn't want to have a huge block of code. 让我知道你们是否需要查看其他内容,我只是不想拥有大量代码。

if (player == 1)
    {
        for(k=0;k<9;k++) // 9 moves max.
        {
        printf("\n\n"); // print board again.
        printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

        do
        {
            printf("Player 1, where would you like to move? Enter 1-9\n");
            scanf("%d", &move);

            if (move == 1)
                board[0][0] = '1';
            if (move == 2)
                board[0][1] = '1';
            if (move == 3)
                board[0][2] = '1';
            if (move == 4)
                board[1][0] = '1';
            if (move == 5)
                board[1][1] = '1';
            if (move == 6)
                board[1][2] = '1';
            if (move == 7)
                board[2][0] = '1';
            if (move == 8)
                board[2][1] = '1';
            if (move == 9)
                board[2][2] = '1';

        }while(move>9 && move <1);


        do
        {
            printf("Computer moves...");


            move = rand() % 9 + 1;
            printf("%d", move);
            scanf("%d", &move);


            if (move == 1)
                board[0][0] = '2';
            if (move == 2)
                board[0][1] = '2';
            if (move == 3)
                board[0][2] = '2';
            if (move == 4)
                board[1][0] = '2';
            if (move == 5)
                board[1][1] = '2';
            if (move == 6)
                board[1][2] = '2';
            if (move == 7)
                board[2][0] = '2';
            if (move == 8)
                board[2][1] = '2';
            if (move == 9)
                board[2][2] = '2';


        }while(move>9 && move <1);



    }

It appears that you need to reamove scanf() from your code, scanf() blocks until input is given to the program, it returns the number of items that matched the format string if any, by the input. 看来您需要从代码中删除scanf()scanf()块直到输入给程序为止,它通过输入返回与格式字符串(如果有)匹配的项数。

If you are generating the number with rand() then you don't need any input at all. 如果使用rand()生成数字,则根本不需要任何输入。

Please read this , to understand what scanf() is for. 请阅读 ,明白了什么scanf()是。

Try this 尝试这个

move = rand () % 9;

to avoid this. 避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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