本帖最後由 李泳霖 於 2024-1-25 09:03 編輯
試運用亂數、陣列、迴圈等技巧,設計一個簡單的猜拳遊戲。
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- re:
- srand(time(NULL));
- int player,computer;
- string n[]={"剪刀","石頭","布"};
- cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
- cin>>player;
- computer=rand()%3+1;
- cout<<"你出"<<n[player-1]<<endl;
- cout<<"電腦出"<<n[computer-1]<<endl;
- if(player==computer)
- cout<<"平手!"<<endl;
- else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
- cout<<"你贏了!"<<endl;
- else
- cout<<"電腦贏了!"<<endl;
- cout<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |