简体   繁体   中英

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. (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. 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.

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. 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.

If you are generating the number with rand() then you don't need any input at all.

Please read this , to understand what scanf() is for.

Try this

move = rand () % 9;

to avoid this.

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