简体   繁体   中英

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. 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 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).

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#. I'm sure you can convert it in c++.

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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