c++ - 我不明白为什么这个值在一个函数中计算而不是另一个

标签 c++ function class

大家好,我是新手,我从事这个 Blackjack 程序已经有一段时间了,我一直在尝试制作像这样的简单游戏来练习。我快完成了,但我遇到了一个让我整晚发疯的小问题。有一个 Game、Player、Deck 和 Card 类。我会尝试只发布相关代码,我真的不想让它冗长且难以阅读。我认为 Game 类中的这些功能可以解释问题。

游戏.cpp:

void Game::createPlayers(int p) //creates p players and stores in players vector
{
    for (int x = 0; x < p; x++)
    players.push_back(Player(x + 1));
}

void Game::startRound() //deals 2 cards to each player and prints their hand
{

    dealer.hit(deck.draw());
    dealer.hit(deck.draw());
    cout << "Dealer's hand: " << endl << "Face down card" << endl;

    dealer.printDealer();

    for (int x = 0; x < players.size(); x++)
    {
        players[x].hit(deck.draw());
        players[x].hit(deck.draw());

        players[x].calcTotal();
        cout << "Total: " << players[x].getTotal() << endl;
        cout << "Player " << players[x].getSeat() << "'s hand:" << endl;
        players[x].printHand();
    }
}

void Game::playerChoice(Player x) //while user enters 1, keeps adding cards to hand
{
    int action = 0;
    x.calcTotal();
    if (x.getTotal() == 21)
    {
        cout << "Player " << x.getSeat() << " has a blackjack!" << endl;
        return;
    }
    else
    {
        cout << "Player " << x.getSeat() << "'s turn: 1 to hit, 2 to stay: ";
        cin >> action;
    }

    while (action == 1)
    {
        cout << endl;
        x.hit(deck.draw());
        x.calcTotal();
        x.printHand();

        if (x.getTotal() == 0)
        {
            cout << "Player " << x.getSeat() << " has busted\n\n";
            action = 2;
        }

        if (action == 1)
        {
            cout << "Enter 1 to hit again, 2 to stay: ";
            cin >> action;
        }
        cout << endl;
    }

    cout << "Player " << x.getSeat() << "'s total: " << x.getTotal() << endl;
}

void Game::collectWinnings() //compares the player's hand total to the dealer's
{
    for (int i = 0; i < players.size(); i++)
    {
            players[i].calcTotal();
        if (players[i].getTotal() > dealer.getTotal())
        {
            cout << "Player " << players[i].getSeat() << " wins." << endl;
                players[i].winBet();
        }
        else if (players[i].getTotal() < dealer.getTotal())
        {
            cout << "Player " << players[i].getSeat() << " loses." << endl;
            players[i].loseBet();
        }
        else
            cout << "Player " << players[i].getSeat() << " ties." << endl;
    }
}

如果太乱或太多了,真的很抱歉。我的问题是,在 collectWinnings() 中,当我使用 calcTotal() 然后使用 getTotal() 时,它始终只返回它们在 startRound() 中给出的最初两张牌的总数。但是,如果我使用 calcTotal() 然后在 playerChoice() 末尾使用 getTotal(),它会返回整手牌的总数。当我在 collectWinnings() 中这样做时,不知何故它并没有计算整手牌。是否有一个原因?我是否正在尝试做 C 不允许的事情?

最佳答案

Game::playerChoice(Player x) 中,您按值传递 Player x。换句话说,在函数执行期间会创建一个本地拷贝。当控制权返回给调用者时,更改(例如添加到玩家牌组的新牌)将丢失,因为它们不是在原始 Player 实例(第一个存储在 startRound()collectWinnings() 中使用的 players vector 中。

为避免此问题,您可以通过引用传递播放器:

void Game::playerChoice(Player & x)
//                            ^^^

x 所做的更改随后会在调用方范围内的原始 Player 实例上进行。因此,如果您在以下内容中调用 playerChoice:

void Game::allPlayersChoice()
{
  for (int x = 0; x < players.size(); x++)
  {
    playerChoice(players[x]);
  }
}

然后 Game 对象的 Player 实例 players[x] 将被更新。由于 collectWinnings() 也使用那些相同 players[x] 实例,所以调用 players[i].calcTotal() 中的 collectWinnings() 然后将在更新的玩家上完成,这将占整手牌。

关于c++ - 我不明白为什么这个值在一个函数中计算而不是另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306101/

相关文章:

c++ - 使用 std::function 中函数的类型来声明该类型的多个函数

c# - 如何使用 MEF 导出和导入函数并执行它们?

list - Python函数输出错误,检查索引以查看值?

MySQL UDF/存储函数无法识别作为参数传递的表名

ios - Swift:在 ViewController 的初始化例程中使用数据模型类结构

c++ - 构造函数中的 "my_constructor : variable(x)"和 "this.variable = x"有区别吗?

c++ - 内部结构/类声明是否自动成为嵌套类的友元?

c++ - 没有可用的源 "libstdc++-6!_ZNSo9_M_insertIlEERSoT_() at 0x6fc868a8"

C++ 函数指针和类

c++ - 子类递归方法调用