Well I hope one day I can do this easier, but right now its a pain.
This next problem is suppose to navigate a maze using recursion. An array 8x8 is suppose to represent the maze. You start out at 0,1 and are suppose to get to 7,7. If a spot equals x then it is a impassable block. Using recursion I'm suppose to print out the path to take and if there is no path I'm suppose to print out that their is no path(I'm not even up to this parts yet).
Anyways what I _think_ I've done so far is:
Move right, and if I can't move right I move down, and if I can't move down, I move left to see if I can move down. Right now I set the maze up so that there are no x's and I'm getting through the maze moving straight right then straight down. I tried putting in a road block on the furthest right column earlier to check if I really do move left then down, but I'm not sure its doing that.
Here is my code - I didn't really comment it yet because I've been doing a lot of deleting of the code. right now x_coord and y_coord functions are there to move you along the maze.
neighbors tells x_coord and y_coord if they are able to move to left, right, up or down and fill_with_blanks fills the character array in neighbors with blank spaces(since 1's represent if it can move). AND I used recursion to fill it with blanks(at least I did a recursion problem!)
Code:
#include <stdio.h>
void x_coord(int x, int y, char maze[8][8]);
void y_coord(int x, int y, char maze[8][8]);
void neighbors(int x, int y, char maze[8][8], char x_locals[5]);
void fill_with_blanks(char x_locals[5], int step);
int main(){
char maze[8][8];
for(int a = 0; a <= 7; a++){
for(int b = 0; b <= 7; b++){
maze[a][b] = ' ';
}
}
x_coord(0,1,maze);
return 0;
}
void x_coord(int x, int y, char maze[8][8]){
char x_locals[5];
neighbors(x,y,maze, x_locals);
if(x == 7 && y == 7) return;
if(x_locals[0] == '1')
x_coord(x+1,y,maze);
if(x <= 7 && x_locals[0] != '1')
y_coord(x,y,maze);
}
void y_coord(int x, int y, char maze[8][8]){
char x_locals[5];
neighbors(x,y,maze,x_locals);
if(x == 7 && y == 7) return;
if(x_locals[2] == '1')
y_coord(x,y+1,maze);
else if(x_locals[2] != '1' && x_locals[1] == '1')
y_coord(x-1,y,maze);
}
void neighbors(int x, int y, char maze[8][8],char x_locals[5]){
fill_with_blanks(x_locals, 3);
// right, [0]
// left, [1]
// down, [2]
// up, [3]
if(x < 7){
if(maze[x+1][y] != 'x')
x_locals[0] = '1';
}
if(x > 0){
if(maze[x-1][y] != 'x')
x_locals[1] = '1';
}
if(y < 7){
if(maze[x][y+1] != 'x')
x_locals[2] = '1';
}
if(y > 0){
if(maze[x][y-1] != 'x')
x_locals[3] = '1';
}
}
void fill_with_blanks(char x_locals[5], int step){
if(step < 0){
}
else{
x_locals[step] = ' ';
fill_with_blanks(x_locals, step - 1);
}
}