如何在C中打印函数的结果?

如何在C中打印函数的结果?,c,function,printing,C,Function,Printing,我目前正在开发一款互动的Tic-Tac-Toe游戏。我想这一切都正常工作了,我只是不知道如何让displayBoard功能打印出电路板的当前状态,以便用户知道哪些空间是开放的,哪些空间不是。如何使显示板功能打印到屏幕上 我的代码如下: #include <stdio.h> void initializeBoard(char board[][3]); void displayBoard(char board[][3]); void makeMove(char board[][3],

我目前正在开发一款互动的Tic-Tac-Toe游戏。我想这一切都正常工作了,我只是不知道如何让displayBoard功能打印出电路板的当前状态,以便用户知道哪些空间是开放的,哪些空间不是。如何使显示板功能打印到屏幕上

我的代码如下:

#include <stdio.h>

void initializeBoard(char board[][3]);
void displayBoard(char board[][3]);
void makeMove(char board[][3], int player);

int main(void)
{
    char board[3][3];
    int row;
    int column;
    int move = 1;
    int player = 1;

    initializeBoard(board);


    while (move > 10)
    {
        displayBoard(board);
        makeMove(board, player);
        move++;
        if (move == 2 || move == 4 || move == 6 || move == 8)
        {
            player = 2;
        }//close of if
        else
        {
            player = 1;
        }//close of else
    }

}//close of main function

void initializeBoard(char board[][3])
{
    int i = 0;
    int j = 0;

    while (i < 4)
    {
        while (j < 4)
        {
            board[i][j] = ' ';
            j++;
        }//close of while loop

        j = 0;
        i++;
    }//close of while loop

}//close of initializeBoard

void displayBoard(char board[][3])
{
    printf("   1    2    3");
    printf("1 [%c] [%c] [%c]", board[1][1],board[1][2],board[1][3]);
    printf("2 [%c] [%c] [%c]", board[2][1],board[2][2],board[2][3]);
    printf("3 [%c] [%c] [%c]", board[3][1],board[3][2],board[3][3]);

}//close of displayBoard

void makeMove(char board[][3], int player)
{
    int row;
    int column;

    printf("Enter the row and column you'd like to fill:");
    scanf("%d %d", &row, &column);
    if (player == 1)
    {
        board[row][column] = 'X';
    }//close of if
    else
    {
        board[row][column] = 'O';
    }//close of else

}//close of makeMove

你们写了很多错误的东西,也并没有调用任何函数来绘制主电路板。在这里,我有一个更好的游戏版本。你可以看到它,比较,使你的更好。它非常相似,它在C++中,但是如果这是一个问题,只需改变输入和输出行。附言:我不太记得C 祝你好运

#include<iostream.h>
#include<conio.h>

//to print digits on board and for checks

char num[10]={'0','1','2','3','4','5','6','7','8','9'};

void gameBoard() //function to print board
{
    cout<<"     |     |     "<<endl;
    cout<<"  "<<num[1]<<"  |  "<<num[2]<<"  |  "<<num[3]<<"  "<<endl;
    cout<<"_____|_____|_____"<<endl;
    cout<<"     |     |     "<<endl;
    cout<<"  "<<num[4]<<"  |  "<<num[5]<<"  |  "<<num[6]<<"  "<<endl;
    cout<<"_____|_____|_____"<<endl;
    cout<<"     |     |     "<<endl;
    cout<<"  "<<num[7]<<"  |  "<<num[8]<<"  |  "<<num[9]<<"  "<<endl;
    cout<<"     |     |     "<<endl;

}
int win()   //function to check if a player wins or game is draw,
        // returns 1 if someone won, 0 if draw and -1 is game is still going on
{
    if(num[1]==num[2] && num[2]==num[3])
        return 1;
    else if(num[4]==num[5] && num[5]==num[6])
        return 1;
    else if(num[7]==num[8] && num[8]==num[9])
        return 1;
    else if(num[1]==num[4] && num[4]==num[7])
        return 1;
    else if(num[2]==num[5] && num[5]==num[8])
        return 1;
    else if(num[3]==num[6] && num[6]==num[9])
        return 1;
    else if(num[1]==num[5] && num[5]==num[9])
        return 1;
    else if(num[3]==num[5] && num[5]==num[7])
        return 1;
    else if(num[1]!='1' && num[2]!='2' && num[3]!='3' && num[4]!='4' && num[5]!='5' && num[6]!='6' && num[7]!='7' && num[8]!='8' && num[9]!='9')
        return 0;
    else
        return -1;
}
void input()   //function for player input and to assign both players signs.
{
    int choice,w,player=1;
    char sym;
    do{
    clrscr();
    gameBoard();
    player=(player%2)? 1:2;  //to check whos turn
    cout<<"\nPlayer "<<player<< " ,Enter Your Choice: ";
    cin>>choice;

    sym = (player == 1)? 'X' : 'O';  //assigns X to player 1 and O to player 2

    if(choice==1&&num[1]=='1')
    num[1]=sym;
    else if(choice==2&&num[2]=='2')
    num[2]=sym;
    else if(choice==3&&num[3]=='3')
    num[3]=sym;
    else if(choice==4&&num[4]=='4')
    num[4]=sym;
    else if(choice==5&&num[5]=='5')
    num[5]=sym;
    else if(choice==6&&num[6]=='6')
    num[6]=sym;
    else if(choice==7&&num[7]=='7')
    num[7]=sym;
    else if(choice==8&&num[8]=='8')
    num[8]=sym;
    else if(choice==9&&num[9]=='9')
    num[9]=sym;
    else
    {
        cout<<"\n\nWrong Move, Please enter again:";
        cin.ignore();
        player--;     //Ignores the input and let player choose again.
        getch();
    }
    w=win();
    player++;
    }while(w==-1);
    clrscr();
    gameBoard();

    if(w==1)
    cout<<"\n Player "<<--player<<" wins!!";
    else
    cout<<"\n Game Draw";
    getch();
}

int main()
{
    clrscr();
    input();    return 0;
}

在C语言中,数组是基于零的。这意味着3元素数组包含元素[0]、[1]和[2]。您可能还有其他问题。当i<4{while j<4-><3Tip:With scanf%d%d,&row,&column;检查scanf返回值和row,column的值时,printfs中没有一个\n看起来是错误的,然后继续。