如何限制减量?

How to limit a decrement?

初始游戏难度为

game_difficulty=5   //Initial

每做对 3 次,难度就会上升到无穷大,但每做错 3 次,难度就会下降 ,但不会低于 5。因此,在这段代码中,例如:

if(user_words==words)   win_count+=1;
else()  incorrect_count+=1;


if(win_count%3==0) /*increase diff*/;
if(incorrect_count%3==0) /*decrease difficulty*/;

我应该怎么做?

您可以将其添加为条件:

if (user words==words) {
    win_count += 1;
    if (win_count %3 == 0) {
        ++diff;
    }
} else {
    incorrect_count += 1;
    if (incorrect_count % 3 == 0 && diff > 5) {
        --diff
    }
}

例如:

if(win_count%3==0) difficulty++;
if(incorrect_count%3==0 && difficulty > 5) difficulty--;

简单回答:

if(incorrect_count%3==0) difficulty = max(difficulty-1, 5);

但我个人会将其封装在一个小的 class 中,然后您可以包含所有逻辑并在进行时扩展它,例如:

class Difficulty
{
public:
    Difficulty() {};

    void AddWin()
    {
        m_IncorrectCount = 0; // reset because we got one right?

        if (++m_WinCount % 3)
        {
            m_WinCount = 0;
            ++m_CurrentDifficulty;
        }
    }

    void AddIncorrect()
    {
        m_WinCount = 0; // reset because we got one wrong?

        if (++m_IncorrectCount >= 3 && m_CurrentDifficulty > 5)
        {
            m_IncorrectCount = 0;
            --m_CurrentDifficulty;
        }
    }

    int GetDifficulty()
    {
        return m_CurrentDifficulty;
    }

private:
    int m_CurrentDifficulty = 5;
    int m_WinCount = 0;
    int m_IncorrectCount = 0;
};

这可以变成自定义数据类型的激励示例。

创建一个class将难度int包装为私有成员变量,并在public成员函数中确保所谓的contract 满足了。您最终会得到一个始终保证满足您的规格的值。这是一个例子:

class Difficulty
{
public:

    // initial values for a new Difficulty object:
    Difficulty() :
        right_answer_count(0),
        wrong_answer_count(0),
        value(5)
    {}

    // called when a right answer should be taken into account:
    void GotItRight()
    {
        ++right_answer_count;
        if (right_answer_count == 3)
        {
            right_answer_count = 0;
            ++value;
        }
    }

    // called when a wrong answer should be taken into account:
    void GotItWrong()
    {
        ++wrong_answer_count;
        if (wrong_answer_count == 3)
        {
            wrong_answer_count = 0;
            --value;
            if (value < 5)
            {
                value = 5;
            }
        }
    }

    // returns the value itself
    int Value() const
    {
        return value;
    }

private:
    int right_answer_count;
    int wrong_answer_count;
    int value;
};

下面是您将如何使用 class:

Difficulty game_difficulty;

// six right answers:
for (int count = 0; count < 6; ++count)
{
    game_difficulty.GotItRight();
}

// check wrapped value:
std::cout << game_difficulty.Value() << "\n";   

// three wrong answers:
for (int count = 0; count < 3; ++count)
{
    game_difficulty.GotItWrong();
}

// check wrapped value:
std::cout << game_difficulty.Value() << "\n";   

// one hundred wrong answers:
for (int count = 0; count < 100; ++count)
{
    game_difficulty.GotItWrong();
}

// check wrapped value:
std::cout << game_difficulty.Value() << "\n";   

输出:

7
6
5

一旦您牢牢掌握了此类类型的创建和使用方式,您就可以开始研究运算符重载,以便可以更像真实的 int 一样使用该类型,即 [=15] =]、- 等等。

How should I go about doing this?

您已将此问题标记为 C++。恕我直言,c++ 方法是创建一个 class 封装所有问题。

可能是这样的:

class GameDifficulty
{
public:
    GameDifficulty () : 
       game_difficulty (5), win_count(0), incorrect_count(0)
    {}

    ~GameDifficulty () {}

    void update(const T& words)
    {
       if(user words==words)   win_count+=1;
       else              incorrect_count+=1;

       // modify game_difficulty as you desire
       if(win_count%3 == 0)      
          game_difficulty += 1 ; // increase diff no upper limit

       if((incorrect_count%3 == 0) && (game_difficulty > 5)) 
          game_difficulty -= 1; //decrease diff;
    }

   inline int gameDifficulty() { return (game_difficulty); }
   // and any other access per needs of your game

private:
   int game_difficulty;
   int win_count;
   int incorrect_count;
}

// 注意 - 未编译或测试

用法为:

// instantiate
GameDiffculty  gameDifficulty;

// ...

// use update()
gameDifficulty.update(word);

// ...

// use access
gameDifficulty.gameDifficulty();

优点:封装

此代码位于一处,不会污染您代码中的其他地方。

您可以在这一个地方更改这些政策,而不会影响您的代码的其余部分。