【问题标题】:C++ Tic Tac Toe Program OptimizationC++井字游戏程序优化
【发布时间】:2017-08-28 22:27:04
【问题描述】:

我对 C++ 还是很陌生。

我刚刚完成了我的第一个“真正的”程序。

这是一款井字游戏。

有人愿意阅读我的代码并告诉我可以在哪里优化吗?

#include <iostream>

using namespace std;

    /**< Board variables */
    string a1 = "_";    string a2 = "_";    string a3 = "_";
    string b1 = "_";    string b2 = "_";    string b3 = "_";
    string c1 = "_";    string c2 = "_";    string c3 = "_";

    string showboard (){
        cout << '\t' << ""<< '\t' << "1" << '\t' << "2" << '\t' << "3" << "\n";
        cout << '\t' << "a"<< '\t' << a1 << '\t' << a2 << '\t' << a3 << "\n";
        cout << '\t' << "b"<< '\t' << b1 << '\t' << b2 << '\t' << b3 << "\n";
        cout << '\t' << "c"<< '\t' << c1 << '\t' << c2 << '\t' << c3 << "\n\n";
        return "";
    };

    /**< Turn */
    int turn;
    /**< useless variable */
    int x =1;
    /**< "tiles" for example "X" or "O" */
    string boardvalue;

    /**< Checks if Turn is valid/legal */
    string turnvalidation (string boardvalue){
            if(turn==1 && a1!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==2 && a2!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==3 && a3!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==4 && b1!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==5 && b2!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==6 && b3!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==7 && c1!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==8 && c2!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==9 && c3!="_"){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else if(turn==10){
                cout << "Invalid option. Choose again! \n\n";
                x=1;
            } else {

                x=0;
                switch (turn){

                    case 1: a1=boardvalue; break;
                    case 2: a2=boardvalue; break;
                    case 3: a3=boardvalue; break;
                    case 4: b1=boardvalue; break;
                    case 5: b2=boardvalue; break;
                    case 6: b3=boardvalue; break;
                    case 7: c1=boardvalue; break;
                    case 8: c2=boardvalue; break;
                    case 9: c3=boardvalue; break;
                }
            }
            return"";
    };

    /**< Checks if the game is won */
    int wincheck(string boardvalue){
    // Rows
    if(a1==boardvalue && a2==boardvalue && a3==boardvalue){return 0;}else
    if(b1==boardvalue && b2==boardvalue && b3==boardvalue){return 0;}else
    if(c1==boardvalue && c2==boardvalue && c3==boardvalue){return 0;}else

    // Columns
    if(a1==boardvalue && b1==boardvalue && c1==boardvalue){return 0;}else
    if(a2==boardvalue && b2==boardvalue && c2==boardvalue){return 0;}else
    if(a3==boardvalue && b3==boardvalue && c3==boardvalue){return 0;}else

    // Diagonals
    if(c1==boardvalue && b2==boardvalue && a3==boardvalue){return 0;}else
    if(a1==boardvalue && b2==boardvalue && c3==boardvalue){return 0;}else
    {return 1;};
    }

int main()
{
    string player1;
    string player2;

    cout << "Player 1 enter your name \n";
    cin >> player1;

    cout << "\nPlayer 2 enter your name \n";
    cin >> player2;
    cout << endl;

    cout << "Input example: a1 [enter]\n\n";

    string turni; /**< Converts Input in switch value! */

    while(1){ /**< Play again */
            string a1 = "_";    string a2 = "_";    string a3 = "_";
            string b1 = "_";    string b2 = "_";    string b3 = "_";
            string c1 = "_";    string c2 = "_";    string c3 = "_";
        while(1){

            /**< Player 1s turn */
            x=1;
            while(1){

                cout << player1 << " place your X \n\n";
                showboard();
                cin >> turni;

                if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;};

                /**< Checks if Turn is valid/legal */

                turnvalidation("X");
                break;
            }

            /**< Win Check */

            if(wincheck("X") == 0){

                showboard();
                cout << "Congratulations " << player1 << " you won!";
                break;
            };

            /**< Player 1s turn is over */


            /**< Player 2s turn */
            x=1;
            while(1){

                cout << player2 << " place your 0 \n\n";
                showboard();
                cin >> turni;

                if(turni=="a1"){turn=1;} else if(turni=="a2"){turn=2;} else if(turni=="a3"){turn=3;} else if(turni=="b1"){turn=4;} else if(turni=="b2"){turn=5;} else if(turni=="b3"){turn=6;} else if(turni=="c1"){turn=7;} else if(turni=="c2"){turn=8;} else if(turni=="c3"){turn=9;} else{turn=10;};

                /**< Checks if Turn is valid/legal */

                turnvalidation("O");
                break;
            }

            /**< Win Check */

            if(wincheck("O")==0){

                showboard();
                cout << "Congratulations " << player2 << " you won!";
                break;
            };

            /**< Player 1s turn is over */
        }

            /**< Play again and clear board */
        cout << "\n\nIf you want to play again type ""1""! \n";
        cin >> x;

        if(x==1){
        a1 = "_";     a2 = "_";     a3 = "_";
        b1 = "_";     b2 = "_";     b3 = "_";
        c1 = "_";     c2 = "_";     c3 = "_";
        } else {break;}
    }
}

游戏本身可以运行。

但是

在这个部分有一个错误:

        cout << "\n\nIf you want to play again type ""1""! \n";
        cin >> x;

        if(x==1){
        a1 = "_";     a2 = "_";     a3 = "_";
        b1 = "_";     b2 = "_";     b3 = "_";
        c1 = "_";     c2 = "_";     c3 = "_";
        } else {break;} 

获胜即可进入此部分。

如果您输入“1”,游戏将重新开始并应清除棋盘。

所有其他输入都会关闭程序。

游戏将重新开始,但棋盘不会清除。

【问题讨论】:

  • 如果您的代码有效并且您只是在寻找同行评审,您应该改为在Code Review 上发帖。本网站是针对您遇到的问题的问题,而不是您能帮我看看这个吗? 问题。
  • 您声明了 9 个变量,而不是某种数组或结构,这会迫使您的代码在整个过程中有大量重复。当您的代码开始看起来像是全部被复制/粘贴时,这表明您做错了。
  • 你很幸运井字游戏只有 9 个位置。学习使用数组。
  • “你赢了就进入这个部分。”呸。学习经典。 "The only winning move is not to play."

标签: c++ optimization


【解决方案1】:

你的错误在这里

while(1){ /**< Play again */
            string a1 = "_";    string a2 = "_";    string a3 = "_";
            string b1 = "_";    string b2 = "_";    string b3 = "_";
            string c1 = "_";    string c2 = "_";    string c3 = "_";

当你已经将它们声明为全局时,你不应该再次声明它们。

你应该把这部分改成这个。因此,您的变量值将随处更新。

while (1){ /**< Play again */
         a1 = "_";    a2 = "_";     a3 = "_";
         b1 = "_";    b2 = "_";     b3 = "_";
         c1 = "_";     c2 = "_";    c3 = "_";

【讨论】:

    【解决方案2】:

    Mohammad Tayyab 的回答对于修复错误是正确的。给他胜利,但如果以下内容也有帮助,那么投票会很好。您在游戏开始时设置棋盘,您不需要在任何其他时间设置它。将原来的语句改为:

    /* Board variables */
    string a1, a2, a3, b1, b2, b3, c1, c2, c3;
    

    再次播放代码为:

    /**< Play again and clear board */
    cout << "\n\nIf you want to play again type ""1""! \n";
    cin >> x;
    
    if (x != 1) 
    { 
        break; 
    }
    

    此外,您最好为您的电路板使用矢量/数组,例如在一个简单的程序中完全摆脱 a1 - c3 并使用:

    int board[9]; // a1 now = board[0], a2 now = board[1] and so on
    

    您现在可以大大简化“turnvalidation”功能。

    另外你在两个玩家的代码中都重复了这个代码:

    if (turni == "a1") { turn = 1; }
    else if (turni == "a2") { turn = 2; }
    else if (turni == "a3") { turn = 3; }
    else if (turni == "b1") { turn = 4; }
    else if (turni == "b2") { turn = 5; }
    else if (turni == "b3") { turn = 6; }
    else if (turni == "c1") { turn = 7; }
    else if (turni == "c2") { turn = 8; }
    else if (turni == "c3") { turn = 9; }
    else { turn = 10; };
    

    使用 board[9] 数组并将该代码组合到验证函数中(也将名称更改为“movevalidation”),您可以这样做:

    bool movevalidation(string move_string, string player_symbol)
    {
        string moves[9] = { "a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3" };
        int move_int = -1; // -1 means invalid
        for (int i = 0; i < 9; ++i)
        {
            if (move_string == moves[i])
                move_int = i;
        }
    
        if (moves < 0 || board[move_int] != "_")
        {
            cout << "Invalid option. Choose again! \n\n";
            return false;
        }
    
        board[move_int] = player_symbol;
        return true;
    }
    

    玩家-选择-移动循环变成了这样:

            while (1) {
    
                cout << player1 << " place your X \n\n";
                showboard();
                cin >> turni;
    
                if(movevalidation(turni)) 
                    break;
            }
    

    对于两个玩家。使用数组,您还需要将 clear-board 代码更改为循环:

    while (1) { /**< Play again */
        for(int i = 0; i < 9 ++i)
            board[i] = "_";
    

    您必须更改您的 wincheck 以将名称替换为数组名称。您可以简单地将 a1 替换为 board[0],将 a2 替换为 board[1] 等等。

    【讨论】:

    • 伙计,这太不可思议了。非常感谢详细的描述。我用一本书自学了 C++,天堂也学习了数组,所以我使用了我知道的工具。如果我的声望达到 15,你会看到我的支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多