想要返回 if/else 语句上的函数
Want to go back to a function on if/else statement
请看这段代码,我会解释:
void GameOver()
{
cout << "\nWelp, you died. Want to try again?" << endl;
cin >> choice;
if (choice == "Yes" || "yes")
{
/*This is where I want the code. I want it to go back to the last
function that the player was on.*/
}
if (choice == "No" || "no")
{
cout << "Are you sure? The game will start over when you open it back up." << endl;
cin >> choice;
if (choice == "No" || "no")
{
cout << "Well, bye now!" << endl;
usleep(1000000);
exit (EXIT_FAILURE);
}
}
return;
}
我希望这样当我在 GameOver 函数中选择 "Yes" 时,我想要一个 if/else
语句 "if you came from this function, then you will go to that function",你明白我在说什么吗?
例如,假设我在 GameOver
函数中,我来自 FightProcess
函数。我选择 "Yes" 然后它将转到 Town
函数。
我将如何编码?
我建议在 GameOver
函数中使用一个参数。然后每次你想去别的地方时,你都可以传递一个不同的参数。例如,从函数 1 调用 GameOver(1)
,从函数 2 调用 GameOver(2)
。
这是假设从 GameOver
返回并在调用函数中执行不同的选项不是一个选项。
首先声明如下:
if (choice == "Yes" || "yes")
编码错误,将始终评估为真。您需要改用它:
if (choice == "Yes" || choice == "yes")
或者更好的是,使用不区分大小写的比较函数,如下所示:
if (strcmpi(choice.c_str(), "Yes") == 0)
其次,除非你添加一个输入参数,或者使用一个全局变量,否则GameOver()
不知道是谁在调用它。所以你想做的事情不属于 GameOver()
本身。它属于调用函数。 GameOver()
如果用户选择不继续,则退出游戏。这就是它应该做的。如果游戏没有退出,调用函数应该决定如何重试。例如:
void GameOver()
{
cout << "\nWelp, you died. Want to try again?" << endl;
cin >> choice;
//if (choice == "Yes" || choice == "yes")
if (strcmpi(choice.c_str(), "Yes") == 0)
return;
cout << "Are you sure? The game will start over when you open it back up." << endl;
cin >> choice;
//if (choice == "No" || choice == "no")
if (strcmpi(choice.c_str(), "No") == 0)
return;
cout << "Well, bye now!" << endl;
usleep(1000000);
exit (EXIT_FAILURE);
}
void FightProcess()
{
...
if (defeated)
{
GameOver();
Town();
return;
}
...
}
或者,如果 Town()
是调用 FightProcess()
的函数:
void FightProcess()
{
...
if (defeated)
{
GameOver();
return;
}
...
}
void Town()
{
...
FightProcess();
...
}
或者,使用 FightProcess()
循环可能更有意义:
void FightProcess()
{
...
do
{
...
if (won)
break;
GameOver();
...
}
while (true);
...
}
看看如果不将限制性逻辑放在不属于它的地方,事情会变得更加灵活吗?
或者您可以选择在 FightProcess() 中触发一个事件。
例如:-
void FightProcess(){
...
if( ...){
observer.send("FightProcess"); // or with more information.
//observer.send("FightProcess",Avatar::Killed);
GameOver();
}
}
并且在 GameOver() 中,您可以查询观察者以查找最后一个事件是什么。
请看这段代码,我会解释:
void GameOver()
{
cout << "\nWelp, you died. Want to try again?" << endl;
cin >> choice;
if (choice == "Yes" || "yes")
{
/*This is where I want the code. I want it to go back to the last
function that the player was on.*/
}
if (choice == "No" || "no")
{
cout << "Are you sure? The game will start over when you open it back up." << endl;
cin >> choice;
if (choice == "No" || "no")
{
cout << "Well, bye now!" << endl;
usleep(1000000);
exit (EXIT_FAILURE);
}
}
return;
}
我希望这样当我在 GameOver 函数中选择 "Yes" 时,我想要一个 if/else
语句 "if you came from this function, then you will go to that function",你明白我在说什么吗?
例如,假设我在 GameOver
函数中,我来自 FightProcess
函数。我选择 "Yes" 然后它将转到 Town
函数。
我将如何编码?
我建议在 GameOver
函数中使用一个参数。然后每次你想去别的地方时,你都可以传递一个不同的参数。例如,从函数 1 调用 GameOver(1)
,从函数 2 调用 GameOver(2)
。
这是假设从 GameOver
返回并在调用函数中执行不同的选项不是一个选项。
首先声明如下:
if (choice == "Yes" || "yes")
编码错误,将始终评估为真。您需要改用它:
if (choice == "Yes" || choice == "yes")
或者更好的是,使用不区分大小写的比较函数,如下所示:
if (strcmpi(choice.c_str(), "Yes") == 0)
其次,除非你添加一个输入参数,或者使用一个全局变量,否则GameOver()
不知道是谁在调用它。所以你想做的事情不属于 GameOver()
本身。它属于调用函数。 GameOver()
如果用户选择不继续,则退出游戏。这就是它应该做的。如果游戏没有退出,调用函数应该决定如何重试。例如:
void GameOver()
{
cout << "\nWelp, you died. Want to try again?" << endl;
cin >> choice;
//if (choice == "Yes" || choice == "yes")
if (strcmpi(choice.c_str(), "Yes") == 0)
return;
cout << "Are you sure? The game will start over when you open it back up." << endl;
cin >> choice;
//if (choice == "No" || choice == "no")
if (strcmpi(choice.c_str(), "No") == 0)
return;
cout << "Well, bye now!" << endl;
usleep(1000000);
exit (EXIT_FAILURE);
}
void FightProcess()
{
...
if (defeated)
{
GameOver();
Town();
return;
}
...
}
或者,如果 Town()
是调用 FightProcess()
的函数:
void FightProcess()
{
...
if (defeated)
{
GameOver();
return;
}
...
}
void Town()
{
...
FightProcess();
...
}
或者,使用 FightProcess()
循环可能更有意义:
void FightProcess()
{
...
do
{
...
if (won)
break;
GameOver();
...
}
while (true);
...
}
看看如果不将限制性逻辑放在不属于它的地方,事情会变得更加灵活吗?
或者您可以选择在 FightProcess() 中触发一个事件。
例如:-
void FightProcess(){
...
if( ...){
observer.send("FightProcess"); // or with more information.
//observer.send("FightProcess",Avatar::Killed);
GameOver();
}
}
并且在 GameOver() 中,您可以查询观察者以查找最后一个事件是什么。