TIC TAC TOE游戏:如何循环

Tic Tac Toe Game: How to loop it?

本文关键字:循环 何循环 TAC TOE 游戏 TIC      更新时间:2023-10-16

目前正在从事TIC TAC TOE游戏的工作遇到了一个问题,即在玩家赢得比赛后循环循环。我尝试使用的是exit(1);,但是仅从整个程序中退出,并且不允许循环。我想要的是退出游戏,并提示用户提出Y/N问题,以循环游戏。这是功能的样子。

void checkwin(char drawTable[]){
if(drawTable[0] == 'X' && drawTable[1] == 'X' && drawTable[2] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[3] == 'X' && drawTable[4] == 'X' && drawTable[5] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[6] == 'X' && drawTable[7] == 'X' && drawTable[8] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[0] == 'X' && drawTable[4] == 'X' && drawTable[8] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[2] == 'X' && drawTable[4] == 'X' && drawTable[6] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[0] == 'X' && drawTable[3] == 'X' && drawTable[6] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[1] == 'X' && drawTable[4] == 'X' && drawTable[7] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[2] == 'X' && drawTable[5] == 'X' && drawTable[8] == 'X'){
    cout << PLAYER1.name << " won" << endl;
    exit(1);
}
else if(drawTable[0] == 'O' && drawTable[1] == 'O' && drawTable[2] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[3] == 'O' && drawTable[4] == 'O' && drawTable[5] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[6] == 'O' && drawTable[7] == 'O' && drawTable[8] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[0] == 'O' && drawTable[4] == 'O' && drawTable[8] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[2] == 'O' && drawTable[4] == 'O' && drawTable[6] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[0] == 'O' && drawTable[3] == 'O' && drawTable[6] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[1] == 'O' && drawTable[4] == 'O' && drawTable[7] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
}
else if(drawTable[2] == 'O' && drawTable[5] == 'O' && drawTable[8] == 'O'){
    cout << PLAYER2.name << " won" << endl;
    exit(1);
    //    }
    //        else
    //{
    //      cout << "It's a TIE" << endl;
    // }

}

使用一个while循环和Win的另一个功能(重构代码和退出条件)。

main() {
    while(1){
      //do everything here
      //when someone wins call
      someOneWon(name);
   }
}   

功能

void someOneWon(string name){
    char input;
    cout << name << " won" << endl;
    cout << "would you like to play another game ?(Y/N)"<<endl;
    do {
       input = getchar();
       putchar (input);
    }while( input != 'Y' && input != 'N');
    if (input == 'Y')
        return; // reinitilaize the game, set drawTable to starting point
    else
        exit(0);
}

您可以做这样的事情。这在C#中。我确定您可以在C 中转换。

string sUserInput = "y";
while(sUserInput.Equals("y")
{
   //Logic of your code.
   Console.WriteLine("Do you play again?")
   sUserInput = Console.Readline();
}