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