如何重新启动C++命令提示符应用程序?
我正在创建一个将用完C++命令提示符的游戏。如何重新启动C++命令提示符应用程序?
这款游戏叫做PIG。你正在对抗电脑,你的目标是通过掷骰子达到100 GAME SCORE。如果你掷出1,你的回合结束,你没有增加任何分数。如果您掷出任何其他号码,它会被添加到您的“分数”中。滚动后,您可以选择再次滚动或“保持”。持有会将您的“比分”添加到您的“比赛分数”中,并将该回合传给下一名玩家。
一切都按照我希望的方式工作,但现在我正在尝试创建一个playagain()函数,在游戏结束时询问用户是否希望再次玩游戏。如果他们这样做,应用程序重新启动,并将零的所有变量。如果他们不这样做,程序就会退出。
这里是我的问候,我的问题:
if(comp_score == 100){
char ans;
cout << "Your opponent has reached a score of 100 and has won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
/*restarts application and zero's all variables*/
playagain();
} else if(ans == 'n'){ exit(); }}
if(play_score == 100){
char ans;
cout << "You have reached a score of 100 and have won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
/*restarts application and zero's all variables*/
playagain();
} else if(ans == 'n'){ exit(); }
}
TIA!
IMO做到这一点,最简单的方法是使用while
循环:
bool keep_playing = TRUE;
while (keep_playing)
{
keep_playing = FALSE;
/* zero out variables */
/* rest of code to play the game */
if(comp_score == 100){
char ans;
cout << "Your opponent has reached a score of 100 and has won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
keep_playing = TRUE;
} else if(ans == 'n')
{ keep_playing = FALSE; }}
if(play_score == 100){
char ans;
cout << "You have reached a score of 100 and have won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
keep_playing = TRUE;
} else if(ans == 'n')
{ keep_playing = FALSE; }}
} -- while (keep_playing)...
分享和享受。
似乎是一个'do-while'的合适情况,不是吗? – clcto 2014-10-28 22:14:14
@clcto:可以使用任何循环结构。 :-) – 2014-10-28 22:38:46
请记住,如果您使用Windows。您可以使用ShellExecute
打开一个新的游戏窗口,并返回0代码以关闭旧游戏窗口。喜欢这个。
#include <windows.h> // >>>>>> JACOBTECH EDIT.
if(comp_score == 100){
char ans;
cout << "Your opponent has reached a score of 100 and has won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
/*restarts application and zero's all variables*/
playagain();
} else if(ans == 'n'){ exit(); }}
if(play_score == 100){
char ans;
cout << "You have reached a score of 100 and have won! Would you like to play again? [y/n] ";
cin >> ans;
if(ans == 'y'){
ShellExecuteA(NULL, "open", "C:/GameDirectory/Game.exe", NULL, NULL, SW_NORMAL); //>>>>>> JACOBTECH EDIT.
return 0; //>>>>>> JACOBTECH EDIT.
} else if(ans == 'n'){ exit(); }
干杯!
你知道['while'loops](http://msdn.microsoft.com/en-us/library/0c98k0ks.aspx)吗? – clcto 2014-10-28 21:55:14
当我有多个功能时,如何使用'while'循环,并且游戏在每个循环之间传递? @clcto – Welsh4588 2014-10-28 21:57:35
您将信息从一个函数传递给另一个函数:'do {/*....*/ playAgain = PromptPlayAgain(); } while(playAgain);'例如。 – clcto 2014-10-28 21:59:17