游戏在if语句之后停止
Game Stops after If statement
我运行此代码,但是当我到达屏幕边缘时,它不允许我进入新的旅行方向而不是关闭控制台。/p>
class weapon
{
public:
weapon();
weapon(int x, int y);
int xPos;
int yPos;
};
weapon::weapon()
{
xPos = 0;
yPos = 0;
}
weapon::weapon(int x, int y)
{
xPos = x;
yPos = y;
}
struct Game
{
weapon Bow;
weapon Sword;
};
int main()
{
weapon * Bow = new weapon(4, 6); // how to add cout to this to show you have the weapon?
int xPos = 1;
int yPos = 1;
char input = '#';
while (xPos >= 1 && xPos <= 20 && yPos >= 1 && yPos <= 20)
{
cout << "Current x-coord = " << xPos << " Current y-coord = " << yPos << endl;
cout << "Which direction would you like to travel? Enter N, E, S or W" << endl;
cin >> input;
switch (input)
{
case 'E': case 'e':
++xPos;
break;
case 'W': case 'w':
--xPos;
break;
case 'N': case 'n':
++yPos;
break;
case 'S': case 's':
--yPos;
break;
}
if (xPos <= 0 || xPos >= 21 || yPos <= 0 || yPos >= 21)
{
cout << "There is a wall in the way!" << endl; //how do i make it continue the game after hitting a wall
cout << "Which direction would you like to travel? Enter N, E, S or W" << endl;
//cin >> input; // this whole section needs some fixing
}
}
return 0;
}
正确的解决方案应返回到WHIL循环,允许用户输入旅行的新输入方向。
一旦到达墙,while
的条件为false,循环退出。
我将进行修改条件而不是首先修改,然后检查它是否有效:
while (xPos >= 1 && xPos <= 20 && yPos >= 1 && yPos <= 20)
{
cout << "Current x-coord = " << xPos << " Current y-coord = " << yPos << endl;
cout << "Which direction would you like to travel? Enter N, E, S or W" << endl;
cin >> input;
switch (std::toupper(input))
{
case 'E':
if (xPos < 20)
++xPos;
else
cout << "There is a wall to the east!" << endl;
break;
case 'W':
if (xPos > 1)
--xPos;
else
cout << "There is a wall to the west!" << endl;
break;
// and the other two cases...
}
相关文章:
- Clang-格式:在多行语句之后的换行符上打开大括号
- 如果语句之后,返回vs无能为力
- 有没有办法在评论以外的 #include 之后编写语句
- 我的程序在SCANF语句之后不起作用
- 游戏在if语句之后停止
- 我的 for 循环之后的代码不起作用,for 循环后的打印语句不会被打印
- C :Bool方法在COUT语句之后返回意外数字
- 为什么在语句之后重置变量?(C )
- C :在控制语句之后,循环的行为不同
- 可以在 main 的返回 0 语句之后调用任何函数吗?
- 不会让我在 IF 语句(家庭作业)之后读取用户输入
- Boost.Log在每个日志记录语句之后刷新
- 如何在 switch 语句之后将变量添加到另一个变量
- C++函数中返回语句之后的指令
- 为什么我的第一个 if 语句有效,但我的 else if 语句在它之后不起作用
- 为什么return之后的语句会改变返回值?
- 在clang-format中控制语句之后中断
- "在返回语句之后' ; '预期,有人可以告诉我我的代码出了什么问题吗?
- 为什么在满足if语句之后,else语句仍然打印?
- Std::async保证在return语句之后运行