本帖最後由 tonyh 於 2013-8-24 15:26 編輯
運用產生隨機亂數的技巧, 設計一個猜拳遊戲,
使用者可以選擇出剪刀石頭或布, 電腦隨機出拳,
比對後判斷輸贏.- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- int player,computer,again;
- string name[]={"剪刀","石頭","布"};
- cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
- cin>>player;
- srand(time(NULL));
- computer=rand()%3+1;
- cout<<"玩家出: "<<name[player-1]<<endl;
- cout<<"電腦出: "<<name[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<<"在玩一次? (1)是 (2)否 ";
- cin>>again;
- if(again==1)
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |