本帖最後由 tonyh 於 2012-9-29 15:16 編輯
讓使用者輸入0可選擇退出遊戲,
同時顯示戰果統計,
包括了共玩了幾次, 贏幾次, 輸幾次, 平手幾次, 猜贏的機率為幾%.- #include<iostream>
- using namespace std;
- int main()
- {
- int computer;
- int player;
- int win=0, lose=0, draw=0;
- start:
- cout<<"請輸入你想出的拳, 或輸入0退出遊戲: (1=剪刀 2=石頭 3=布) ";
- cin>>player;
- srand(time(NULL));
- computer=rand()%3+1;
- if(computer==1)
- {
- if(player==1)
- {
- cout<<"電腦出剪刀, 你也出剪刀, 平手!";
- draw++;
- }else if(player==2)
- {
- cout<<"電腦出剪刀, 你出石頭, 你贏了!";
- win++;
- }else if(player==3)
- {
- cout<<"電腦出剪刀, 你出布, 你輸了!";
- lose++;
- }else if(player==0)
- {
- goto over;
- }else
- {
- cout<<"你輸入錯誤!";
- }
- }
- if(computer==2)
- {
- if(player==1)
- {
- cout<<"電腦出石頭, 你出剪刀, 你輸了!";
- lose++;
- }else if(player==2)
- {
- cout<<"電腦出石頭, 你也出石頭, 平手!";
- draw++;
- }else if(player==3)
- {
- cout<<"電腦出石頭, 你出布, 你贏了!";
- win++;
- }else if(player==0)
- {
- goto over;
- }else
- {
- cout<<"你輸入錯誤!";
- }
- }
- if(computer==3)
- {
- if(player==1)
- {
- cout<<"電腦出布, 你出剪刀, 你贏了!";
- win++;
- }else if(player==2)
- {
- cout<<"電腦出布, 你出石頭, 你輸了!";
- lose++;
- }else if(player==3)
- {
- cout<<"電腦出布, 你也出布, 平手!";
- draw++;
- }else if(player==0)
- {
- goto over;
- }else
- {
- cout<<"你輸入錯誤!";
- }
- }
- cout<<endl<<endl;
- goto start;
- over:
- float total=win+lose+draw;
- cout<<endl<<"***戰績統計***"<<endl;
- cout<<"總共玩了"<<total<<"次, 贏了"<<win<<"次, 輸了"
- <<lose<<"次, 平手"<<draw<<"次."<<endl;
- float rate=win/total*100;
- cout<<"你贏的機率為"<<rate<<"%"<<endl;
- int level=rate/10;
- switch(level)
- {
- case 8: case 9: case 10:
- cout<<"太神了!"<<endl;
- break;
- case 5: case 6: case 7:
- cout<<"運氣超好的呦!"<<endl;
- break;
- case 4:
- cout<<"運氣很不錯!"<<endl;
- break;
- case 3:
- cout<<"今天運氣在正常範圍內!"<<endl;
- break;
- case 2:
- cout<<"運氣有點背!"<<endl;
- break;
- case 1: case 0:
- cout<<"你今天肯定衰到爆!"<<endl;
- break;
- }
- cout<<endl;
- system("pause");
- return 0;
- }
複製代碼 |