c++ - 帮助将 C++ 代码转化为函数

标签 c++ arrays function

SigTerm 是这样说的: “闻起来像一个糟糕的代码。squareOne..squareSix 可能应该是数组。而不是 char 常量,应该使用枚举或整数......” 如果有人可以建议我必须使用的代码来让用户输入游戏的 X 和 O 的行和列,我该如何使用这个板代码来做到这一点。

void showboard(char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
                char &squareEight, char &squareNine)
{

    cout << squareOne << "|" << squareTwo << "|" << squareThree << endl
            << "-+-+-"<< endl
            << squareFour << "|" << squareFive << "|" << squareSix << endl
            << "-+-+-"<< endl
            << squareSeven << "|" << squareEight << "|" << squareNine << endl;

我遇到的问题是获取此代码并将其转换为函数。 下面列出的是我试图制作成函数的代码。在它之后的“检查板”功能是我试图将其上面的代码变成一个函数

  bGameOver     = false;
                bool bWinGame   = true;
                // Check for end of game conditions
                if (squareOne != '1') {
                    if (squareTwo == squareOne && squareThree == squareOne) {
                        bGameOver = true;
                    }
                    if (squareFour == squareOne && squareSeven == squareOne) {
                        bGameOver = true;
                    }
                }
                if (squareFive != '5') 
                {
                    if (squareOne == squareFive && squareNine == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareTwo == squareFive && squareEight == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareFour == squareFive && squareSix == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareThree == squareFive && squareSeven == squareFive) 
                    {
                        bGameOver = true;
                    }
                }
                if (squareNine != '9') 
                {
                    if (squareThree == squareNine && squareSix == squareNine) 
                    {
                        bGameOver = true;
                    }
                    if (squareSeven == squareNine && squareEight == squareNine) 
                    {
                        bGameOver = true;
                    }
                }
                /* Need to check the board full (no-win condition*/)
                if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
                    squareFour != '4' && squareFive != '5' && squareSix != '6' &&
                    squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
                {
                    bGameOver = true;
                    bWinGame = false;
                }

                system("cls");

                if (bGameOver) 
                {
                    if (bWinGame) 
                    {
                        cout << "Player " << currentPlayer << " wins!" << endl;
                    }
                    // Print ending board
                    cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
                    cout << "-+-+-"<< endl;
                    cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
                    cout << "-+-+-"<< endl;
                    cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;

                    cout << "Game Over!" << endl;
                    cout << "Play again (y/n)?" << endl;
                    char cPlayAgain;
                    cin >> cPlayAgain;

                    if (cPlayAgain == 'y') 
                    {
                        bGameOver = false;
                        /* Clear the board*/
                        squareOne = '1';
                        squareTwo = '2';
                        squareThree = '3';
                        squareFour = '4';
                        squareFive = '5';
                        squareSix = '6';
                        squareSeven = '7';
                        squareEight = '8';
                        squareNine = '9';
                    }
                    currentPlayer[100] = 1;
                } 

            } while (!bGameOver);



void checkboard(char** pCurrentPlayer, char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
                    char &squareEight, char &squareNine, char &playerMarker, bool bGameOver)
    {

            bGameOver       = false;
            bool bWinGame   = true;
            // Check for end of game conditions
            if (squareOne != '1') {
                if (squareTwo == squareOne && squareThree == squareOne) {
                    bGameOver = true;
                }
                if (squareFour == squareOne && squareSeven == squareOne) {
                    bGameOver = true;
                }
            }
            if (squareFive != '5') 
            {
                if (squareOne == squareFive && squareNine == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareTwo == squareFive && squareEight == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareFour == squareFive && squareSix == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareThree == squareFive && squareSeven == squareFive) 
                {
                    bGameOver = true;
                }
            }
            if (squareNine != '9') 
            {
                if (squareThree == squareNine && squareSix == squareNine) 
                {
                    bGameOver = true;
                }
                if (squareSeven == squareNine && squareEight == squareNine) 
                {
                    bGameOver = true;
                }
            }
            // Need to check the board full (no-win condition)
            if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
                squareFour != '4' && squareFive != '5' && squareSix != '6' &&
                squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
            {
                bGameOver = true;
                bWinGame = false;
            }

            system("cls");

            if (bGameOver) 
            {
                if (bWinGame) 
                {
                    cout << "Player " << pCurrentPlayer << " wins!" << endl;
                }
                // Print ending board
                cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
                cout << "-+-+-"<< endl;
                cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
                cout << "-+-+-"<< endl;
                cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;

                cout << "Game Over!" << endl;
                cout << "Play again (y/n)?" << endl;
                char cPlayAgain;
                cin >> cPlayAgain;

                if (cPlayAgain == 'y') 
                {
                    bGameOver = false;
                    // Clear the board
                    squareOne = '1';
                    squareTwo = '2';
                    squareThree = '3';
                    squareFour = '4';
                    squareFive = '5';
                    squareSix = '6';
                    squareSeven = '7';
                    squareEight = '8';
                    squareNine = '9';
                }
                pCurrentPlayer = "1";
            } 

    } 

最佳答案

PlayerMove() 中,您将 pCurrentPlayer 作为 char ** 传递,这是一个指向指针的指针。将其更改为 char * 然后它应该在函数内正确显示内容。您没有更改该函数中指针的值,因此它不需要额外的间接访问。

同时从相应的函数调用参数中删除运算符(&)的地址。

关于c++ - 帮助将 C++ 代码转化为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3368728/

相关文章:

c++ - 在 SciTE 中链接 C++

php - 对于MySQL查询中的每个结果,推送到数组(复杂)

c - 基于字符串比较的单词翻译程序——堆内存断言失败

function - Foundry Fusion Sheets 中的 TODAY() 函数

c++ - 为什么用户可以从私有(private)用户更改为公共(public)用户?

c++ - 了解 visual studio 2010 中的编译器选项差异和严格的 C++ 合规性

ios - 如何限制我的对象在 iOS 中只调用一次方法?

swift - 将静态方法的引用传递给函数

c++ - 如何获取 OCaml 链接器标志以与 C++ cmake 构建链接

python - 在 Python 和 numpy 中,如何用前一个元素替换数组中的缺失值? (蒙面阵?)