C++ 如何限制减量?
最初的游戏难度是C++ 如何限制减量?,c++,conditional,procedural-programming,C++,Conditional,Procedural Programming,最初的游戏难度是 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 difficu
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 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;
};
这可以成为定制数据类型的激励示例 创建一个类,将难度
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;
};
一旦您掌握了这些类型是如何创建和使用的,您就可以开始研究运算符重载,以便该类型可以像真正的int
一样使用,即使用+
,-
等等
我该怎么做呢
<>你把这个问题标记为C++。iMHO 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();
优点:封装
此代码位于一个地方,不会污染代码中的其他地方
你可以在这里更改这些政策,对代码的其余部分没有影响。使用
if
子句,以确保在递减之前难度不会太低。@MattiVirkkunen这是一个很好的建议,我考虑过它,但我的整个程序有很多条件,我正试图尽可能减少条件,这样逻辑就不会太复杂太令人困惑了。想知道是否还有其他方法!谢谢你的回答!:)在本例中,使用while(n-->最小值),因为语法有效,但更混乱。如果您确信游戏难度>5-=1 : 5;看起来你比我快了!:)那…那真是无中生有。真的没有必要上课。谢谢你的回答!我是编程新手,刚开始学习基本的东西,所以我对类的用法不太了解。但这个概念肯定是可行的。
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();