返回列表 發帖
  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.    
  17.    cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
  18.    cin >> player;
  19.    cout << "電腦出拳中..." << endl;
  20.    com = (rand()%3)+1;
  21.    cout << "你出:" << mora[player-1] << endl;
  22.    cout << "電腦出:" << mora[com-1] << endl;
  23.    
  24.    if(player == 1 && com==3) {
  25.      cout << "你贏了" << endl;
  26.      mypoint++;
  27.    }
  28.    else if(player == 2 && com==1) {
  29.      cout << "你贏了" << endl;
  30.      mypoint++;
  31.    }
  32.    else if(player == 3 && com==2) {
  33.      cout << "你贏了" << endl;
  34.      mypoint++;
  35.    }
  36.    else if(player == com) {
  37.     cout << "平手" << endl;
  38.    
  39.    }
  40.    else {
  41.     cout << "你輸了" << endl;
  42.     compoint++;
  43.    }
  44.    if(mypoint==2||compoint==2){  
  45.    break;                             
  46.    }
  47. }

  48.    if(mypoint>compoint){
  49.     cout<<"玩家贏"<<endl;
  50.                                                   
  51.    }
  52.    else{
  53.     cout<<"電腦贏"<<endl;  
  54.    }

  55.    system("pause");
  56.    return 0;  
  57. }
複製代碼

TOP

返回列表