運用產生隨機亂數的技巧, 設計一個猜拳遊戲,
使用者可以選擇出剪刀石頭或布, 電腦隨機出拳.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- int player,computer;
- cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
- cin>>player;
- computer=rand()%3+1;
- cout<<"你出";
- if(player==1)
- cout<<"剪刀!"<<endl;
- else if(player==2)
- cout<<"石頭!"<<endl;
- else
- cout<<"布!"<<endl;
- cout<<"電腦出";
- if(computer==1)
- cout<<"剪刀!"<<endl;
- else if(computer==2)
- cout<<"石頭!"<<endl;
- else
- cout<<"布!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |