Need help Getting Map to center around character.

//yes i know system commands are evil, and i only used it for the sake of showing
//my code on the forums in 1 file, got my own clearscreen function.

I need help getting the map for my game to only print me 6 characters above, below, left, and right of my player (as if my player (@) had a line of sight, and when i move, the map will update)

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <windows.h> 
#include "clearscreen.h"
using namespace std;
char map[18][32] = {
//The @ is you.
"+-----------------------------+",
"|                             |",
"|                             |",
"|-----------       -----------|",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|-----------       -----------|",
"|                             |",
"|                             |",
"|                             |",
"|              @              |",
"+-----------------------------+"};
void ShowMap()
{
     int MAP;
     for(MAP=0;MAP<18;MAP++)
     {
     cout << map[MAP] << endl;
     }
}
inline void delay( unsigned long ms ) 
{ 
Sleep( ms ); 
}  
int main() 
{
        bool exit = false;
        int x=15, y=16;
        ShowMap();
    while (exit == false) 
    {
    system("CLS");
    ShowMap();
          
        if(GetAsyncKeyState(VK_UP)!=0)
        {
            if(map[y-1][x] != '-' && map[y+1][x] != '|' )
            {
            map[y][x]=' ';
            y -= 1;
            map[y][x]='@';
            }
        }
        if(GetAsyncKeyState(VK_DOWN)!=0)
        {
            if(map[y+1][x] != '-' && map[y+1][x] != '|' )
            {
            map[y][x]=' ';
            y += 1;
            map[y][x] = '@'; 
            }
        }
        if(GetAsyncKeyState(VK_LEFT)!=0)
        {
            if(map[y][x-1] != '-' && map[y][x-1] != '|' )
            {
            map[y][x]=' ';
            x -= 1;
            map[y][x] = '@';
            }
        }
        if(GetAsyncKeyState(VK_RIGHT)!=0)
        {
            if(map[y][x+1] != '-' && map[y][x+1] != '|' )
            {
            map[y][x]=' ';
            x += 1;
            map[y][x] = '@';
            }
        }
    }
}
Last edited on
In the ShowMap() function pass the map as argument and search it for your player character. When it finds it make it print only the elements around the player, which are [x-1[y-1], [x][y-1], [x+1][y-1], [x-1][y], [x][y] (the player itself), [x+1][y]
Problem is, it thinks x and y are undefined in that function (x,y only defined in main). And if i make the x and y variables globals, then the map messes up because both the map, and the variables are global.
Last edited on
Well, that was pretty stupid of me, I didn't notice you already keep track of the player position. In this case there's no need to search the map for the player. Pass 'x' and 'y' to ShowMap() and make it print only the characters you need. Consider making 'map' local to main and passing it to ShowMap(), as it's bad to get used to having globals.

By the way, even if they were global, they wouldn't get messed up, because they are not modified when printing the map. Of course this line of thinking is bad and must be avoided
im having trouble passing it from main, when i use it in showmap, it says x and y are undeclared in first use of this function, so can you play around w/ this code?
Post the prototype and how you call it
The nonworking prototype, because x and y are undeclared, it says
1
2
3
4
5
6
7
8
9
10
11
12
void ShowMap()
{
     int MAP;
     for(MAP=y-6;MAP<y+6;MAP++)
     {
         for(MAP=x-6;MAP<x+6;MAP++)
         {
         cout << map[y][x];
         }
     cout << endl;
     }
}
Last edited on
So as maeriden said, you need to pass x and y into the ShowMap function.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Change the parameters
void ShowMap(int x, int y /*, char** map  */) // If you want to make 'map' local then pass it too
{
//  int MAP; // You can't use the same variable for both loops. The inner loop
                     // modifies it screwing with the outer loop execution
     int i, j;
     for(i=y-6; i<y+6; i++)
     {
         for(j=x-6; j<x+6; j++)
         {
         cout << map[i][j];
         }
     cout << endl;
     }
}


Now there's another issue. What if x=4 ? That would print starting from map[-2][0]. The same thing happens if 'i' or 'j' are smaller than 0 or greater than the array size. You have to implement a way to check if you go out of bounds
Last edited on
Now there's another issue. What if x=4 ? That would print starting from map[-2][0]. 
The same thing happens if 'i' or 'j' are smaller than 0 or greater than the array size.
 You have to implement a way to check if you go out of bounds


i tried setting parameters to make sure it didn't print, if it went over or under, but even when i do that it messes up; if i go too far down it prints strange characters; if i go too far left or right it shows the opposite side of the map, please help, thanks

current code//please test any changes before presenting a fix
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <windows.h> 
#include "clearscreen.h"
using namespace std;
char map[18][32] = {
//The @ is you.
"+-----------------------------+",
"|                             |",
"|                             |",
"|-----------       -----------|",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|                             |",
"|-----------       -----------|",
"|                             |",
"|                             |",
"|                             |",
"|              @              |",
"+-----------------------------+"};
void ShowMap(int x, int y)
{
     system("CLS");
     int i, j;
         for(i=y-6; i<y+6; i++)
         {
             for(j=x-6; j<x+6; j++)
             {
             cout << map[i][j];
             }
         cout << endl;
         }
}

inline void delay( unsigned long ms ) 
{ 
Sleep( ms ); 
}  
int main() 
{
        bool exit = false;
        int x=15, y=16;
        ShowMap(x,y);
    while (exit == false) 
    {
          
        if(GetAsyncKeyState(VK_UP)!=0)
        {
            if(map[y-1][x] != '-' && map[y+1][x] != '|' )
            {
            map[y][x]=' ';
            y -= 1;
            map[y][x]='@';
            ShowMap(x,y);
            }
        }
        if(GetAsyncKeyState(VK_DOWN)!=0)
        {
            if(map[y+1][x] != '-' && map[y+1][x] != '|' )
            {
            map[y][x]=' ';
            y += 1;
            map[y][x] = '@'; 
            ShowMap(x,y);
            }
        }
        if(GetAsyncKeyState(VK_LEFT)!=0)
        {
            if(map[y][x-1] != '-' && map[y][x-1] != '|' )
            {
            map[y][x]=' ';
            x -= 1;
            map[y][x] = '@';
            ShowMap(x,y);
            }
        }
        if(GetAsyncKeyState(VK_RIGHT)!=0)
        {
            if(map[y][x+1] != '-' && map[y][x+1] != '|' )
            {
            map[y][x]=' ';
            x += 1;
            map[y][x] = '@';
            ShowMap(x,y);
            }
        }
    delay(10);
    }
}
Last edited on
Any help would be great.
Why not use a debugger - or even just print out some useful debugging output - to see what's going on?
the program works, its just that it prints out random emoticon characters if i go too far down or up when the formula
 
(i+6>18 or i-6<0)
is true or it displays the other side of the map with the when the formula
 
(j-6<0 or j+6>32) 
is true because it starts reading from the beginning of the row once it goes over

(i need help doing the parameters that make it not do the weird displaying as i keep messing up and it does not work)
Last edited on
You asked :
Print characters (arrows) up - down - left - right around the character position?
Have you solved your delay problem yet?
Last edited on
no because when i go out of boundrys it does weird thisgs, see above post
This is "Printing array map problem", right?
This is "Printing array map problem", right?


yes, it is a printing map problem, and even if i tried to set parameters it still prints funny

thats what i need help fixing
Last edited on
1
2
3
4
5
6
7
8
9
10
     system("CLS");
     int i, j;
         for(i=y-6; i<y+6; i++)
         {
             for(j=x-6; j<x+6; j++)
             {
             cout << map[i][j];
             }
         cout << endl;
         }


I think, your code prints a specific range of map around the character.

Now you want to add an additional feature. Consider :

First, I define a few parameters :
1
2
#define Rows 18
#define Columns 32 




1-
(y - 6) < 0

E.g : y = 3
1
2
3
for(i= y - 6; i< y + 6; i++)

-> for(i= -3; i < 3; i++)


Certainly you don't want your program to access an invalid array item?
You want (i >= 0), not (i < 0)?
And, the solution :
Put a comparison expression.
2-
(y + 6) > 32

E.g : y = 29
1
2
3
for(i = y - 6; i< y + 6; i++)

-> for(i= 26; i < 33; i++)// 33 is invalid 

Similar case...
And, the solution :
Put a comparison expression.

And x... Similar solutions...
So we'll have the right code :
1
2
3
4
5
6
7
8
9
10
11
12
13
  system("CLS");
     int i, j;
         for(i=y-6; i<y+6; i++)
         {
             if(i >= 0 && i < 18)
             for(j=x-6; j<x+6; j++)
             {
             if(j >= 0 && j < 32)
             cout << map[i][j];
             }
         cout << endl;
         }
//Where 18 is number of rows and 32 is number of columns 


You'll need to define some const variables (such as : Rows, Columns)

Hope this helps. Do you know the hand-checking skill?
Last edited on
Thanks for the help, it worked perfectly :)
Topic archived. No new replies allowed.