本帖最後由 tonyh 於 2012-1-14 15:50 編輯
再次改良作業中的猜拳遊戲,
讓電腦能回應玩家, 贏的機率, 並針對玩家贏的機率發表評論.
譬如: 贏的機率80%以上時, 電腦回應"哇你好厲害".
贏的機率高於50%低於80%時, 電腦回應"還不錯喔".
依此類推, 自由發揮.- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int computer;
- int player;
- int tryagain;
- float win=0, lose=0, draw=0;
- int rate;
- start:
- srand(time(NULL));
- computer=rand()%3+1;
- cout<<"請輸入你要出的拳 1=剪刀 2=石頭 3=布"<<endl;
- cin>>player;
- if(computer==1)
- {
- if(player==1)
- {
- cout<<"你出剪刀, 電腦也出剪刀."<<endl<<"平手!"<<endl;
- draw++;
- }else if(player==2)
- {
- cout<<"你出石頭, 電腦出剪刀."<<endl<<"你贏了!"<<endl;
- win++;
- }else if(player==3)
- {
- cout<<"你出布, 電腦出剪刀."<<endl<<"你輸了!"<<endl;
- lose++;
- }else
- {
- cout<<"輸入錯誤!";
- }
- }
- if(computer==2)
- {
- if(player==1)
- {
- cout<<"你出剪刀, 電腦出石頭."<<endl<<"你輸了!"<<endl;
- lose++;
- }else if(player==2)
- {
- cout<<"你出石頭, 電腦也出石頭."<<endl<<"平手!"<<endl;
- draw++;
- }else if(player==3)
- {
- cout<<"你出布, 電腦出石頭."<<endl<<"你贏了!"<<endl;
- win++;
- }else
- {
- cout<<"輸入錯誤!";
- }
-
- }
- if(computer==3)
- {
- if(player==1)
- {
- cout<<"你出剪刀, 電腦出布."<<endl<<"你贏了!"<<endl;
- win++;
- }else if(player==2)
- {
- cout<<"你出石頭, 電腦出布."<<endl<<"你輸了!"<<endl;
- lose++;
- }else if(player==3)
- {
- cout<<"你出布, 電腦也出布."<<endl<<"平手!"<<endl;
- draw++;
- }else
- {
- cout<<"輸入錯誤!";
- }
- }
- cout<<"再玩一次嗎? 1=是 2=否"<<endl;
- cin>>tryagain;
- if(tryagain==1)
- {
- goto start;
- }else
- {
- goto end;
- }
- end:
- cout<<"總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl;
- rate=win/(win+lose+draw)*100;
- cout<<"你贏的機率為"<<rate<<"%"<<endl;
- if(rate>=80)
- {
- cout<<"哇!你好厲害!";
- }else if(rate>=50 && rate<80)
- {
- cout<<"還不錯喔!";
- }else if(rate>=20 && rate<50)
- {
- cout<<"運氣有點背喔!";
- }else
- {
- cout<<"你也太遜了!";
- }
- cout<<endl;
- system("pause");
- return 0;
- }
複製代碼 |