如何限制递减

How to limit a decrement?

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

初始游戏难度为

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(incorrect_count%3==0) difficulty = max(difficulty-1, 5);

但就我个人而言,我会把它包装成一个小类,然后你可以包含所有的逻辑,并随着你的进行而扩展它,比如:

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;
};

您可以将其添加为条件:

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--;

这可以转换为自定义数据类型的激励示例。

创建一个类,将难度int包装为私有成员变量,并在公共成员函数中确保满足所谓的契约。您最终将得到一个始终保证符合您规格的值。下面是一个示例:

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;
};

以下是您将如何使用该类:

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使用,即与+-等一起使用。

我应该怎么做?

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

也许像这样:

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();

优点:封装

此代码位于一个位置,不会污染代码中的其他位置。

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