C++;Tic-Tac-Toe程序优化 我对C++很陌生.

C++;Tic-Tac-Toe程序优化 我对C++很陌生.,c++,optimization,C++,Optimization,我刚刚完成了我的第一个“真正的”计划 这是一场井字游戏 有人愿意阅读我的代码并告诉我哪里可以优化一些东西吗 #include <iostream> using namespace std; /**< Board variables */ string a1 = "_"; string a2 = "_"; string a3 = "_"; string b1 = "_"; string b2 = "_"; string b3 =

我刚刚完成了我的第一个“真正的”计划

这是一场井字游戏

有人愿意阅读我的代码并告诉我哪里可以优化一些东西吗

#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;}
    }
}
#包括
使用名称空间std;
/**<董事会变量*/
字符串a1=“\”;字符串a2=“\”;字符串a3=“\”;
字符串b1=“\”;字符串b2=“\”;字符串b3=“\”;
字符串c1=“\”;字符串c2=“\”;字符串c3=“\”;
字符串展示板(){

你的错误就在这里

while(1){ /**< Play again */
            string a1 = "_";    string a2 = "_";    string a3 = "_";
            string b1 = "_";    string b2 = "_";    string b3 = "_";
            string c1 = "_";    string c2 = "_";    string c3 = "_";
while(1){/**<再次播放*/
字符串a1=“”字符串a2=“”字符串a3=“”;
字符串b1=“”字符串b2=“”字符串b3=“”;
字符串c1=“”字符串c2=“”字符串c3=“”;
如果已将它们声明为全局,则不应再次声明它们

您应该将此部分更改为此。因此,您的变量值将到处更新

while (1){ /**< Play again */
         a1 = "_";    a2 = "_";     a3 = "_";
         b1 = "_";    b2 = "_";     b3 = "_";
         c1 = "_";     c2 = "_";    c3 = "_";
while(1){/**<再次播放*/
a1=“”a2=“”a3=“”;
b1=“”b2=“”b3=“”;
c1=“”c2=“”c3=“”;

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; 
}
您现在可以大大简化“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;
}
bool movevalidation(字符串移动\u字符串,字符串播放器\u符号)
{
字符串移动[9]={“a1”、“a2”、“a3”、“b1”、“b2”、“b3”、“c1”、“c2”、“c3”};
int move_int=-1;//-1表示无效
对于(int i=0;i<9;++i)
{
if(move_string==moves[i])
move_int=i;
}
如果(移动<0 | |板[move|int]!=“|”)
{

如果你的代码运行正常,并且你只是在寻找同行评议,那么你应该在上面发布。这个网站是关于你遇到的问题的,不是你能帮我看一下吗?问题。你声明了9个变量,而不是某种数组或结构,这迫使你的代码在整个过程中有很多重复。当你的代码开始看起来像是所有的片段都被复制/粘贴了,这表明你做错了什么。幸运的是,tic tac toe只有9个位置。学会使用数组。“你通过获胜进入这一部分。”学习经典。哥们,这太不可思议了。非常感谢你的详细描述。我教自己C++,有一本书,天知道了数组,所以我用了我所知道的工具。如果我能达到15的名声,你会看到我的赞成票。
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; };
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;
        }
while (1) { /**< Play again */
    for(int i = 0; i < 9 ++i)
        board[i] = "_";