解決平手時的BUG- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
-
- //請將遊戲改為三戰兩勝 並將贏的次數最多者輸出
- // 第一局 我:1 電腦:0
- // 第二局 我:1 電腦:1
- // 第三局 我:2 電腦:1
-
- // 結果: 玩家贏
- srand(time(NULL));
- int player =0;
- int com =0;
- int mypoint = 0;
- int compoint = 0;
- int game = 1;
- string mora[] = {"剪刀","石頭","布"};
-
-
- for(int i=game;i<=3;i++) {
- cout << "第" << i << "局" << "玩家: "<< mypoint << "電腦: " << compoint << endl;
- cout << "=========================" << endl;
- cout << endl;
- cout << "請選擇想要出的拳 (1:剪刀 2:石頭 3:布)"<< endl;
- cin >> player;
- cout << "電腦出拳中..." << endl;
- com = (rand()%3)+1;
- cout << "你出:" << mora[player-1] << endl;
- cout << "電腦出:" << mora[com-1] << endl;
- if(player == 1 && com==3)
- {
- cout << "你贏了" << endl;
- mypoint++;
- }
- else if(player == 2 && com==1)
- {
- cout << "你贏了" << endl;
- mypoint++;
- }
- else if(player == 3 && com==2)
- {
- cout << "你贏了" << endl;
- mypoint++;
- }
- else if(player == com)
- {
- cout << "平手" << endl;
- }
- else
- {
- cout << "你輸了" << endl;
- compoint++;
- }
-
- if(mypoint == 2 || compoint == 2) {
- break;
- }
- cout << "=========================" << endl;
- cout << endl;
-
- }
- cout << "最後結果: " << "玩家: "<< mypoint << "電腦: " << compoint << endl;
- if(mypoint > compoint) {
- cout << "玩家贏" << endl;
- }
- else if(mypoint == compoint ) {
- cout << "平手" << endl;
- }
- else {
- cout << "電腦贏" << endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |