- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- int player=0;
- int com=0;
- int my=0;
- int comp=0;
- int game=1;
- string mora[]={"剪刀","石頭","布"};
- for(int i=game;i<=3;i++)
- {
- cout<<"輸入你要的拳(1:剪刀,2:石頭,3:布)"<<endl;
- cin>>player;
- cout<<"電腦出拳中"<<endl;
- com=(rand()%3)+1;
- cout << "你出:" << mora[player-1] << endl;
- cout << "電腦出:" << mora[com-1] << endl;
- if(player==com)
- {
- cout<<"平手"<<endl;
- }
- else if(player==1&&com==3)
- {
- cout<<"贏"<<endl;
- my++;
- }
- else if(player==2&&com==1)
- {
- cout<<"贏"<<endl;
- my++;
- }
- else if(player==3&&com==2)
- {
- cout<<"贏"<<endl;
- my++;
- }
- else
- {
- cout<<"輸"<<endl;
- comp++;
- }
- if(my==2||comp==2)
- {break;
- }
- }
- if(my>comp)
- {
- cout<<"你贏"<<endl;
- }
- else
- {cout<<"我贏了"<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |