How do I make it go in diagonal directions?

I have code that I already have four basic directions (Up, Down, Left, Right). However, I want to have them go in diagonal directions as well, so it will add up to eight directions. How would I do that? What I see from it, I basically have to have two directions change at the same time, but I don't know how to do that without ruining the entire code.

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <cstring> 
#include <iostream> 
#include <stack> 

using namespace std;

#define N 4 
#define M 5 

class node {
public:
    int x, y;
    int dir;

    node(int i, int j)
    {
        x = i;
        y = j;

        // Initially direction  
        // set to 0 
        dir = 0;
    }
};

// maze of n*m matrix 
int n = N, m = M;

// Coordinates of food 
int fx, fy;
bool visited[N][M];

bool isReachable(int maze[N][M])
{
    // Initially starting at (0, 0). 
    int i = 0, j = 0;
    cout << '[' << i << ':' << j << ']';
    stack<node> s;

    node temp(i, j);

    s.push(temp);

    while (!s.empty()) {

        // Pop the top node and move to the 
        // left, right, top, down or retract  
        // back according the value of node's 
        // dir variable. 
        temp = s.top();
        int d = temp.dir;
        i = temp.x, j = temp.y;

        // Increment the direction and 
        // push the node in the stack again. 
        temp.dir++;
        s.pop();
        s.push(temp);

        // If we reach the Food coordinates 
        // return true 
        if (i == fx and j == fy) {
            return true;
        }

        // Checking the Up direction. 
        if (d == 0) {
            if (i - 1 >= 0 and maze[i - 1][j] == 1 and
                !visited[i - 1][j]) {
                cout <<'['<< i - 1 << ':'<<j << ']' ;
                node temp1(i - 1, j);
                visited[i - 1][j] = true;
                s.push(temp1);
            }
        }

        // Checking the left direction 
        else if (d == 1) {
            if (j - 1 >= 0 and maze[i][j - 1] == 1 and
                !visited[i][j - 1]) {
                cout << '[' << i << ':' << j-1 << ']' ;
                node temp1(i, j - 1);
                visited[i][j - 1] = true;
                s.push(temp1);
            }
        }

        // Checking the down direction 
        else if (d == 2) {
            if (i + 1 < n and maze[i + 1][j] == 1 and
                !visited[i + 1][j]) {
                cout << '[' << i + 1 << ':' << j << ']' ;
                node temp1(i + 1, j);
                visited[i + 1][j] = true;
                s.push(temp1);
            }
        }
        // Checking the right direction 
        else if (d == 3) {
            if (j + 1 < m and maze[i][j + 1] == 1 and
                !visited[i][j + 1]) {
                cout << '[' << i  << ':' << j + 1<< ']' ;
                node temp1(i, j + 1);
                visited[i][j + 1] = true;
                s.push(temp1);
            }
        }

        // If none of the direction can take  
        // the rat to the Food, retract back  
        // to the path where the rat came from. 
        else {
            visited[temp.x][temp.y] = false;
            s.pop();
        }
        //system("pause");
    }

    // If the stack is empty and 
    // no path is found return false. 
    return false;
}

// Driver code 
int main()
{
    // Initially setting the visited 
    // array to true (unvisited) 
    memset(visited, false, sizeof(visited));

    // Maze matrix 
    int maze[N][M] = {
        { 1, 0, 1, 1, 0 },
        { 1, 1, 1, 0, 1 },
        { 0, 1, 0, 1, 1 },
        { 1, 1, 1, 1, 1 }
    };

    // Food coordinates 
    fx = 2;
    fy = 3;

    if (isReachable(maze)) {
        cout << "Path Found!" << '\n';
    }
    else
        cout << "No Path Found!" << '\n';

    return 0;
}
Last edited on
just go there. to go north west, its row-1 and col-1 from where you are.
with 8 directions I highly advise you to make that a function rather than 8 copies of virtually the same code.
Last edited on
Topic archived. No new replies allowed.