2014-10-28 78 views
-1

我正在創建一個將用完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!

+1

你知道['while'loops](http://msdn.microsoft.com/en-us/library/0c98k0ks.aspx)嗎? – clcto 2014-10-28 21:55:14

+0

當我有多個功能時,如何使用'while'循環,並且遊戲在每個循環之間傳遞? @clcto – Welsh4588 2014-10-28 21:57:35

+0

您將信息從一個函數傳遞給另一個函數:'do {/*....*/ playAgain = PromptPlayAgain(); } while(playAgain);'例如。 – clcto 2014-10-28 21:59:17

回答

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

分享和享受。

+0

似乎是一個'do-while'的合適情況,不是嗎? – clcto 2014-10-28 22:14:14

+0

@clcto:可以使用任何循環結構。 :-) – 2014-10-28 22:38:46

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(); } 

乾杯!

相關問題