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