Reputation: 3
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;
}
}
};
Upvotes: 0
Views: 378
Reputation: 110738
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. You can apply similar logic to the other directions.
Edit after addition of PickUpGame
class to question
Since the movement logic is in PickUpGame
and you say you're not allowed to modify it, that makes it a little bit annoying. Ideally the if
statements inside Move
would check the bounds. Instead, you're going to have to do the check outside from where you call 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. Then and only then will you pass Direction
along to Move
.
Upvotes: 2
Reputation: 296
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.
Upvotes: 0