返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.    srand(time(NULL));

  8.    int player =0;
  9.    int com =0;
  10.    int mypoint=0;
  11.    int compoint=0;
  12.    int game=1;
  13.    string mora[] = {"剪刀","石頭","布"};
  14.    for(int i=game;i<=3;i++)
  15.    {
  16.    cout<<"第"<<i<<"局"<<"玩家"<< mypoint<<"電腦"<<compoint<<endl;
  17.   
  18.    
  19.    cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
  20.    cin >> player;
  21.    cout << "電腦出拳中..." << endl;
  22.    com = (rand()%3)+1;
  23.    cout << "你出:" << mora[player-1] << endl;
  24.    cout << "電腦出:" << mora[com-1] << endl;
  25.    
  26.    if(player == 1 && com==3) {
  27.      cout << "你贏了" << endl;
  28.      mypoint++;
  29.    }
  30.    else if(player == 2 && com==1) {
  31.      cout << "你贏了" << endl;
  32.      mypoint++;
  33.    }
  34.    else if(player == 3 && com==2) {
  35.      cout << "你贏了" << endl;
  36.      mypoint++;
  37.    }
  38.    else if(player == com) {
  39.     cout << "平手" << endl;
  40.    
  41.    }
  42.    else {
  43.     cout << "你輸了" << endl;
  44.     compoint++;
  45.    }
  46.    if(mypoint==2||compoint==2){  
  47.    break;                             
  48.    }
  49. }

  50.    cout<<"最後結果:"<<"玩家:"<<mypoint<<"電腦:"<<compoint<<endl;
  51.    if(mypoint>compoint){
  52.     cout<<"玩家贏"<<endl;
  53.    
  54.    
  55.     else if(mypoint=compoint)
  56.     cout<<"平手"<<endl;                                             
  57.    }
  58.    
  59.    
  60.    else{
  61.     cout<<"電腦贏"<<endl;  
  62.    }
複製代碼

TOP

返回列表