2016-05-10 17 views
0

訪問http://voyager.deanza.edu/~bentley/ass5.html骰子游戲,需要我們與添加*一起數算自己每天輪流搖骰子

我的目標是嘗試匹配的樣本輸出,鏈接之前。我似乎無法克服的唯一障礙是如何在每行擲骰之前添加回合以及「*」。

#include <iostream> 
#include <cstdlib> 
#include <cmath> 

using namespace std; 


int roll(); 
int turn(); 


int main() 
{ 
srand(time(0)); 

int gameTotal = 0; 

while (gameTotal < 100) 
{ 

    gameTotal += turn(); 
    cout << "Your total is now " << gameTotal << endl << endl;; 
} 


} 

int turn() 
{ 
int turnTotal = 0; 
int temp; 


for (int i = 0; i < 3; i++) 
{ 
    temp = roll(); 
    if (temp == 7) break; 
    turnTotal += temp; 

} 
cout << "You scored " << turnTotal << " points for this turn" << endl; 
return turnTotal; 
} 

int roll() 
{ 
int die1 = rand() % 6 + 1; 
int die2 = rand() % 6 + 1; 

int sum = die1 + die2; 



cout << "You rolled a " << die1 << " and " << die2 << ". " << "That's " << sum << endl; 
return sum; 
} 
+1

請說明您的具體問題或添加額外的細節,突顯你需要什麼。正如目前所寫,很難確切地說出你在問什麼。請參閱[如何提問](https://stackoverflow.com/help/how-to-ask)頁面以獲得澄清此問題的幫助。 – jotik

+2

請分享你的任何代碼,並詳細描述你是如何「卡住」的。我們也不做你的功課。 – Sheph

+1

感謝您的輸入。這個網站還是新手。只需附上我的代碼。抱歉不清楚。 –

回答

0

只需添加一個變量來存儲播放的回合數並在每一回合更新它。加入*甚至simplier:

int main() 
{ 
    srand(time(0)); 

    int gameTotal = 0; 
    int turns = 0; 

    while (gameTotal < 100) 
    { 
     // update number of turns and output it 
     ++turns; 
     cout << "This is your turn #" << turns << endl; 

     gameTotal += turn(); 
     cout << "*** Your total is now " << gameTotal << endl << endl; 
     //  ^^^ easy 
    } 
} 

在其它功能類似的變化:

cout << "** You scored " << turnTotal << " points for this turn" << endl; 
//  ^^ 

cout << "* You rolled a " << die1 << " and " << die2 << ". " << "That's " << sum << endl; 
//  ^
+0

你我的朋友,是真正的MVP ......謝謝你。真的很有幫助。 –

+0

@JoseVasquez歡迎您。如果它有幫助,你也可以考慮[接受](http://stackoverflow.com/help/accepted-answer)這個答案。 –