c++ NIM游戏逻辑错误
C++ NIM game logical error
**显示不正常,主板内实现时显示不正常
这是NIM游戏的更新代码<<获胜者功能和显示功能不能正常工作**我需要帮助弄清楚如何让显示功能正常工作,请。
class cboard{
private:
int sticks;
bool player;
public:
//default constructor
cboard(){sticks=10;}
//constructor with parameter given
cboard(int x){sticks=x;}
//stick set
void setstick(int m){sticks-=m;}
//display stick getter
int get_sticks(){return sticks;}
//display function, a.k.a baord getter
void displaycboard ()
{for (int i=sticks; i>0; i--)
{cout<<"| ";}
cout<<endl;}
};
您的displaycboard()
直接修改类成员变量sticks
,在第一个displaycboard()
之后将其减少为0。下次调用displaycboard()
时,成员变量sticks
为0,这意味着没有什么要打印的。
您的问题在于displaycboard()
。for
循环需要更改一个非sticks
的变量。
将代码改为
for (int i = sticks; i > 0; i--)
{
...
}
,它工作得很好。它仍然输出两行,因为在循环中被反复调用(因为循环迭代,并且在第一次循环结束时调用相同数量的棍棒,第二个循环开始)。
编辑:参考关于您的goto BAD
的评论,您可以将其替换为
int s = 0;
while (s <= 2)
{
cout << "How many... greater than 2" << endl;
cin >> s;
}
第二次编辑:您的评论反映出您知道这是不安全的,但您可以使用stringstream (#include <sstream>
)和
std::string instring;
while (s <= 2)
{
cout<<"How many sticks would you like to play with this game (Greater than 2): ";
getline(cin, instring);
stringstream(instring) >> s;
}
这将允许您输入1.25
之类的东西,并让程序要求一个新值,或5.25
,并让它玩5棒。请注意,5.99
仍然给出5棒…
相关文章:
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我的游戏引擎的 spdlog 日志记录出现奇怪的"unresolved external symbol"错误
- C++ 中连接 4 游戏的输入逻辑错误
- Visual Studio 中的 lcr 游戏 c++ LINK 2019 错误
- 游戏引擎 SFML C++错误
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- C++随机猜数游戏错误
- 井字游戏中的错误
- 我正在尝试安装游戏引擎,但遇到错误
- 致命错误LNK1169:在C++Visual Studio中编写战舰游戏时发现的一个或多个多重定义的符号
- C++ 抛出读取访问冲突错误,但我不确定为什么。平铺滑块益智游戏
- 如何使用运行时检查错误来修复我的C 格斗游戏
- 我在使用C 的Hangman游戏中遇到错误
- C TIC TAC TOE游戏.开关空间而不是错误消息
- 我正在尝试学习使用 SDL 制作游戏。我的书教SDL1.2.3,我无法让它在Visual Studio上工作。谁能帮忙?我的错误:
- C 猜测游戏错误与某事
- 在Windows 8上设置COCOS 2DX游戏开发环境中的错误
- 错误 C2512 和 C3892 - 像游戏一样构建吃豆人
- C++强力球 - 文件拆分和错误(游戏类型)