hi iam new to c++ and i have been working on a array it is fairly simple but i have a small dilemma. well you see when i move around in my 2d array it all works fine, but when i walk outside of the map it glitches up. so i was wondering if there is a way to restrict the user from going outside the map
here is the code so far:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main (){
string direction;
string room [3][3];
bool game =true;
room[0][0]="this is the northwest room ";
room[0][1]="this is the west room ";
room[0][2]="this is the southeast room ";
room[1][0]="this is the north room ";
room[1][1]="this is the middle room ";
room[1][2]="this is the south room ";
room[2][0]="this is the northeast room ";
room[2][1]="this is the east room ";
room[2][2]="this is the southeast room ";
int x=0;
int y=0;
if (direction =="east"){
x++;
}
if (direction =="south"){
y++;
}
if (direction =="west"){
x--;
}
if (direction =="north"){
y--;
}//fixa ett slut
}
getch ();
return 0;
}
By MAX I just ment the maximal value x can has: in your case thats 2. The same way, you need to define the maximum value for y and for both x and y a minumium value, for the other directions.