標題:
決戰21點
[打印本頁]
作者:
tonyh
時間:
2014-2-8 13:16
標題:
決戰21點
本帖最後由 tonyh 於 2014-2-8 15:06 編輯
試設計一個21點的電腦遊戲, 與電腦比大小. 遊戲過程中玩家要能依據目前持有的點數, 決定是否再抽一張牌. 若所有牌的總合超過21點, 就算爆掉, 就輸囉! 在此程式中, 不考慮牌面A的值可為1或11. A的值為1, J, Q 與K的值皆為10. 最後在遊戲結束時統計戰績.
[attach]829[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
鎧言
時間:
2014-2-8 15:24
本帖最後由 鎧言 於 2014-2-15 16:14 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
re:
system("cls");
int a[10], b[10], c[10], d[10], r1=0, r2=0, sum1=0, sum2=0;
char option;
for(int i=0; i<=9; i++)
{
a[i]=rand()%4+3;
b[i]=rand()%13+1;
c[i]=rand()%4+3;
d[i]=rand()%13+1;
}
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r1; i++)
{
cout<<char(a[i]);
if(b[i]==1)
{
cout<<"A ";
sum1+=1;
}else if(b[i]==11)
{
cout<<"J ";
sum1+=10;
}else if(b[i]==12)
{
cout<<"Q ";
sum1+=10;
}else if(b[i]==13)
{
cout<<"k ";
sum1+=10;
}else
{
cout<<b[i]<<" ";
sum1+=b[i];
}
}
cout<<endl<<"目前點數: "<<sum1<<endl;
if(sum1==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else if(sum1>21)
{
cout<<"你爆了!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else
{
cout<<"再抽一張牌? (y/n) ";
cin>>option;
if(option=='n')
break;
else
sum1=0;
}
r1++;
}
/*cout<<endl<<"輪到電腦抽牌囉..."<<endl<<endl;
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r2; i++)
{
cout<<char(c[i]);
if(d[i]==1)
{
cout<<"A ";
sum2+=1;
}else if(d[i]==11)
{
cout<<"J ";
sum2+=10;
}else if(d[i]==12)
{
cout<<"Q ";
sum2+=10;
}else if(d[i]==13)
{
cout<<"k ";
sum2+=10;
}else
{
cout<<d[i]<<" ";
sum2+=d[i];
}
}
cout<<endl<<"目前點數: "<<sum2<<endl;
if(sum2==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else if(sum2>21)
{
cout<<"電腦爆了!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else
{
if(sum2>=sum1)
{
if(sum2>sum1)
cout<<endl<<"電腦贏了!"<<endl;
else
cout<<endl<<"平手!"<<endl;
goto end;
}else
{
cout<<"電腦決定再抽一張牌!"<<endl;
sum2=0;
}
}
r2++;
}*/
end:
system("pause");
goto re;
system("pause");
return 0;
}
複製代碼
作者:
劉泳鱔
時間:
2014-2-8 15:26
3
複製代碼
作者:
郭凡瑛
時間:
2014-2-8 18:29
13
複製代碼
作者:
林以諾
時間:
2014-2-11 20:46
本帖最後由 林以諾 於 2014-2-16 16:15 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
re:
system("cls");
int a[10], b[10], c[10], d[10], r1=0, r2=0, sum1=0, sum2=0;
char option;
for(int i=0; i<=9; i++)
{
a[i]=rand()%4+3;
b[i]=rand()%13+1;
c[i]=rand()%4+3;
d[i]=rand()%13+1;
}
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r1; i++)
{
cout<<char(a[i]);
if(b[i]==1)
{
cout<<"A ";
sum1+=1;
}else if(b[i]==11)
{
cout<<"J ";
sum1+=10;
}else if(b[i]==12)
{
cout<<"Q ";
sum1+=10;
}else if(b[i]==13)
{
cout<<"k ";
sum1+=10;
}else
{
cout<<b[i]<<" ";
sum1+=b[i];
}
}
cout<<endl<<"目前點數: "<<sum1<<endl;
if(sum1==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else if(sum1>21)
{
cout<<"你爆了!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else
{
cout<<"再抽一張牌? (y/n) ";
cin>>option;
if(option=='n')
break;
else
sum1=0;
}
r1++;
}
cout<<endl<<"輪到電腦抽牌囉..."<<endl<<endl;
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r2; i++)
{
cout<<char(c[i]);
if(d[i]==1)
{
cout<<"A ";
sum2+=1;
}else if(d[i]==11)
{
cout<<"J ";
sum2+=10;
}else if(d[i]==12)
{
cout<<"Q ";
sum2+=10;
}else if(d[i]==13)
{
cout<<"k ";
sum2+=10;
}else
{
cout<<d[i]<<" ";
sum2+=d[i];
}
}
cout<<endl<<"目前點數: "<<sum2<<endl;
if(sum2==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else if(sum2>21)
{
cout<<"電腦爆了!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else
{
if(sum2>=sum1)
{
if(sum2>sum1)
cout<<endl<<"電腦贏了!"<<endl;
else
cout<<endl<<"平手!"<<endl;
goto end;
}else
{
cout<<"電腦決定再抽一張牌!"<<endl;
sum2=0;
}
}
r2++;
}
end:
system("pause");
goto re;
system("pause");
return 0;
}
複製代碼
作者:
張瀚仁
時間:
2014-2-15 11:41
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a,b,c,d,f,g,h,i,j,k;
srand(time(NULL));
start:
int n=0,e=0,ce=0,cn=0;
cout<<"**決戰21點v1.0**"<<endl;
system("pause");
system("cls");
re:
a=rand()%4+3;
b=rand()%13+1;
c=rand()%4+3;
d=rand()%13+1;
cout<<"你抽到到的是 "<<char(a);
n++;
if(b==1)
cout<<"A ";
else if(b==11)
cout<<"J ";
else if(b==12)
cout<<"Q ";
else if(b==13)
cout<<"K ";
else
cout<<b<<" ";
cout<<char(c);
n++;
if(d==1)
cout<<"A ";
else if(d==11)
cout<<"J ";
else if(d==12)
cout<<"Q ";
else if(d==13)
cout<<"K ";
else
cout<<d<<endl;
if(b==10 || b==11 || b==12 || b==13 || b==1 )
{
e=e+10;
}else
{
e=e+b;
}
if(d==10 || d==11 || d==12 || d==13 || d==1)
{
e=e+10;
}else
{
e=e+d;
}
if(e>21)
{
cout<<"你目前的點數是:"<<e<<endl;
loose:
cout<<"你輸了!!"<<endl;
system("pause");
system("cls");
goto start;
}else
if(n>=6 && e<=21)
{
cout<<"你目前的點數是:"<<e<<endl;
win:
cout<<"你贏了"<<endl;
system("pause");
system("cls");
goto start;
}
if(e==21 && n<6)
{
cout<<"Black Jack!!"<<endl;
}
cout<<"你目前的點數是: "<<e<<endl;
hi:
cout<<"還要再抽牌嗎? (1)yes(2)no"<<endl;
cin>>f;
if(f==1)
goto re;
else if(f==2)
goto com;
else
{
goto hi;
}
com:
g=rand()%4+3;
h=rand()%13+1;
i=rand()%4+3;
j=rand()%13+1;
cout<<"電腦抽到到的是 "<<char(g);
cn++;
if(h==1)
cout<<"A ";
else if(h==11)
cout<<"J ";
else if(h==12)
cout<<"Q ";
else if(h==13)
cout<<"K ";
else
cout<<h<<" ";
cout<<char(i);
ce++;
if(j==1)
cout<<"A ";
else if(j==11)
cout<<"J ";
else if(j==12)
cout<<"Q ";
else if(j==13)
cout<<"K ";
else
cout<<j<<endl;
if(h==10 || h==11 || h==12 || h==13 || h==1 )
{
ce=ce+10;
}else
{
ce=ce+b;
}
if(j==10 || j==11 || j==12 || j==13 || j==1)
{
ce=ce+10;
}else
{
ce=ce+j;
}
if(cn>=6 && ce<=21)
{
cout<<"電腦目前的點數是:"<<ce<<endl;
cout<<"電腦贏了"<<endl;
goto loose;
}
if(ce>21)
{
cout<<"電腦目前的點數是:"<<ce<<endl;
cout<<"點腦爆了!!"<<endl;
goto win;
}
if(ce==21 && n<6)
{
cout<<"Black Jack!!"<<endl;
}
cout<<"電腦目前的點數是:"<<ce<<endl;
if(ce>16 && ce>0)
{
cout<<"電腦決定再抽一次!!"<<endl;
goto com;
}else if(ce>=16 || ce>=17 || ce>=18)
{
cout<<"電腦決定不要再抽牌了"<<endl;
goto end;
}
end:
cout<<"玩家"<<e<<"點,電腦"<<ce<<"點"<<endl;
if(ce>e)
{
goto loose;
}else if(e>ce)
{
goto win;
}else if(e==ce)
{
cout<<"平手"<<endl;
goto start;
}
system("pause");
return 0;
}
複製代碼
作者:
許逸群
時間:
2014-2-15 12:00
本帖最後由 許逸群 於 2014-2-15 12:11 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
re:
system("cls");
int a[10], b[10], c[10], d[10], r1=0, r2=0, sum1=0, sum2=0;
char option;
for(int i=0; i<=9; i++)
{
a[i]=rand()%4+3;
b[i]=rand()%13+1;
c[i]=rand()%4+3;
d[i]=rand()%13+1;
}
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r1; i++)
{
cout<<char(a[i]);
if(b[i]==1)
{
cout<<"A ";
sum1+=1;
}else if(b[i]==11)
{
cout<<"J ";
sum1+=10;
}else if(b[i]==12)
{
cout<<"Q ";
sum1+=10;
}else if(b[i]==13)
{
cout<<"k ";
sum1+=10;
}else
{
cout<<b[i]<<" ";
sum1+=b[i];
}
}
cout<<endl<<"目前點數: "<<sum1<<endl;
if(sum1==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else if(sum1>21)
{
cout<<"你爆了!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else
{
cout<<"再抽一張牌? (y/n) ";
cin>>option;
if(option=='n')
break;
else
sum1=0;
}
r1++;
}
cout<<endl<<"輪到電腦抽牌囉..."<<endl<<endl;
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r2; i++)
{
cout<<char(c[i]);
if(d[i]==1)
{
cout<<"A ";
sum2+=1;
}else if(d[i]==11)
{
cout<<"J ";
sum2+=10;
}else if(d[i]==12)
{
cout<<"Q ";
sum2+=10;
}else if(d[i]==13)
{
cout<<"k ";
sum2+=10;
}else
{
cout<<d[i]<<" ";
sum2+=d[i];
}
}
cout<<endl<<"目前點數: "<<sum2<<endl;
if(sum2==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else if(sum2>21)
{
cout<<"電腦爆了!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else
{
if(sum2>=sum1)
{
if(sum2>sum1)
cout<<endl<<"電腦贏了!"<<endl;
else
cout<<endl<<"平手!"<<endl;
goto end;
}else
{
cout<<"電腦決定再抽一張牌!"<<endl;
sum2=0;
}
}
r2++;
}
end:
system("pause");
goto re;
system("pause");
return 0;
}
複製代碼
作者:
黃崇維
時間:
2014-2-16 11:57
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
re:
system("cls");
int a[10], b[10], c[10], d[10], r1=0, r2=0,sum1=0,sum2=0;
char option;
for(int i=0; i<=9; i++)
{
a[i]=rand()%4+3;
b[i]=rand()%13+1;
c[i]=rand()%4+3;
d[i]=rand()%13+1;
}
for(int j=0; j<=8; j++)
{
for(int i=0; i<=1+r1; i++)
{
cout<<char(a[i]);
if(b[i]==1)
{
cout<<"A";
sum1+=1;
}else if(b[i]==11)
{
cout<<"J";
sum1+=10;
}else if(b[i]==12)
{
cout<<"Q";
sum1+=10;
}else if(b[i]==13)
{
cout<<"K";
sum1+=10;
}else
{
cout<<b[i]<<" ";
sum1+=b[i];
}
}
cout<<endl<<"目前點數: "<<sum1<<endl;
if(sum1==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else if(sum1>21)
{
cout<<"你爆了!"<<endl;
cout<<"電腦贏了!"<<endl;
goto end;
}else
{
cout<<"再抽一張牌?(y/n)";
cin>>option;
if(option=='n')
break;
else
sum1=0;
}
r1++;
}
cout<<endl<<"輪到電腦抽排囉..."<<endl<<endl;
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r2; i++)
{
cout<<char(c[i]);
if(d[i]==1)
{
cout<<"A";
sum2+=1;
}else if(d[i]==11)
{
cout<<"J";
sum2+=10;
}else if(d[i]==12)
{
cout<<"Q";
sum2+=10;
}else if(d[i]==13)
{
cout<<"K";
sum2+=10;
}else
{
cout<<d[i]<<" ";
sum2+=d[i];
}
}
cout<<endl<<"目前點數: "<<sum2<<endl;
if(sum2==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else if(sum2>21)
{
cout<<"電腦爆了!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else
{
if(sum2>=sum1)
{
if(sum2>=sum1)
cout<<endl<<"電腦贏了!"<<endl;
else
cout<<endl<<"平手!"<<endl;
goto end;
}else
{
cout<<"電腦決定再抽一張牌!"<<endl;
sum2=0;
}
}
r2++;
}
end:
system("pause");
goto re;
system("pause");
return 0;
}
複製代碼
作者:
劉得恩
時間:
2014-5-28 13:28
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
re:
system("cls");
int a[10], b[10], c[10], d[10], r1=0, r2=0,sum1=0,sum2=0;
char option;
for(int i=0; i<=9; i++)
{
a[i]=rand()%4+3;
b[i]=rand()%13+1;
c[i]=rand()%4+3;
d[i]=rand()%13+1;
}
for(int j=0; j<=8; j++)
{
for(int i=0; i<=1+r1; i++)
{
cout<<char(a[i]);
if(b[i]==1)
{
cout<<"A";
sum1+=1;
}else if(b[i]==11)
{
cout<<"J";
sum1+=10;
}else if(b[i]==12)
{
cout<<"Q";
sum1+=10;
}else if(b[i]==13)
{
cout<<"K";
sum1+=10;
}else
{
cout<<b[i]<<" ";
sum1+=b[i];
}
}
cout<<endl<<"目前點數: "<<sum1<<endl;
if(sum1==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else if(sum1>21)
{
cout<<"你爆了!"<<endl;
cout<<"電腦贏了!"<<endl;
goto end;
}else
{
cout<<"再抽一張牌?(y/n)";
cin>>option;
if(option=='n')
break;
else
sum1=0;
}
r1++;
}
cout<<endl<<"輪到電腦抽排囉..."<<endl<<endl;
for(int j=0; j<=9; j++)
{
for(int i=0; i<=1+r2; i++)
{
cout<<char(c[i]);
if(d[i]==1)
{
cout<<"A";
sum2+=1;
}else if(d[i]==11)
{
cout<<"J";
sum2+=10;
}else if(d[i]==12)
{
cout<<"Q";
sum2+=10;
}else if(d[i]==13)
{
cout<<"K";
sum2+=10;
}else
{
cout<<d[i]<<" ";
sum2+=d[i];
}
}
cout<<endl<<"目前點數: "<<sum2<<endl;
if(sum2==21)
{
cout<<"BlackJack!"<<endl;
cout<<endl<<"電腦贏了!"<<endl;
goto end;
}else if(sum2>21)
{
cout<<"電腦爆了!"<<endl;
cout<<endl<<"你贏了!"<<endl;
goto end;
}else
{
if(sum2>=sum1)
{
if(sum2>=sum1)
cout<<endl<<"電腦贏了!"<<endl;
else
cout<<endl<<"平手!"<<endl;
goto end;
}else
{
cout<<"電腦決定再抽一張牌!"<<endl;
sum2=0;
}
}
r2++;
}
end:
system("pause");
goto re;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2