返回列表 發帖

猜拳遊戲(五)

解決平手時的BUG
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.     //請將遊戲改為三戰兩勝 並將贏的次數最多者輸出
  8.     // 第一局  我:1  電腦:0
  9.     // 第二局  我:1  電腦:1
  10.     // 第三局  我:2  電腦:1
  11.    
  12.    // 結果: 玩家贏
  13.    srand(time(NULL));

  14.    int player =0;
  15.    int com =0;
  16.    int mypoint = 0;
  17.    int compoint = 0;
  18.    int game = 1;
  19.    string mora[] = {"剪刀","石頭","布"};
  20.   
  21.    
  22.    for(int i=game;i<=3;i++) {
  23.       cout << "第" << i << "局" << "玩家: "<< mypoint << "電腦: " << compoint << endl;
  24.       cout << "=========================" << endl;
  25.       cout << endl;     
  26.       cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
  27.       cin >> player;
  28.       cout << "電腦出拳中..." << endl;
  29.       com = (rand()%3)+1;
  30.       cout << "你出:" << mora[player-1] << endl;
  31.       cout << "電腦出:" << mora[com-1] << endl;     
  32.       if(player == 1 && com==3)
  33.       {
  34.          cout << "你贏了" << endl;
  35.          mypoint++;
  36.       }
  37.       else if(player == 2 && com==1)
  38.       {
  39.          cout << "你贏了" << endl;
  40.          mypoint++;
  41.       }
  42.       else if(player == 3 && com==2)
  43.       {
  44.          cout << "你贏了" << endl;
  45.          mypoint++;
  46.       }
  47.       else if(player == com)
  48.       {
  49.         cout << "平手" << endl;
  50.       }
  51.       else
  52.       {
  53.        cout << "你輸了" << endl;
  54.        compoint++;
  55.       }
  56.       
  57.       if(mypoint == 2 || compoint == 2) {
  58.           break;   
  59.       }
  60.       cout << "=========================" << endl;
  61.       cout << endl;
  62.      
  63.    }
  64.    cout << "最後結果: " <<  "玩家: "<< mypoint << "電腦: " << compoint << endl;
  65.    if(mypoint > compoint) {
  66.     cout << "玩家贏" << endl;
  67.    }
  68.    else if(mypoint == compoint ) {
  69.     cout << "平手" << endl;
  70.    }
  71.    else {
  72.     cout << "電腦贏" << endl;
  73.    }

  74.    system("pause");
  75.    return 0;   
  76. }
複製代碼

返回列表