2013-02-27 52 views
0

所以即時通信基本上創建一個二十一點遊戲,即時通訊卡在我必須問用戶,如果他想要另一張卡(採取HIT),我決定做一個while循環。我想弄清楚的一個問題是,我如何使它成爲第一個HIT選擇的隨機數,不能再次選擇,因爲我將該變量分配給「card3」While Loop和gen rand()

while (hit == 'yes' || hit == 'Yes' || hit =='Y' || hit == 'y') 
{ 
//create new card, add onto total, ask again 
card3 = rand() % 51 + 1; 
while (card3 == card1 || deal1 == card2 || card3 == deal1){ 
    card3 = rand() % 52 + 1; 
} 
if (card3 == 1 || card3 == 2 || card3 == 3 || card3 == 4){ 
cout <<"|A|"; 
total = total + 11;} 
else if (card3 == 5 || card3 == 6 || card3 == 7 || card3 == 8){ 
cout <<"|2|"; 
total = total + 2;} 
else if (card3 == 9 || card3 == 10 || card3 == 11 || card3 == 12){ 
cout <<"|3|"; 
total = total + 3;} 
else if (card3 == 13 || card3 == 14 || card3 == 15 || card3 == 16){ 
cout <<"|4|"; 
total = total + 4;} 
else if (card3 == 17 || card3 == 18 || card3 == 19 || card3 == 20){ 
cout <<"|5|"; 
total = total + 5;} 
else if (card3 == 21 || card3 == 22 || card3 == 23 || card3 == 24){ 
cout <<"|6|"; 
total = total + 6;} 
else if (card3 == 25 || card3 == 26 || card3 == 27 || card3 == 28){ 
cout <<"|7|"; 
total = total + 7;} 
else if (card3 == 29 || card3 == 30 || card3 == 31 || card3 == 32){ 
cout <<"|8|"; 
total = total + 8;} 
else if (card3 == 33 || card3 == 34 || card3 == 35 || card3 == 36){ 
cout <<"|9|"; 
total = total + 9;} 
else if (card3 == 37 || card3 == 38 || card3 == 39 || card3 == 40){ 
cout <<"|10|"; 
total = total + 10;} 
else if (card3 == 41 || card3 == 42 || card3 == 43 || card3 == 44){ 
cout <<"|J|"; 
total = total + 10;} 
else if (card3 == 45 || card3 == 46 || card3 == 47 || card3 == 48){ 
cout <<"|Q|"; 
total = total + 10;} 
else if (card3 == 49 || card3== 50 || card3 == 51 || card3 == 52){ 
cout <<"|K|"; 
total = total + 10;} 
cout << endl; 
cout <<"Your total is: " << total << endl; 
cout <<"Would you like another card? yes or no: " << endl; 
cin >> hit; 
cout << endl; 
} 
+0

這是一些有限制的功課,還是你自己學習? – hyde 2013-02-27 21:11:27

+0

@hyde這是學校作業 – 2013-02-27 21:16:54

+0

[C++生成rand數字]的可能重複(http://stackoverflow.com/questions/15118795/c-generating-rand-numbers) – Johnsyweb 2013-02-27 21:57:32

回答

2

你可以把所有的牌放在一個容器中,然後用std::random_shuffle來洗牌。然後從容器背面一次放一張卡片。當甲板完成後,重新開始。

+0

不幸的是,我只能做我們已經在課堂上教過的東西,所以幾乎如果語句和循環 – 2013-02-27 21:10:23

+2

@MichaelRamos然後你應該在問題中明確說明您的要求,因爲它們很人造。 – juanchopanza 2013-02-27 21:25:18

0

看起來像一個練習,你應該使用一個數組。我無法爲您編寫程序,但可以閱讀數組 - 它們是解決方案的重要組成部分。您可以創建一個由52個布爾變量組成的數組,用於指示哪些卡已被選中。你應該設計一個算法來檢查卡片是否被挑選出來,並且當我們得到一張隨機挑選的卡片時,繼續挑選一張新卡片。

+0

我們沒有涉及數組,因此我無法使用它們 – 2013-02-27 21:17:36