返回列表 發帖

[隨堂測驗] 猜拳遊戲 (三)

本帖最後由 陳品肇 於 2022-3-5 10:09 編輯

延續 猜拳遊戲 (二) 的練習,
完成:  1. 判斷輸贏  2. 加上 goto 敘述使可重複遊戲
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int player=0,computer=0;
  8.     string go[3]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)"+go[0]+"(2)"+go[1]+"(3)"+go[2]+":";
  10.     cin>>player;
  11.     cout<<"你出"+go[player-1]+"!"<<endl;
  12.     srand(time(NULL));
  13.     computer=rand()%3+1;
  14.     cout<<"電腦"+go[computer-1]+"!"<<endl;
  15.     if(player == computer)
  16.     {
  17.         cout<<"平手!"<<endl<<endl;         
  18.     }else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.     {
  20.         cout<<"你贏了!"<<endl<<endl;     
  21.     }else
  22.     {
  23.         cout<<"電腦贏了!"<<endl<<endl;   
  24.     }goto re;
  25.     system("pause");   
  26.     return 0;
  27. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.         re:
  8.     int x, z;
  9.     string meth[3]= {"剪刀", "石頭", "布"};
  10.     cout<<"請出拳! (1)"+meth[0]+" (2)"+meth[1]+" (3)"+meth[2]+": ";
  11.     cin>>x;
  12.     if(x>3||x<1){
  13.            cout<<"輸入錯誤"<<endl;
  14.         goto re;   
  15.     }
  16.     cout<<"你出"+meth[x-1]+"!"<<endl;
  17.     srand(time(NULL));
  18.     z = rand()%3+1;   
  19.     cout<<"電腦出"+meth[z-1]+"!"<<endl;
  20.     if(x==z)
  21.     {
  22.             cout<<"平手"<<endl;
  23.         }else if((x==1 && z==3)||(x==2 && z==1)||(x==3 && z==2))
  24.         {
  25.                 cout<<"你贏了!"<<endl;
  26.         }else
  27.         {
  28.                 cout<<"電腦贏了!"<<endl;
  29.         }
  30.     goto re;
  31.     system("pause");
  32.     return 0;
  33. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string meth[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    if(player==1||player==2||player==3)
  14.    {}else
  15.    {
  16.            cout<<"輸入錯誤"<<endl;
  17.         goto re;
  18.    }
  19.    computer=rand()%3+1;
  20.    cout<<"你出"<<meth[player-1]<<endl;
  21.    cout<<"電腦出"<<meth[computer-1]<<endl;
  22.    if(player==computer)
  23.        cout<<"平手!"<<endl;  
  24.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  25.        cout<<"你贏了!"<<endl<<endl;
  26.    else
  27.        cout<<"電腦贏了!"<<endl<<endl;
  28.    goto re;
  29.    system("pause");   
  30.    return 0;
  31. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     re:
  8.     int x,y;
  9.     string meth[3]= {"剪刀", "石頭", "布"};
  10.     cout<<"請出拳! (1)"+meth[0]+" (2)"+meth[1]+" (3)"+meth[2]+": ";
  11.     cin>>x;
  12.    if(x>3||x<1){
  13.            cout<<"輸入錯誤"<<endl;
  14.         goto re;   
  15.     }
  16.     cout<<"你出"+meth[x-1]+"";
  17.     srand(time(NULL));   
  18.     y=rand()%3+1;
  19.     cout<<"電腦出"+meth[y-1]<<endl;
  20.     if(y==x)
  21.     cout<<"平手"<<endl;
  22.     else if((x==1 && y==3)||(x==2&&y==1)||(x==3&&y==2))
  23.         cout<<"你贏了!"<<endl;
  24.     else{      
  25.         cout<<"電腦贏了"<<endl;
  26.     }
  27.     goto re;
  28.     system("pause");   
  29.     return 0;
  30. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.    re:
  8.             srand(time(NULL));
  9.     int player, computer;
  10.      string meth[3]={"剪刀","石頭","布"};
  11.      
  12.     cout<<"請出拳!   (1)"+meth[0]+"(2)"+meth[1]+"(3)"+meth[2]+":  ";
  13.     cin>>player;
  14.     computer=rand()%3+1;
  15.     cout<<"你出"<<meth[player-1]<<"!"<<endl;
  16.     srand(time(NULL));
  17.      computer = rand()%3+1;
  18.     cout<<"電腦出"+meth[computer-1]+"!"<<endl;
  19.     if(computer==player)
  20.     cout<<"平手"<<endl;
  21.     else if((player==1 && computer==3)||(player==2&&computer==1)||(player==3&&computer==2))
  22.                         cout<<"你贏了!"<<endl;
  23.         else{       
  24.         cout<<"電腦贏了"<<endl;
  25.                 }
  26.    
  27.     goto re;
  28.     system("pause");   
  29.     return 0;
  30. }
複製代碼

TOP

本帖最後由 田家齊 於 2022-2-26 12:02 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     re:
  8.   int a,b;
  9.   srand(time(NULL));
  10.   string x[3]={"剪刀","石頭","布"};
  11.   cout<<"請出拳(1)"+x[0]+"(2)"+x[1]+"(3)"+x[2]+":";
  12.   cin>>a;
  13.   b=rand()%3+1;
  14.   cout<<"你出"+x[a-1]+"!"<<endl;
  15.   cout<<"電腦出"+x[b-1]+"!"<<endl;
  16.   if(a==b)
  17.   {
  18.     cout<<"平手"<<endl;     
  19.   } else if(
  20.   (a==1&&b==3)|| (a==2&&b==1)|| (a==3&&b==2)
  21.   )
  22.   {
  23.     cout<<"玩家贏"<<endl;      
  24.   } else
  25.   {
  26.     cout<<"電腦贏"<<endl;   
  27.   }
  28.   goto re;
  29. system("pause");
  30. return 0;
  31. }  
複製代碼

TOP

本帖最後由 許宸瑀 於 2022-2-26 11:40 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    re:
  8.    srand(time(NULL));                  
  9.    int player,computer;
  10.    string n[]={"剪刀","石頭","布"};
  11.    cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
  12.    cin>>player;
  13.    computer=rand()%3+1;
  14.    cout<<"你出"<<n[player-1]<<endl;
  15.    cout<<"電腦出"<<n[computer-1]<<endl;
  16.    if(player==computer)
  17.        cout<<"平手!"<<endl;  
  18.    else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
  19.        cout<<"你贏了!"<<endl;
  20.    else
  21.        cout<<"電腦贏了!"<<endl;
  22.    cout<<endl;
  23.    goto re;
  24.    system("pause");   
  25.    return 0;
  26. }
複製代碼
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.    
  8.     int player, computer;
  9.     string meth[3]= {"剪刀", "石頭", "布"};
  10.     cout<<"請出拳! (1)"+meth[0]+" (2)"+meth[1]+" (3)"+meth[2]+": ";

  11.     re:

  12.     cin>>player;
  13.     cout<<"你出"+meth[player-1]+"!"<<endl;
  14.     srand(time(NULL));
  15.     computer = rand()%3+1;   
  16.     cout<<"電腦"+meth[computer-1]+"!"<<endl;
  17.    
  18.     goto  re;
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

返回列表