- #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<<"請出拳:";
- cout<<endl<<"(1)剪刀 (2)石頭 (3)布 (0)結束"<<endl;
- cin>>player;
- if(player==0)
- goto over;
- 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;
- }
- over:
- cout<<endl<<"戰果統計: "<<endl;
- cout<<"你總共贏了"<<win<<"次, 輸了"<<lose<<"次, 平手"<<draw<<"次!"<<endl<<endl;
- system("pause");
- return 0;
- }
複製代碼 |