为什么我的代码在 3 轮游戏后无法正确切换玩家名称?

Why won't my code switch player names correctly after 3 rounds of play?

本文关键字:玩家 代码 我的 游戏 为什么      更新时间:2023-10-16

所以在我的课堂上,我不得不做一个Numberwang模拟游戏。一切正常,除了 2 轮后名称无法正确关联的事实。它应该说"第 3 轮,玩家 1 先玩",但它确实如此,但玩家 2 作为第一个玩的人出现。

# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;

bool numberwang(int n)
{
    if(n < 100 ){
        return 1;
    } else {
        return 0;
    }
}
int main()
{
    string Firstplayer, Otherplayer;
    int rounds;
    int  counter = 1;
    int number;
    int win = 18;
    int lose= 1;
    cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
    cout << "What is player 1's name? ";
    cin >> Firstplayer;
    cout << "What is player 2's name? ";
    cin >> Otherplayer;
    cout << "How many rounds? ";
    cin >> rounds;
    cout << "Well, if you're ready, lets play Numberwang!" << endl;

    while(counter <= rounds){
        cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
        while(true){
            cout << Firstplayer << ": ";
            cin >> number;
            if(numberwang(number)){
                counter++;
                if(counter > rounds){
                    cout << "That's Numberwang!" << endl;
                    cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose <<  endl;
                    break;
                }
                cout << "That's Numberwang!" << endl;
                swap(Firstplayer, Otherplayer);
                cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
            }
            cout << Otherplayer << ": ";
            cin >> number;
            if(numberwang(number)){
                counter++;
                if(counter > rounds){
                    cout << "That's Numberwang!" << endl;
                    cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose <<  endl;
                    break;
                }
                cout << "That's Numberwang!" << endl;
                swap(Firstplayer, Otherplayer);
                cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
            }
        }
    }
    return 0;
}

在你的if语句(第61行)之后,你说"Firstplayer",然后你输出"Otherplayer"。名称不匹配。

块引用

cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
        }
        cout << Otherplayer << ": ";
        cin >> number;