返回列表 發帖

猜拳遊戲 (一)

本帖最後由 tonyh 於 2015-5-9 11:11 編輯

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player, computer;
  8.     cout<<"請出拳! (1)剪刀 (2)石頭 (3)布  ";
  9.     cin>>player;
  10.     if(player==1)
  11.         cout<<"你出剪刀!"<<endl;
  12.     else if(player==2)
  13.         cout<<"你出石頭!"<<endl;
  14.     else
  15.         cout<<"你出布!"<<endl;
  16.     computer=rand()%3+1;
  17.     if(computer==1)
  18.         cout<<"電腦出剪刀!"<<endl;
  19.     else if(computer==2)
  20.         cout<<"電腦出石頭!"<<endl;
  21.     else
  22.         cout<<"電腦出布!"<<endl;
  23.     system("pause");
  24.     return 0;   
  25. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     re:
  8.         int a=0,c=0;
  9.    cout<<"請猜拳(1)剪刀(2)石頭(3)布"<<endl;  
  10.    cin>>a;
  11.     cout<<"你"<<endl;
  12.    if(a==1)
  13.    {
  14.    cout<<"剪刀"<<endl;      
  15.    }
  16.    if(a==2)
  17.    {
  18.    cout<<"石頭"<<endl;      
  19.    }
  20.       if(a==3)
  21.    {
  22.    cout<<"布"<<endl;      
  23.    }
  24.    c=rand()%3+1;
  25.    cout<<"電腦"<<endl;
  26.    if(c==1)
  27.    {
  28.    cout<<"剪刀"<<endl;      
  29.    }
  30.       if(c==2)
  31.    {
  32.    cout<<"石頭"<<endl;      
  33.    }
  34.       if(c==3)
  35.    {
  36.    cout<<"布"<<endl;      
  37.    }
  38.     system("pause");
  39.     return 0;
  40. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));   
  7.     int p,c;
  8.     cout<<"請出拳! (1)刀(2)石(3)網 ";
  9.     cin>>p;
  10.    
  11.     if(p==1)
  12.         cout<<"出刀"<<endl;
  13.     else if(p==3)
  14.         cout<<"出網"<<endl;  
  15.     else
  16.         cout<<"出石"<<endl;
  17.     c=rand()%3+1;
  18.     if(p==1)
  19.         cout<<"對手出刀"<<endl;
  20.     else if(p==3)
  21.         cout<<"對手出網"<<endl;  
  22.     else
  23.         cout<<"對手出石"<<endl;              
  24.    system("pause");
  25.    return 0;
  26. }
複製代碼

TOP

本帖最後由 梁和雋 於 2015-5-6 21:46 編輯

正常版
  1. #include <iostream>
  2. #include <cstdlib>;
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b, c;
  7.     cout<<"請輸入你出的拳"<<endl;
  8.     cout<<"(1):剪刀, (2):石頭, (3):布"<<endl;
  9.     cin>>a;
  10.    
  11.         if(a==1)
  12.              cout<<"你出剪刀"<<endl;
  13.         else if(a==2)
  14.              cout<<"你出石頭"<<endl;  
  15.         else if(a==3)
  16.              cout<<"你出布"<<endl;
  17.         else
  18.         {
  19.              srand(time(NULL));
  20.              for(int i=1; i>=0; i++)
  21.              {
  22.                  cout<<rand()<<"錯誤!";
  23.                  cout<<rand()<<"(。凡。''')";
  24.                  cout<<rand()<<"orz";
  25.              }        
  26.         }  
  27.                     
  28.     srand(time(NULL));
  29.     b=rand()%3+1;
  30.         if(b==1)
  31.              cout<<"電腦出剪刀"<<endl;
  32.         else if(b==2)
  33.              cout<<"電腦出石頭"<<endl;  
  34.         else if(b==3)
  35.              cout<<"電腦出布"<<endl;
  36.             
  37.         if(a==b)
  38.              cout<<"平手"<<endl;
  39.         else if(a>b && a-b==1)
  40.              cout<<"you win"<<endl;
  41.         else if(b>a && b-a==2)
  42.              cout<<"you win"<<endl;
  43.         else
  44.              cout<<"you lose"<<endl;

  45.     system ("pause") ;
  46.     return 0;
  47. }
複製代碼
題外話 本人戰績  輸24場 平手34場 5勝.....
電腦有作弊嫌疑呀
穩輸版
  1. #include <iostream>
  2. #include <cstdlib>;
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b, c;
  7.     cout<<"請輸入你出的拳"<<endl;
  8.     cout<<"(1):剪刀, (2):石頭, (3):布"<<endl;
  9.     cin>>a;
  10.    
  11.         if(a==1)
  12.         {
  13.              cout<<"你出剪刀"<<endl;
  14.              cout<<"電腦出石頭"<<endl;
  15.              cout<<"你輸了"<<endl;
  16.         }
  17.         else if(a==2)
  18.         {
  19.              cout<<"你出石頭"<<endl;
  20.              cout<<"電腦出布"<<endl;
  21.              cout<<"你輸了"<<endl;
  22.         }      
  23.         else if(a==3)
  24.         {
  25.              cout<<"你出布"<<endl;
  26.              cout<<"電腦出剪刀"<<endl;
  27.              cout<<"你輸了"<<endl;
  28.         }     
  29.         else
  30.         {
  31.              srand(time(NULL));
  32.              for(int i=1; i>=0; i++)
  33.              {
  34.                  cout<<rand()<<"錯誤!";
  35.                  cout<<rand()<<"(。凡。''')";
  36.                  cout<<rand()<<"orz";
  37.              }        
  38.         }  
  39.     system ("pause") ;
  40.     return 0;
  41. }
複製代碼
0勝^^
棒打老虎雞吃蟲
  1. #include <iostream>
  2. #include <cstdlib>;
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b, c;
  7.     cout<<"棒打老虎雞吃蟲"<<endl;
  8.     cout<<"(1):棒子, (2):老虎,  (3):雞, (4):蟲 "<<endl;
  9.     cin>>a;
  10.    
  11.         if(a==1)
  12.              cout<<"你出棒子"<<endl;
  13.         else if(a==2)
  14.              cout<<"你出老虎"<<endl;  
  15.         else if(a==3)
  16.              cout<<"你出雞"<<endl;
  17.         else if(a==4)
  18.              cout<<"你出蟲"<<endl;
  19.         else
  20.         {
  21.              for(int i=1; i>=0; i++)
  22.              {
  23.                  cout<<"錯誤!";
  24.                  cout<<"(。凡。''')";
  25.                  cout<<"orz";
  26.              }        
  27.         }  
  28.                     
  29.     srand(time(NULL));
  30.     b=rand()%4+1;
  31.         if(b==1)
  32.              cout<<"電腦出棒子"<<endl;
  33.         else if(b==2)
  34.              cout<<"電腦出老虎"<<endl;  
  35.         else if(b==3)
  36.              cout<<"電腦出雞"<<endl;
  37.         else if(b==4)
  38.              cout<<"電腦出蟲"<<endl;
  39.             
  40.         if(a==b)
  41.              cout<<"平手"<<endl;
  42.         else if(b>a && b-a==2)
  43.              cout<<"平手"<<endl;
  44.         else if(b>a && b-a==1)
  45.              cout<<"you win"<<endl;
  46.         else if(a>b && a-b==3)
  47.              cout<<"you win"<<endl;
  48.         else
  49.              cout<<"you lose"<<endl;

  50.     system ("pause") ;
  51.     return 0;
  52. }
複製代碼
計算:平手機率 1/2 贏(輸)機率 各1/4
統計:15勝,34敗,26平手
電腦是作弊嗎
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

因為卡關很久,自己寫出來有種小小的成就感.原本還打算放上來直接問老師的...
(咳咳)總之--

要感謝靈感大神
不然我絕對寫不出來
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer=rand()%3+1;
  8.     cout<<"請出拳! (1)剪刀  (2)石頭  (3)布"<<"\n";
  9.     cin>>player;
  10.     if(player==1 && computer==3)
  11.     {
  12.         cout<<"你出剪刀"<<endl<<"電腦出布"<<endl<<"YOU WIN!"<<endl;
  13.     }
  14.     else if(player==2 && computer==1)
  15.     {
  16.         cout<<"你出石頭"<<endl<<"電腦出剪刀"<<endl<<"YOU WIN!"<<endl;
  17.     }
  18.     else if(player==3 && computer==2)
  19.     {
  20.         cout<<"你出布"<<endl<<"電腦出石頭"<<endl<<"YOU WIN!"<<endl;
  21.     }
  22.     else if(player==1 && computer==1)
  23.     {
  24.         cout<<"你出剪刀"<<endl<<"電腦出剪刀"<<endl<<"平手!"<<endl;
  25.     }
  26.     else if(player==2 && computer==2)
  27.     {
  28.         cout<<"你出石頭"<<endl<<"電腦出石頭"<<endl<<"平手!"<<endl;
  29.     }
  30.     else if(player==3&& computer==3)
  31.     {
  32.         cout<<"你出布"<<endl<<"電腦出布"<<endl<<"平手!"<<endl;
  33.     }
  34.     else if(player==3 && computer==1)
  35.     {
  36.         cout<<"你出布"<<endl<<"電腦出剪刀"<<endl<<"YOU LOSE!"<<endl;
  37.     }
  38.     else if(player==1 && computer==2)
  39.     {
  40.         cout<<"你出剪刀"<<endl<<"電腦出石頭"<<endl<<"YOU LOSE!"<<endl;
  41.     }
  42.     else
  43.     {
  44.         cout<<"你出石頭"<<endl<<"電腦出布"<<endl<<"YOU LOSE!"<<endl;
  45.     }
  46.     system("pause");
  47.     return 0;
  48. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int game, com;
  8.     cout<<"請出拳! <1>剪刀 <2>石頭 <3>布   ";
  9.     cin>>game;

  10.     if(game==1)
  11.         cout<<"你出剪刀!"<<endl;
  12.     else if(game==2)
  13.         cout<<"你出石頭!"<<endl;   
  14.     else
  15.         cout<<"你出布!"<<endl;
  16.     com=rand()%3+1;
  17.     if(com==1)
  18.         cout<<"電腦出剪刀!"<<endl;
  19.     else if(com==2)
  20.         cout<<"電腦出石頭!"<<endl;   
  21.     else
  22.         cout<<"電腦出布!"<<endl;
  23.       
  24.     system("pause");
  25.     return 0;   
  26. }
複製代碼

TOP

返回列表