返回列表 發帖

猜拳遊戲 (一)

運用產生隨機亂數的技巧, 設計一個猜拳遊戲,
使用者可以選擇出剪刀石頭或布, 電腦隨機出拳.

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<time.h>
  4. using namespace std;
  5. int main()
  6. {         
  7.     int player=0;
  8.     int com=0;
  9.     cout<<"請出拳(1剪刀.2石頭3布)"<<endl;
  10.     cin>>player;
  11.     cout<<"電腦出拳中。。。"<<endl;
  12.     srand(time(NULL));
  13.     com=rand()%3+1;
  14.     if(com==1)
  15.     {
  16.       cout<<"電腦出剪刀"<<endl;
  17.     }
  18.     else if(com==2)
  19.     {
  20.       cout<<"電腦出石頭"<<endl;
  21.     }
  22.     else if(com==3)
  23.     {
  24.       cout<<"電腦出布"<<endl;
  25.     }
  26.     if(player==1&&com==1)
  27.     {
  28.       cout<<"平手"<<endl;   
  29.     }
  30.     else if(player==1&&com==2)
  31.     {
  32.       cout<<"你輸了"<<endl;   
  33.     }
  34.     else if(player==1&&com==3)
  35.     {
  36.       cout<<"你贏了"<<endl;   
  37.     }
  38.     else if(player==2&&com==1)
  39.     {
  40.       cout<<"你贏了"<<endl;   
  41.     }
  42.     else if(player==2&&com==2)
  43.     {
  44.       cout<<"平手"<<endl;   
  45.     }
  46.     else if(player==2&&com==3)
  47.     {
  48.       cout<<"你輸了"<<endl;   
  49.     }
  50.     else if(player==3&&com==1)
  51.     {
  52.       cout<<"你輸了"<<endl;   
  53.     }
  54.     else if(player==3&&com==2)
  55.     {
  56.       cout<<"你贏了"<<endl;   
  57.     }
  58.     else if(player==3&&com==3)
  59.     {
  60.       cout<<"平手"<<endl;   
  61.     }
  62.     system("pause");
  63.     return 0;
  64. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     int player=0;
  8.     int com=0;
  9.     cout<<"輸入你要的拳(1:剪刀,2:石頭,3:布)"<<endl;
  10.     cin>>player;
  11.     cout<<"電腦出拳中"<<endl;
  12.     srand(time(NULL));
  13.     com=(rand()%3)+1;
  14.     if(com==1)
  15.     {cout<<"電腦出剪刀"<<endl;
  16.     }
  17.     else if(com==2)
  18.     {cout<<"電腦出石頭"<<endl;
  19.     }
  20.     else if(com==3)
  21.     {cout<<"電腦出布"<<endl;
  22.     }
  23.    
  24.     if(player==1&&com==1)
  25.     {
  26.         cout<<"平手"<<endl;
  27.     }
  28.     else if(player==1&&com==2)
  29.     {
  30.         cout<<"輸"<<endl;
  31.     }
  32.     else if(player==1&&com==3)
  33.     {
  34.         cout<<"贏"<<endl;
  35.     }
  36.    
  37.     else if(player==2&&com==2)
  38.     {
  39.         cout<<"平手"<<endl;
  40.     }
  41.     else if(player==2&&com==1)
  42.     {
  43.         cout<<"贏"<<endl;
  44.     }
  45.     else if(player==2&&com==3)
  46.     {
  47.         cout<<"輸"<<endl;
  48.     }
  49.    
  50.     else if(player==3&&com==3)
  51.     {
  52.         cout<<"平手"<<endl;
  53.     }
  54.     else if(player==3&&com==2)
  55.     {
  56.         cout<<"贏"<<endl;
  57.     }
  58.     else if(player==3&&com==1)
  59.     {
  60.         cout<<"輸"<<endl;
  61.     }
  62.    
  63.    
  64.     system("pause");
  65.     return 0;     
  66. }
複製代碼

TOP

本帖最後由 戴嘉禾 於 2018-4-14 15:10 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<time.h>
  4. using namespace std;
  5. int main()
  6. {
  7.   re:
  8.   system("cls");        
  9.   srand(time(NULL));
  10.   int player=0;
  11.   int com=0;
  12.   cout<< "請輸入你想要出的拳   (1)剪刀(2)石頭(3)布" <<endl;
  13.   cin>>player;
  14.   cout<< "電腦出拳中..." <<endl;
  15.   com=(rand()%3)+1;
  16.   if(com==1)
  17.   cout<<"電腦出剪刀"<<endl;
  18.   else if(com==2)
  19.   cout<<"電腦出石頭"<<endl;
  20.   else if(com==3)
  21.   cout<<"電腦出布"<<endl;
  22.   if(player==com)
  23.   cout<<"平手"<<endl;
  24.   else if(player==1&&com==2)
  25.   cout<<"你輸了"<<endl;
  26.   else if(player==1&&com==3)
  27.   cout<<"你贏了"<<endl;
  28.   else if(player==2&&com==1)
  29.   cout<<"你贏了"<<endl;
  30.   else if(player==2&&com==3)
  31.   cout<<"你輸了"<<endl;
  32.   else if(player==3&&com==1)
  33.   cout<<"你輸了"<<endl;
  34.   else if(player==3&&com==2)
  35.   cout<<"你贏了"<<endl;   
  36. system("pause");
  37. goto re;
  38. return 0;
  39. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     //剪刀石頭布
  8.     string win[3]={"你贏","電腦贏","平手"};
  9.     int player=0;
  10.     int com=0;
  11.     cout<<"輸入你要的拳(1.剪刀2.石頭3.布)"<<endl;
  12.     cin>>player;
  13.     cout<<"電腦出拳中..."<<endl;
  14.     srand(time(NULL));
  15.     com=(rand()%3)+1;
  16.     if(player==1&&com==3)
  17.     {
  18.         cout<<win[0]<<endl;
  19.     }
  20.     if(player==1&&com==2)
  21.     {
  22.         cout<<win[1]<<endl;
  23.     }
  24.     if(player==2&&com==1)
  25.     {
  26.         cout<<win[0]<<endl;
  27.     }
  28.     if(player==2&&com==3)
  29.     {
  30.         cout<<win[1]<<endl;
  31.     }
  32.     if(player==3&&com==2)
  33.     {
  34.         cout<<win[0]<<endl;
  35.     }
  36.     if(player==2&&com==3)
  37.     {
  38.         cout<<win[1]<<endl;
  39.     }
  40.     else
  41.     {
  42.         cout<<win[2]<<endl;
  43.     }
  44.     system("PAUSE");
  45.     return 0;
  46. }
複製代碼

TOP

返回列表