简体   繁体   English

井字游戏:如何循环播放?

[英]Tic Tac Toe Game: How to loop it?

Currently working on a Tic Tac Toe Game ran across a problem with looping the function after a player has won. 目前正在制作Tic Tac Toe Game的游戏遇到了一个问题,即在玩家获胜后循环使用该功能。 I tried using, exit(1); 我尝试使用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 ay/n question to loop the game or not. 我想要退出游戏,然后用是/否问题提示用户是否循环游戏。 Here is what the function looks like. 该函数的外观如下。

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


}

use a while loop and another function for win (to refactor the code and exit condition). 使用while循环和另一个函数获胜(重构代码并退出条件)。

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

You can do something like this. 你可以做这样的事情。 This is in C#. 这是在C#中。 I'm sure you can convert it in c++. 我确定您可以将其转换为c ++。

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM