所以即時通信基本上創建一個二十一點遊戲,即時通訊卡在我必須問用戶,如果他想要另一張卡(採取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;
}
這是一些有限制的功課,還是你自己學習? – hyde 2013-02-27 21:11:27
@hyde這是學校作業 – 2013-02-27 21:16:54
[C++生成rand數字]的可能重複(http://stackoverflow.com/questions/15118795/c-generating-rand-numbers) – Johnsyweb 2013-02-27 21:57:32