0

我试图寻找我的问题的答案,但找不到任何完全正确的东西。

我的问题涉及设置一个数组并基本上围绕一个点移动小光标。我需要能够忽略用户的输入,如果他们在一个方向上按了一个箭头键,就会把他们带出网格的边界。我不完全确定什么最能做到这一点。

这是我无法修改的类。我只能继承它来做到这一点。

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

2 回答 2

2

您只需要检查一个运动是否会将它们带出边界,如果是,则忽略它。例如,向右移动:

if (right_key_pressed() && x_position < MAX_X_VALUE) {
  x_position++;
}

也就是说,只有按下→</kbd> key and are not yet at the maximum X position value. You can apply similar logic to the other directions.


PickUpGame将课程添加到问题后进行编辑

由于运动逻辑在PickUpGame并且你说你不允许修改它,这让它有点烦人。理想情况下,if里面的语句Move会检查边界。相反,你将不得不在你打电话的地方进行检查Move

if ((Direction == 72 && xPos > 0) ||
    /* do the same for right, down, and left */) {
  Move(Direction);
}

因此,您希望它检查是否Direction向上并且有向上移动的空间,或者它是否正确并且有向右移动的空间,或者......等等。然后,只有这样,你才会传递DirectionMove.

于 2013-05-21T21:40:16.550 回答
0

听起来您需要做的就是为用户在数组中的位置保留一个变量(您可能已经有一个),并为最大值和最小值保留两个。然后,当用户按下箭头键时,在移动光标之前,测试该动作是否会使它们超出范围。

于 2013-05-21T21:41:40.280 回答