如何限制减量?

时间:2016-03-05 16:01:45

标签: c++ conditional procedural-programming

最初的游戏难度是

game_difficulty=5   //Initial

每3次如果你做对了,你的难度会上升到无穷大,但每3次你弄错了,你的难度会下降但不会低于5 。因此,在此代码中为ex:

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

我应该怎么做呢?

5 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

例如:

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

答案 2 :(得分:0)

简单回答:

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

答案 3 :(得分:0)

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

创建一个将难度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,即使用+-等等。

答案 4 :(得分:0)

  

我该怎么做呢?

您已将此问题标记为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;
}

// note - 未编译或测试

用法是:

// instantiate
GameDiffculty  gameDifficulty;

// ...

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

// ...

// use access
gameDifficulty.gameDifficulty();

优势:封装

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

您可以在这一处更改这些政策,而不会影响其他代码。