本帖最後由 張峻瑋 於 2014-3-15 17:02 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int player,computer,a=0,b=0,c=0;
- srand(time(NULL));
- string name[3]={"剪刀","石頭","布"};
- re:
- cout<<"*** 猜拳遊戲 ***"<<endl;
- cout<<"請出拳!(1)剪刀(2)石頭(3)布(0)結束";
- cin>>player;
- if(player==0)
- {
- goto end;
- }
- 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;
- a++;
- }
- else if((player==1 && computer==3)||
- (player==2 && computer==1)||
- (player==3 && computer==2))
- {
- cout<<"你贏了!"<<endl;
- b++;
- }
- else
- {
- cout<<"電腦贏了!"<<endl;
- c++;
- }
- goto re;
- }
- else
- {
- cout<<"輸入錯誤"<<endl;
- goto re;
- }
- cout<<endl;
- end:
- cout<<"*** 戰果統計 ***"<<endl;
- cout<<"總共贏了"<<b<<"次,輸了"<<c<<"次,"<<"平手"<<a<<"次"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |