返回列表 發帖

猜拳遊戲 (二)

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

以陣列存放 "剪刀" "石頭" "布", 重寫上一個程式.

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

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));   
  7.     int p,c;
  8.     string name[3]={"刀","石","網"};
  9.     cout<<"請出拳! (1)刀(2)石(3)網 ";
  10.     cin>>p;
  11.         cout<<"你出"<<name[p-1]<<endl;
  12.     c=rand()%3+1;
  13.         cout<<"電腦出"<<name[p-1]<<endl;     
  14.    system("pause");
  15.    return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int game, com;
  8.     string name[3]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! <1>剪刀 <2>石頭 <3>布  ";
  10.     cin>>game;
  11.     cout<<"你出"<<name[game-1]<<"!"<<endl;
  12.     com=rand()%3+1;
  13.     cout<<"電腦出"<<name[com-1]<<"!"<<endl;
  14.     system("pause");
  15.     return 0;   
  16. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player, computer;
  8.     string name[3]={"剪刀","石頭","布"};   //name[0]  name[1]  name[2]
  9.     cout<<"請出拳! (1)剪刀 (2)石頭 (3)布  ";
  10.     cin>>player;
  11.     cout<<"你出"<<name[player-1]<<"!"<<endl;
  12.     computer=rand()%3+1;
  13.     cout<<"電腦出"<<name[computer-1]<<"!"<<endl;
  14.     system("pause");
  15.     return 0;   
  16. }
複製代碼

TOP

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

TOP

  1. #include <iostream>
  2. #include <cstdlib>;
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int a, b, c;
  8.     re:
  9.     string name[3]={"剪刀","石頭","布"};
  10.     cout<<"請輸入你出的拳"<<endl<<"(1)剪刀:, (2)石頭:, (3)布:"<<endl;
  11.     cin>>a;
  12.              cout<<"你出"<<name[a-1]<<endl;
  13.         if(a>0 && 4<0 )
  14.         {  
  15.              for(int i=1; i>=0; i++)
  16.                  cout<<rand()<<"錯誤!   (。凡。''')   orz";  
  17.         }         
  18.     b=rand()%3+1;
  19.              cout<<"電腦出"<<name[b-1]<<endl;      
  20.         if(a==b)
  21.              cout<<"平手"<<endl;
  22.         else if(a-b==1)
  23.              cout<<"you win"<<endl;
  24.         else if(b-a==2)
  25.              cout<<"you win"<<endl;
  26.         else
  27.              cout<<"you lose"<<endl;
  28.     goto re;
  29.     system ("pause") ;
  30.     return 0;
  31. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

返回列表