Array-reading function problem

I have built a function that reads numbers from an array (that stores a map) to determine which directions the player may move. It was intended to scan the tiles one above, below, to the left of and to the right of the specified position (unless they don't exist, in which case it will skip them to avoid a segmentation fault).

My problem: I used an example map to test it out and the function should've printed "You can move: east and south.". It actually printed, "You can move: north and east. south.". My map is as follows ('2' is a placeholder for the player's position and would be '1' in the actual map):

10210
41110
01506
61038
04117

The player should not even be able to move north, seeing as they are at the top of the map anyway. They should only be able to move east and south.

The code is here:

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    void DirectionCheck(MapFunctions& MapObject)
    {
        bool DirectionSpecifier[4];
        int DirectionSpecifierCount = 0;
        if(locy>0)
        {
            if(MapObject.ReadMap(locx,locy-1)!=0)
            {
                DirectionSpecifier[North]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[North]=0;
        }
        if(locx<MapObject.getCols())
        {
            if(MapObject.ReadMap(locx+1,locy)!=0)
            {
                DirectionSpecifier[East]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[East]=0;
        }
        if(locy<MapObject.getRows())
        {
            if(MapObject.ReadMap(locx,locy+1)!=0)
            {
                DirectionSpecifier[South]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[South]=0;
        }
        if(locx>0)
        {
            if(MapObject.ReadMap(locx-1,locy)!=0)
            {
                DirectionSpecifier[West]=1;
                DirectionSpecifierCount++;
            }
        }
        else
        {
            DirectionSpecifier[West]=0;
        }
        for(int i=0; i<4; i++)
        {
            if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>1)
            {
                switch(i)
                {
                case 0:
                    cout << "north, ";
                    break;
                case 1:
                    cout << "east, ";
                    break;
                case 2:
                    cout << "south, ";
                    break;
                case 3:
                    cout << "west, ";
                    break;
                }
                DirectionSpecifierCount--;
            }
            else if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount==1)
            {
                switch(i)
                {
                case 0:
                    cout << "north and ";
                    break;
                case 1:
                    cout << "east and ";
                    break;
                case 2:
                    cout << "south and ";
                    break;
                case 3:
                    cout << "west and ";
                    break;
                }
                DirectionSpecifierCount--;
            }
            else if(DirectionSpecifierCount=1 && DirectionSpecifier[i]!=0)
            {
                switch(i)
                {
                case 0:
                    cout << "north.";
                    break;
                case 1:
                    cout << "east.";
                    break;
                case 2:
                    cout << "south.";
                    break;
                case 3:
                    cout << "west.";
                    break;
                }
                DirectionSpecifierCount--;
            }
        }
    }


How do I solve the problem described above?
Thanks!
Last edited on
Topic archived. No new replies allowed.