本帖最後由 歐柏罕 於 2018-4-9 20:53 編輯
需求說明:
1. 為程式加上一個適當的標題 (如: *** 猜拳遊戲 v1.0 ***)
2. 要有判斷輸入錯誤的機制
3. 加上戰果統計, 顯示總共贏了幾次, 輸了幾次, 平手幾次.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int player, computer;
- int win=0,lose=0,draw=0;
- srand(time(NULL));
- string name[]={"剪刀","石頭","布"};
- cout<<"☆★☆ 猜拳遊戲 ☆★☆"<<endl;
- start:
- cout<<endl<<"請出拳! (1)剪刀 (2)石頭 (3)布 (0)結束"<<endl;
- cin>>player;
- if(player==0)
- {
- goto end;
- }
- else if(player>=1 && player<=3)
- {
- computer=rand()%3+1;
- cout<<"你出: "<<name[player-1]<<endl;
- cout<<"電腦出: "<<name[computer-1]<<endl;
- if(player==computer)
- {
- cout<<"平手!"<<endl;
- draw++;
- }else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
- {
- cout<<"你贏了!"<<endl;
- win++;
- }else
- {
- cout<<"電腦贏了!"<<endl;
- lose++;
- }
- goto start;
- }
- else
- {
- cout<<"輸入錯誤!"<<endl;
- goto start;
- }
- end:
- cout<<endl<<"☆★☆ 戰果統計 ☆★☆"<<endl;
- cout<<"總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl<<endl;
- system("pause");
- return 0;
- }
複製代碼 |