C Tic Tac Toe程序-在Main中调用函数时崩溃

1wnzp6jl  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(86)

“大家好,我很抱歉,我是一个新手。我现在正在学习C,其中一个初学者练习是写一个井字游戏程序。我已经为此写了几个函数,我试图一起工作,目前我遇到了一个点,当我试图在主函数中进行一些测试时,程序崩溃。我已经发布了下面的代码。对不起,这是一个烂摊子我确定。现在大部分都被注解掉了。

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

//GLOBAL WORKSPACE//
char playerOneMarkX = 'X'; //output of 'X' for player 1 marking board
char playerTwoMarkO = 'O';
char playerOneInput; //input from scanf for player 1
char playerTwoInput;
char boardSpaces[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' } ;//define the array that will house the spaces of the board
bool playerOneWins;

    //declare function to draw board
    //it will pull directly from the values stored in the array boardSpaces
    //it is marked as void type and void for arguments because it will not require any arguments to be invoked in main
    void drawBoard(void)
    {
    system("clear"); //This is the syntax to clear the screen so the board can be re-drawn in between changes
    printf("\n\n\tTic Tac Toe\n\nPlayer 1 (X)  -  Player 2 (O)\n\n");

    printf("       |       |       \n");
    printf("   %c   |   %c   |   %c   \n", boardSpaces[1], boardSpaces[2] , boardSpaces[3]);
    printf("_______|_______|_______\n");
    printf("       |       |       \n");
    printf("   %c   |   %c   |   %c   \n", boardSpaces[4], boardSpaces[5], boardSpaces[6]);
    printf("_______|_______|_______\n");
    printf("       |       |       \n");
    printf("   %c   |   %c   |   %c   \n", boardSpaces[7], boardSpaces[8], boardSpaces[9]);
    printf("       |       |       \n");

    //return 0;
    }

    //declare function to mark the board based on player input
    //X for player 1, O for player 2
    //the for loop iterates through the elements of the boardSpaces array
    //if the player input is the same as one of the elements it will replace the value stored with an X
    void p1markBoard(void)
    {
        char loop; //variable to loop through array

        for (loop = 0; loop <=10; loop++ ){
                if ( boardSpaces[loop] == playerOneInput){
                boardSpaces[loop] = playerOneMarkX;
                }
                //printf("\n%c", boardSpaces[loop]);//FORTESTINGONLY - prints the array iteration
        }
                //printf("\nValue of element 5: %c", spaces[5]);//FORTESTINGONLY
                                                                //testing the value of element 5 outside of the loop
                                                                // so if the player input is 5, this will report an X
    }

    /*
    void checkForWin(void)
    {
        if (boardSpaces[1]&&boardSpaces[2]&&boardSpaces[3]=='X')
            playerOneWins = true;
            printf("\nPlayer One Wins!!!");

    }
    */
    void p1Turn (void)
    {
        printf("\nEnter a value between 1-9 to choose the corresponding space:");//output to terminal to request input from user to select a space
        scanf("%c", &playerOneInput);//stores entered value from user into variable playerOneInput
        //printf("\nYou entered: %c", playerOneInput);//prints the playerAnswer. FORTESTINGONLY

        return playerOneInput;
    }

int main()
{

    /*
    int movement;
    for( movement = 0 ; playerOneWins = true ; movement++){

        drawBoard();

        printf("\nEnter a value between 1-9 to choose the corresponding space:");//output to terminal to request input from user to select a space
        scanf("%c", &playerOneInput);//stores entered value from user into variable playerOneInput
        //printf("\nYou entered: %c", playerOneInput);//prints the playerAnswer. FORTESTINGONLY

        markBoard();

        //checkForWin();

        system("clear");
    }
*/

drawBoard();
p1Turn();
p1markBoard();
drawBoard();
p1Turn();
p1markBoard();
drawBoard();

    //return 0;
}`

字符串
我现在只是试着在main中测试函数,让它们在进一步开发之前流动。现在程序在第二次调用drawBoard函数后退出。我试图弄清楚为什么会发生这种情况,这样我就可以继续测试函数,然后为玩家2开发函数,等等。

8dtrkrch

8dtrkrch1#

如果你第三次调用p1Turn(),你会看到它工作了。这种行为可能是由于scanf函数中%c说明符的工作方式:它收集空格字符,包括Enter键,所以它在第二次调用时没有像预期的那样工作。
我认为最简单的解决方案是在%c之前添加一个空格,如下所示:

scanf(" %c", &playerOneInput);

字符串
这个问题在这里也有很好的描述:Using scanf() function two times: works in one case but not in other case
此外,p1Turn()函数被声明为void函数,因此它不应该返回值。

相关问题