- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int a, b;
- int win=0,lose=0,draw=0;
- srand(time(NULL));
- string name[]={"剪刀","石頭","布"};
- cout<<"猜拳遊戲"<<endl;
- start:
- cout<<endl<<"請出拳! (1)剪刀 (2)石頭 (3)布 (0)結束"<<endl;
- cin>>a;
- if(a==0)
- goto end;
- else if(a>=1 && a<=3)
- {
- b=rand()%3+1;
- cout<<"你出: "<<name[a-1]<<endl;
- cout<<"電腦出: "<<name[b-1]<<endl;
- if((a==1 && b==3)||(a==2 && b==1)||(a==3 && b==2))
- {
- cout<<"你贏了"<<endl;
- win++;
- }
- if((b==1&&a==3)||(b==2&&a==1)||(b==3&&a==2))
- {
- cout<<"你輸了"<<endl;
- lose++;
- }
- if(a==b)
- {
- cout<<"平手"<<endl;
- draw++;
- }
- goto start;
- }else
- {
- cout<<"輸入錯誤!"<<endl;
- goto start;
- }
- end:
- cout<<endl<<"戰果統計"<<endl;
- cout<<"總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl<<endl;
- system("pause");
- return 0;
- }
複製代碼 |