Tic Tac Toe游戏:如何循环播放?

时间:2021-08-16 22:01:27

Currently working on a Tic Tac Toe Game ran across a problem with looping the function after a player has won. I tried using, exit(1); however that just exits out of the entire program, and doesn't allow a loop. What I want is to exit out of the game and prompt the user with a y/n question to loop the game or not. Here is what the function looks like.

目前正在开展Tic Tac Toe游戏,在玩家赢了之后遇到了循环功能的问题。我试过用,退出(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;
    // }


}

2 个解决方案

#1


use a while loop and another function for win (to refactor the code and exit condition).

使用while循环和另一个win函数(重构代码和退出条件)。

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

function

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);
}

#2


You can do something like this. This is in C#. I'm sure you can convert it in c++.

你可以做这样的事情。这是在C#中。我相信你可以用c ++转换它。

string sUserInput = "y";
while(sUserInput.Equals("y")
{

   //Logic of your code.
   Console.WriteLine("Do you play again?")
   sUserInput = Console.Readline();

}

#1


use a while loop and another function for win (to refactor the code and exit condition).

使用while循环和另一个win函数(重构代码和退出条件)。

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

function

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);
}

#2


You can do something like this. This is in C#. I'm sure you can convert it in c++.

你可以做这样的事情。这是在C#中。我相信你可以用c ++转换它。

string sUserInput = "y";
while(sUserInput.Equals("y")
{

   //Logic of your code.
   Console.WriteLine("Do you play again?")
   sUserInput = Console.Readline();

}