TIC TAC TOE游戏:如何循环
Tic Tac Toe Game: How to loop it?
目前正在从事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();
}
相关文章:
- 嵌套的 for 循环不循环通过第二个数组
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 编写按初始值循环的循环的更好方法是什么
- 如何在重复循环中循环遍历 std::vector
- 对于循环不循环和检测字符数组 [指针和字符数组]
- 自定义对象构造函数在循环外部循环
- 为什么在我的程序中的第二个循环时,当它在循环之前循环时不起作用
- 而循环比循环更有效.可能是什么原因
- 如何在循环中循环访问两个向量
- C++ for 循环继续循环,即使它不应该循环
- 当使用文件中的getline时,循环仅循环一次
- C 数组,如果循环.如果循环忽略值并在输入值时给出错误的行
- 对于循环(内部循环时)被忽略
- 对于循环,循环次数比它必须的次数多一次
- 我的循环保持循环,并导致程序崩溃
- 数组循环在循环结束后输出垃圾
- C++ do-while 循环不循环
- C++ AMP,用于循环parallel_for_each循环
- 避免在C++循环中循环中复杂对象时最小化范围效率低下的技术
- 设置 for 循环以循环,直到检测到空字节