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

时间:2017-11-23 00:13:16

标签: c function printing

我目前正在开发一款为用户提供互动的Tic Tac Toe游戏。我想我已经完成了所有工作,我只是无法弄清楚如何让displayBoard()函数打印出电路板的当前状态,以便用户知道哪些空格是打开的,哪些空格不是。如何让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

1 个答案:

答案 0 :(得分:1)

你写错了很多东西,也没有调用任何函数在main()中画板。在这里,我有一个更好的游戏版本。你可以看到并比较并使你的更好。它非常相似,它在C ++中,但如果这是一个问题,只需更改输入和输出行。 PS:我不记得很多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;
}