[英]I would like to ignore a user's input if they go out-of-bounds
I tried looking for an answer to my question but couldn't find anything that was quite right. 我试着寻找我的问题的答案,但找不到任何正确的答案。
My problem deals with having an array set up and basically moving the little cursor around a field of dots. 我的问题涉及设置一个数组并基本上将小光标移动到一个点的字段。 I need to be able to ignore the user's input if they hit an arrow key in a direction that would take them out of bounds of the grid.
我需要能够忽略用户的输入,如果他们按箭头键的方向将他们超出网格范围。 I'm not exactly sure what would best do this.
我不确定最好的做法是什么。
This is the class that I cannot modify. 这是我无法修改的类。 I can only inherit from it to do this.
我只能继承它来做到这一点。
//You can add or delete includes
#include <iostream>
#include <stdlib.h> //For system()
#include <conio.h> //For getche()
#include <time.h>
using namespace std;
const int MAX_HEIGHT = 20; //The height of the grid
const int MAX_WIDTH = 40; //The width of the grid
class PickUpGame
{
protected:
char Screen[MAX_HEIGHT][MAX_WIDTH]; //The grid to print to the screen
int xPos, yPos; //The current x and y position of the users cursor on the grid
public:
//Constructor that will intialize the screen and x and y positions
PickUpGame() : xPos(0), yPos(MAX_WIDTH - 1)
{
SetupScreen(); //Initalize the grid
}
//Initialize the screen with all '.' characters and set the intial user cursor position on the grid
void SetupScreen()
{
for(int height = 0; height < MAX_HEIGHT; height++) {
for(int width = 0; width < MAX_WIDTH; width++) {
Screen[height][width] = '.'; //Initialize each grid position
}
}
Screen[xPos][yPos] = '<'; //Set the users initial cursor position
}
//Print the grid to the screen
void Print()
{
for(int height = 0; height < MAX_HEIGHT; height++) {
for(int width = 0; width < MAX_WIDTH; width++) {
cout << Screen[height][width]; //Print the character at this location in the grid
}
cout << endl; //After each row is printed, print a newline character
}
}
//Take in user input to move around the grid
void Move(char Direction)
{
switch(static_cast<int>(Direction)) //Don't know the ASCII characters for the arrow keys so use the ASCII numbers
{
case 72: //Up arrow
Screen[xPos][yPos] = ' '; //Wipe out the users current cursor
xPos--; //Move the users x position on the grid
Screen[xPos][yPos] = '^'; //Move the users cursor
break;
case 80: //Down arrow
Screen[xPos][yPos] = ' ';
xPos++;
Screen[xPos][yPos] = 'V';
break;
case 75: //Left arrow
Screen[xPos][yPos] = ' ';
yPos--;
Screen[xPos][yPos] = '<';
break;
case 77: //Right arrow
Screen[xPos][yPos] = ' ';
yPos++;
Screen[xPos][yPos] = '>';
break;
}
}
}; };
You simply need to check whether a movement would take them outside the bounds and ignore it if so. 你只需要检查一个运动是否会将它们带到界外,如果是这样则忽略它。 For example, moving right:
例如,向右移动:
if (right_key_pressed() && x_position < MAX_X_VALUE) {
x_position++;
}
That is, they will only move right if they press the → key and are not yet at the maximum X position value. 也就是说,只有按下→键并且尚未达到最大X位置值时,它们才会向右移动。 You can apply similar logic to the other directions.
您可以将类似的逻辑应用于其他方向。
Edit after addition of PickUpGame
class to question 添加
PickUpGame
类后进行编辑以进行提问
Since the movement logic is in PickUpGame
and you say you're not allowed to modify it, that makes it a little bit annoying. 由于移动逻辑在
PickUpGame
并且您说您不允许修改它,这使得它有点烦人。 Ideally the if
statements inside Move
would check the bounds. 理想情况下,
Move
的if
语句将检查边界。 Instead, you're going to have to do the check outside from where you call Move
: 相反,你将不得不从你调用
Move
地方进行检查:
if ((Direction == 72 && xPos > 0) ||
/* do the same for right, down, and left */) {
Move(Direction);
}
So you want it to check if the Direction
is up and there is room to move up, OR if it is right and there is room to move right, OR... and so on. 因此,您希望它检查
Direction
是否已启动且是否有向上移动的空间,或者如果它是正确的并且有向右移动的空间,或者......等等。 Then and only then will you pass Direction
along to Move
. 然后,只有这样你才能
Move
Direction
传递给Move
。
It sounds like all you need to do is keep a variable for where the user is in the array (you probably already have one) and two for the maximum and minimum value. 听起来你需要做的就是为用户在数组中的位置(你可能已经有一个)保留一个变量,并且为最大值和最小值保留两个变量。 Then, when the user presses an arrow key, before moving the cursor, test whether the action will take them out of bounds.
然后,当用户按下箭头键时,在移动光标之前,测试该动作是否会使它们超出范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.