2d array map(help)

Im having a problem with my 2d array in which you can move around in. The array is read in from a text file and then the character is able to use the "w" "a" "s" and "d" keys to move around.

my map.text file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
G G G G G G G G G G G G G G G G G G G G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I N E E D 4 S L E E P I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G I I I I I I I I I I I I I I I I I I G
G G G G G G G G G G G G G G G G G G G G



Problem: my code works fine except when my 'x' reaches a letter other than "I", it switches the letter one up from where it should be, i cant come up with a way in which it will just stay put. please help! i want to be able to do this:
" I I X G I I" -- what it does currently " I I G X I I" --
what i want it to do - " I I X G I I" -- "I I I X I I" (g is "behind" the x).
how can i go about coding this? i currently use my switch statement to erase my trail.

MAIN.CPP:
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
#include "Map.h"

void cls();
int main()
{
    Map mObj;
    mObj.setxpos(10);
    mObj.setypos(10);
    mObj.createmap();
    cls();
    while(std::cin){
            std::cout << "w - up\ns - down\na - left\nd - right\n";
            mObj.InputChoice();
            cls();
            mObj.RefreshScreen();
        }

}



void cls() //windows function for clearing screen, not actually my function
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}

MAP.H:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MAP_H
#define MAP_H
#include <string>

class Map
{
    public:
        void createmap() ;
        void InputChoice();
        void RefreshScreen();
        void setxpos(int x){xpos = x;};
        void setypos(int y){ypos = y;};
        Map();
    private:
    static const int nrows = 20;
    static const int ncols = 20;
    int xpos;
    int ypos;
    char map[nrows][ncols];
};

#endif // MAP_H 

MAP.CPP:
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
#include "Map.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>


Map::Map()
{
}

void Map::RefreshScreen()
{
    for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++)
                std::cout << map[nrow][ncol] << " ";
            std::cout << std::endl;
            }
}

void Map::InputChoice()
{
        char x;
        char temp;
        std::cin >> x;
         switch(x){
                case 'w' :
                    temp = map[ypos - 1][xpos];
                    if(ypos <= 0){
                        ypos = 0;
                    }else{ypos-=1;}
                    map[ypos][xpos] = 'x';
                    map[ypos + 1][xpos] = temp;
                    break;
                case 's':
                    temp = map[ypos + 1][xpos];
                    if(ypos >= 19){
                        ypos = 19;
                    }else{ypos+=1;}
                    map[ypos][xpos] = 'x';
                    map[ypos - 1][xpos] = temp;
                    break;
                case 'a':
                    temp = map[ypos][xpos - 1];
                    if(xpos <= 0){
                        xpos = 0;
                    }else{xpos -=1;}
                    map[ypos][xpos] = 'x';
                    map[ypos][xpos + 1] = temp;
                    break;

                case 'd':
                    temp = map[ypos][xpos + 1];
                    if(xpos >= 19){
                        xpos = 19;
                        }else{xpos +=1;}
                    map[ypos][xpos] = 'x';
                    map[ypos][xpos - 1] = temp;
                    break;
            }
}

void Map::createmap() {
    using namespace std;
    ifstream MapStruct("Map.txt");
    while(!MapStruct.eof()){
        for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++){
                    MapStruct >> map[nrow][ncol];
                }
            }
        }
MapStruct.close();


}
use a second array as the background and put all non moveable items there. To erase the trail use the background items
could you give me an example? im confused on what you're saying
It's simple:

MAP.H:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAP_H
#define MAP_H
#include <string>

class Map
{
    public:
        void createmap() ;
        void InputChoice();
        void RefreshScreen();
        void setxpos(int x){xpos = x;};
        void setypos(int y){ypos = y;};
        Map();
    private:
    static const int nrows = 20;
    static const int ncols = 20;
    int xpos;
    int ypos;
    char map[nrows][ncols];
    char bg_map[nrows][ncols]; // add background map
};

#endif // MAP_H 
MAP.CPP:
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
#include "Map.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>


Map::Map()
{
}

void Map::RefreshScreen()
{
    for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++)
                std::cout << map[nrow][ncol] << " ";
            std::cout << std::endl;
            }
}

void Map::InputChoice()
{
        char x;
        char temp;
        map[ypos][xpos] = bg_map[ypos][xpos]; // clear the x positon
        std::cin >> x;
         switch(x){
                case 'w' :
                    if(ypos <= 0){
                        ypos = 0;
                    }else{ypos-=1;}
                    break;
                case 's':
                    if(ypos >= 19){
                        ypos = 19;
                    }else{ypos+=1;}
                    break;
                case 'a':
                    if(xpos <= 0){
                        xpos = 0;
                    }else{xpos -=1;}
                    break;

                case 'd':
                    if(xpos >= 19){
                        xpos = 19;
                        }else{xpos +=1;}
                    break;
            }
        map[ypos][xpos] = 'x'; // now set the x to the new position
}

void Map::createmap() {
    using namespace std;
    ifstream MapStruct("Map.txt");
    while(!MapStruct.eof()){
        for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++){
                    MapStruct >> map[nrow][ncol];
                    bg_map[nrow][ncol] = map[nrow][ncol]; // Set the background map
                }
            }
        }
MapStruct.close();


}
Topic archived. No new replies allowed.