如何重新启动C ++命令提示符应用程序?

时间:2014-10-28 21:52:55

标签: c++

我正在创建一个用完C ++命令提示符的游戏。

这款游戏名为PIG。你正在与电脑对战,你的目标是通过掷骰子达到100 GAME SCORE。如果你掷1,你的回合结束,你没有添加任何你的分数。如果您滚动任何其他号码,它将被添加到您的“TURN SCORE”中。滚动后,您可以选择再次滚动或“保持”。举行将“TURN SCORE”添加到“GAME SCORE”并将转弯传递给下一位玩家。

一切都按照我想要的方式工作,但现在我正在尝试创建一个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!

2 个答案:

答案 0 :(得分:0)

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

分享并享受。

答案 1 :(得分:0)

请记住,如果您使用的是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(); }

干杯!

相关问题