簡體   English   中英

如何在C中打印函數的結果?

[英]How to print the result of a function in C?

我目前正在開發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

您寫錯了很多東西,也沒有在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM