Bit of a problem in making a map(array) for a little game

Okay, this is a the code for refreshing a map(2D array) and printing it to the console, and taking input from the keyboard on where you want your character(X) to move. Something I have been trying to figure out is getting the code to detect if you have hit a wall, (A | or - in the array). And then, if you have, reverting your character back to where you were before you tried to walk onto the wall. My problem is, when I hit a wall it will start looping the MapRefresh() function infinitely. that's all I can really say, considering since it runs there wouldn't be any error output. It just doesn't run the way I'd like it too.

The MapRefresh function itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void MapRefresh()
{
	cout <<  string(10000, '\n');

	if(Move=='w')
	{
		CoordY--;
	}
	if(Move=='d')
	{
		CoordX++;
	}
	if(Move=='s')
	{
		CoordY++;
	}
	if(Move=='a')
	{
		CoordX--;
	}


	for(int col=0;col<12;col++)
	{

		for(int row=0;row<12;row++)
    {
		if(col==CoordY && row==CoordX)
		{
			if(Map[col][row]!='|' && Map[col][row]!='-')
			{
              cout << 'X';
			  PosY = CoordY;
			  PosX = CoordX;
			}
			else
			{
			    CoordY = PosY;
			    CoordX = PosX;
				Hit=true;
			}
		}
		else
		{
			cout << Map[col][row];
		}
	}
		cout << endl;


    }

}


The part where the function is called:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	//Main Game Loop
	while(true)
	{

	cin >> Move;

	Hit:
	MapRefresh();

	if(Hit==true)
	{
		Hit=false;
		goto Hit;
	}

	//Main loop end
	}


I know, I know, there are some things here most programmers would not agree with, (IE Goto's), but I am not planning to keep these, and will replace then at some point. Also, I don't think this is really necessary, but here are all of the declarations at the beggining.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char Choice[10], Name[10], Move;

int CoordX = 1;
int CoordY = 1;
bool Hit=true;
int PosY;
int PosX;

char Map[12][12]= {{"+---------+"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"|         |"},
                   {"+---------+"}};

void MapRefresh();


And your goto label has the same name as your variable

When you hit a wall, you are asking for display the map again.
In your display you make the move again. Display only should display the map.
You hit the wall, again.
When you hit a wall, you are asking for display the map again.

Separate the logic a little:
Check if the move is valid, correct if necessary, display the map and ask for input (no need for the inner loop)
I had thought by adding the if statement with the Hit variable and then it setting itself back to false wouldn't cause it to loop. The PosX and Y variables are supposed to keep a sort of history of your last move, and then when you hit a wall, it sets the CoordY and X to the PosX and Y to reset the move, therefore putting you in the place you were before the wall and then breaking the past the goto if, since it set Hit back to false, and allow the program to receive input again. Embarrassed to say, I think I am having trouble reading my own code. So I don't think I really understand what to move where, and what to remove.... I really need to learn to write this stuff better.
why not put the code for moving the character into a function, and do the checking in there, rather than having to revert to the old position if you're on a wall

1
2
3
4
5
6
7
8
void TryToMove(int xChange, int yChange)
{
    if ( Map[xPos_xChange,yPos+yChange] != '-' && Map[xPos_xChange,yPos+yChange] != '|' )
    {
        xPos += xChange;
        yPos += yChange;
    }    
}
Last edited on
I had thought by adding the if statement with the Hit variable and then it setting itself back to false wouldn't cause it to loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	//Suppose that you hit the wall
	MapRefresh(); //side effect Hit=true;
	
	if(Hit==true) //true, get inside
	{
		Hit=false;
		//goto Hit; loop unrolling
		MapRefresh(); //nothing changes so Hit=true
		
		if(Hit==true) //true, get inside
		{
			Hit=false;
			//goto Hit; loop unrolling
			MapRefresh(); //nothing changes so Hit=true
			//...
		}
	}
Last edited on
What exactly is the obvious answer I keep seeming to miss? The thing to do? Please excuse my ignorance, and perhaps stubbornness.
As I am sure by now you are probably smacking your face in annoyance. :/
Topic archived. No new replies allowed.