值未正确递增

Value isn't incrementing properly

本文关键字:      更新时间:2023-10-16

此应用程序的目的是模拟许多骰子游戏。我有另一个版本,它玩一个游戏,要求用户输入并输出信息。这个版本的目的是只显示10000场比赛后的结果。结果是房子赢了多少场比赛,玩家赢了多少比赛,以及每场比赛的平均掷骰子数。我还没有实现滚动,因为我想先让游戏正确地递增。

当我执行这项操作时,会发生一堵数字墙(这是由于cout<<playerwintotal;),这是故意的,但数字会重复3-4次,直到循环执行了10000次。

即。1 1 1 2 2 2 3 3 3 4 4 5 5 5等

最终结果通常是这样的:

10000次掷骰子后:
玩家赢得了2502场比赛
众议院赢得了3625场比赛。

我真的不知道如何解决这个问题,尽管这只是我学习C++的第四天,但据我所知,一切都是应该的。

#include <iostream>
#include <string>
#include "randgen.h"
using namespace std;
const int MAX_PLAYS = 10000;
int main() {
    int roll;
    RandGen rg;
    int die1 = rg(6) + 1;
    int die2 = rg(6) + 1;
    int point;
    int total = die1 + die2;
    bool playerwin;
    bool housewin;
    int playerwintotal = 0;
    int housewintotal = 0;
    for (int i = 0; i < MAX_PLAYS; ++i) {
        roll = 1;
        if (roll == 1 && (total == 7 || total == 11)) {
            playerwin = true;
            ++playerwintotal;
        }
        if (roll == 1 && (total == 2 || total == 3 || total == 12)) {
            housewin = true;
            ++housewintotal;
        }
        if (roll == 1 && (total != 2 || total != 3 || total != 12)) {
            point = total;
            playerwin = false;
            housewin = false;
        }
        die1 = rg(6) + 1;
        die2 = rg(6) + 1;
        total = die1 + die2;
        ++roll;
        if (total == point) {
            playerwin = true;
            ++playerwintotal;
        }
        if (total == 7) {
            housewin = true;
            ++housewintotal;
        }
        cout << playerwintotal;
    }
    cout << "After " << MAX_PLAYS << " games of craps:n" << "Player won "
            << playerwintotal << " timesn" << "The house won " << housewintotal
            << " timesn";
    return 0;
}
total != 2 || total != 3 || total != 12

总是正确的。你可能是指

total != 2 && total != 3 && total != 12

数字在重复,因为当房子获胜或无人获胜时,playerwintotal保持不变,因此重复。也许你想做:

cout << "Turn: " << i+1 << " Player wins: " << playerwintotal << ' ';

此外,正如Sebastian在回答中指出的那样,"否"不是一个好主意,所以一定要给他赞成票。

但数字是的3-4倍

它们应该是——你不打印当前游戏的数字,而是打印你的玩家获胜的次数(他不是每次都这样)。

相关文章:
  • 没有找到相关文章