0

Cで作成しているTic-Tac-Toeゲームで問題が発生しています。

ネコゲームのテキストの計算と表示に役立つintcats(ネコゲームはネコゲーム)という変数があります。

私はゲームを結びつける方法を理解するためにほぼ一週間中試みました。あなたたちが私を正しい方向に向けることができたとしても、それは甘いでしょう!

-フレンディン

(PS最初の空のifステートメントは、プレーヤー対コンピューターゲーム用です。ゲームを結び付ける方法がわかれば、その部分を問題なくコーディングできます。ありがとうございました!)

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

void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);

int main(void)
{
    char boardPieces[3][3] = {'\0'};
    int cats = 0;  //A tie game in Tic-Tac-Toe is called a Cats Game.
    int choice;

    printf("Would you like to play against the computer or another player?\n");
    printf("Press 1 to play against another player.\n");
    printf("Press 2 to play against the computer.\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        for(int i = 0; i < 5; ++i)
        {
            printTheBoard(boardPieces);
            playerOne(boardPieces, cats);
            checkBoard(boardPieces, cats);

            printTheBoard(boardPieces);
            playerTwo(boardPieces, cats);
            checkBoard(boardPieces, cats);
        }   
    }

    if(choice == 2)
    {

    }

    return 0;
}

void printTheBoard(char boardPieces[3][3])
{
    printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}

int playerOne(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player O. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'O';
        ++cats;
        return cats;
    }
}

int playerTwo(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player X. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'X';
        ++cats;
        return cats;
    }
}

void checkBoard(char boardPieces[3][3], int cats)
{
    if(boardPieces[0][0] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[0][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'O')
    {
        if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[2][1] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[1][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'O')
    {
        if(boardPieces[2][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }




    if(boardPieces[0][0] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[0][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'X')
    {
        if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[2][1] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[1][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'X')
    {
        if(boardPieces[2][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player O has won the game!\n");
            }
        }
    }



    else if(cats == 9)
    {
        printf("There was a tie game!\n");
    }
}
4

2 に答える 2

1

関数playerOne()では、関数の引数として宣言されplayerTwo()た変数をインクリメントします。の開始時に宣言しcatsた変数に値を格納する必要があります。例:catsmain

for(int i = 0; i < 5; ++i)
{
    printTheBoard(boardPieces);
    cats = playerOne(boardPieces, cats);
    checkBoard(boardPieces, cats);

    printTheBoard(boardPieces);
    cats = playerTwo(boardPieces, cats);
    checkBoard(boardPieces, cats);
}

詳細な説明は次のとおりです。次のコードを検討してください。

void func(int var)
{
    var = 2;
}

int main(void) 
{
    int var;
    var = 1;
    func(var);
}

ここに名前が付いた 2 つの異なる変数があり var ます。1 つは の先頭で宣言され、mainそのスコープまたは有効期間はmain関数内にあります。

もう 1 つは への引数として宣言され、funcそのスコープはfunc関数に対してローカルです。これは、関数が戻るときに変数とその値が存在しなくなることを意味します。 func

funcここで、関数が戻った後に のローカル変数の値を使用したい場合は、次のことができます。

  • によって変更された値を返しますfunc:

    int func(int var)
    {
        var++;
        return var;
    }
    ...
    new_var = func(old_var);
    
  • func代わりに、ポインターを介してその場で変数を変更します。

    void func(int *var)
    {
        (*var)++;
    }
    ...
    func(&var);
    

また、else if最後に引き分けのメッセージを出力する条件checkBoard()は、現在のコードでは決して実行されません。

...
else if(boardPieces[2][0] == 'X')
{
    if(boardPieces[2][1] == 'X')
    {
        if(boardPieces[2][2] == 'X')
        {
            printf("Player O has won the game!\n");
        }
    }
}

else if(cats == 9)
{
    printf("There was a tie game!\n");
}

checkBoard()ゲームの結果を出力した後、関数から戻ることを検討してください。ゲームが終了したかどうかを示す値を返すことを検討してください。これにより、これが発生したときにプログラムを終了できます。

于 2013-02-23T00:03:25.700 に答える