Array map check etc.

So far I have:
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
 character.xCoord = 5;
        character.yCoord = 5;
		char text;
		while (1 == 1)
		{
		    cin >> text;
		    switch(text)
		    {
		        case 'n':
		        character.xCoord -= 1;
;		        map();
		        break;
		        case 's':
                character.xCoord += 1;
		        map();
		        break;
		        case 'e':
		        character.yCoord += 1;
		        map();
		        break;
		        case 'w':
		        character.yCoord -= 1;
		        map();
		        break;
		        default:
		        break;
		    }
		}


and
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
void map()
{
    const int rows = 10;
	const int columns = 10;
	char board [rows][columns] =
	{
		{'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
		{' ', ' ', 'X', ' ', ' ', ' ', ' ', 'X', 'X', 'X'},
		{'X', ' ', 'X', ' ', 'X', 'X', ' ', ' ', 'X', 'X'},
		{'X', ' ', 'X', ' ', 'X', 'X', 'X', ' ', 'X', 'X'},
		{'X', ' ', 'X', ' ', ' ', 'X', 'X', ' ', 'X', 'X'},
		{'X', ' ', 'X', 'X', ' ', 'X', ' ', ' ', 'X', 'X'},
		{'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', 'X', 'X'},
		{'X', ' ', ' ', 'X', ' ', 'X', ' ', 'X', 'X', 'X'},
		{'X', 'X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' '},
		{'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}
	};

for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
			    if(character.xCoord == i && character.yCoord == j)
			    cout << '\1';
			    else
				cout << board[i][j];
			}
			cout << endl;
		}
		if (character.xCoord == 0 && character.yCoord == 0)
			    cout << '\t' << "Error";
}



How do I make it where if they try to go on the X spot, it says INVALID MOVE and pushes them back to there previous valid spot?
Maybe try this:

character.xCoord = 5;
character.yCoord = 5;
character.Tryx = character.xCoord;
character.Tryy = character.yCoord;

char text;
while (1 == 1)
{
cin >> text;
switch(text)
{
case 'n':
character.Tryx -= 1;
; break;
case 's':
character.Tryx += 1;
break;
case 'e':
character.Tryy+= 1;
break;
case 'w':
character.Tryy -= 1;
break;
default:
break;
}
}
if(board[character.Tryx][character.Tryy] != 'X')
{
character.xCoord = character.Tryx;
character.yCoord = character.Tryy;
}else{
cout << "Invalid Move!"
}
map();
Last edited on
Does not work, breaks the walking.
I didnt do it exactly, it has a few typos... like the ; behind the first break. I just posted as an idea of how to do it. You will have to tweak it a bit.
Last edited on
I know that, and I did tweak it, it dosent work .
My bad, put the
character.Tryx = character.xCoord;
character.Tryy = character.yCoord;
Inside the while loop.
Last edited on
It works a little, but is also really buggy. Sometimes it takes 2 clicks to go down, and when im at a corner, it shows the error.
Alright, try this:

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
#include <conio.h> //Used for getch();

character.xCoord = 5;
character.yCoord = 5;
int Tryx;
int Tryy;

char text;
while (1 == 1)
{
Tryx = character.xCoord;
Tryy = character.yCoord;
text = getch(); //Use if you want, so you dont have to hit enter everytime.
switch(text)
{
case 'n':
Tryx -= 1;
break;
case 's':
Tryx += 1;
break;
case 'e':
Tryy+= 1;
break;
case 'w':
Tryy -= 1;
break;
default:
break;
}
if(board[character.Tryx][character.Tryy] != 'X')
{
character.xCoord = Tryx;
character.yCoord = Tryy;
}
else
{
cout << "Invalid Move!"
}
map(); 
}


Im not sure if you want it so you have to press a key then hit enter, with getch() you just click a key and it does the next action. Use it if you want. But the if should also be in the while loop.
Topic archived. No new replies allowed.