Well here is your code, nicely formatted.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
// all variables declared
int rows, columns, r, c, i, rowStart, columnStart, rowEnd, columnEnd, currentX, currentY;
char **mazeblock;
char orientation;
FILE *fPointer; // file pointer
//read file into array
fPointer = fopen("maze01.txt", "r");
if (fPointer == NULL) {
printf("Error in opening file\n");
exit(1); // help check opening error
}
fscanf(fPointer, " %d %d %d %d %d %d \n", &rows, &columns, &rowStart, &columnStart, &rowEnd, &columnEnd);
// Dynamically allocate memory
mazeblock = (char **) malloc(sizeof(char *) * rows);
// failure to allocate memory
if (!mazeblock) {
printf("memmory allocation failed");
}
for (i = 0; i < rows; i++) {
mazeblock[i] = (char *) malloc(sizeof(char) * columns);
}
// to read in the maze
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
fscanf(fPointer, "%c", &mazeblock[r][c]);
}
}
fclose(fPointer);
// Defining direction
if (rowStart == 0) {
orientation = 'S';
} else if (rowStart == (rows - 1)) {
orientation = 'N';
} else if (columnStart == 0) {
orientation = 'E';
} else {
orientation = 'W';
}
// to hold direction
currentX = rowStart;
currentY = columnStart;
//
// for going south rules
while ((currentX != rowEnd) || (currentY != columnEnd)) {
if (orientation == 'S') {
if ((currentY > 0) && (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentY;
orientation = 'W';
} else if ((currentX < (rows - 1))
&& (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] == '#')) {
mazeblock[currentX][currentY] = '#';
++currentX;
orientation = 'S';
} else if ((currentY < (columns - 1))
&& (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
mazeblock[currentX][currentY] = '#';
++currentY;
orientation = 'E';
} else {
mazeblock[currentX][currentY] = '#';
--currentX;
orientation = 'N';
}
}
//for going North
else if (orientation == 'N') {
if ((currentY < (columns - 1))
&& (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
mazeblock[currentX][currentY] = '#';
++currentY;
orientation = 'E';
} else if ((currentX > 0)
&& (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentX;
orientation = 'N';
} else if ((currentY > 0)
&& (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentY;
orientation = 'W';
} else {
mazeblock[currentX][currentY] = '#';
currentX++;
orientation = 'S';
}
}
//for going West
else if (orientation == 'W') {
if ((currentX > 0) && (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentX;
orientation = 'N';
} else if ((currentY > 0)
&& (mazeblock[currentX][currentY - 1] == ' ' || mazeblock[currentX][currentY - 1] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentY;
orientation = 'W';
} else if ((currentX < (rows - 1))
&& (mazeblock[currentX + 1][currentY] == ' ' || mazeblock[currentX + 1][currentY] == '#')) {
mazeblock[currentX][currentY] = '#';
++currentX;
orientation = 'S';
} else {
mazeblock[currentX][currentY] = '#';
currentY++;
orientation = 'E';
}
}
//For going east
else {
if ((currentX < (rows - 1))
&& (mazeblock[currentX + 1][currentY] == '0' || mazeblock[currentX + 1][currentY] == 'W')) {
mazeblock[currentX][currentY] = 'W';
++currentX;
orientation = 'S';
} else if ((currentY < (columns - 1))
&& (mazeblock[currentX][currentY + 1] == ' ' || mazeblock[currentX][currentY + 1] == '#')) {
mazeblock[currentX][currentY] = '#';
++currentY;
orientation = 'E';
} else if ((currentX > 0)
&& (mazeblock[currentX - 1][currentY] == ' ' || mazeblock[currentX - 1][currentY] == '#')) {
mazeblock[currentX][currentY] = '#';
--currentX;
orientation = 'N';
} else {
mazeblock[currentX][currentY] = '#';
currentY--;
orientation = 'W';
}
}
}
mazeblock[currentX][currentY] = '# ';
printf("\n");
// output loop
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
printf("%1c", mazeblock[r][c]);
}
}
return 0;
}
The first problem is you don't even read in the maze properly.
> fscanf(fPointer, "%c", &mazeblock[r][c]);
This will fill your maze with \n characters from the input file.
> mazeblock[currentX][currentY] = '#';
Why are you filling in where you've been with the wall character?
If you reach a dead end, how are you going to get back to a place where you had a choice?
Add some constants to make the code more readable.
Code:
#define WALL '#'
#define FLOOR ' '
#define CRUMB '~' // mark where you've been
#define CHOICE '+' // a point in the maze where there was a choice of next move
So you can say things like
Code:
if ( maze[x][y] == FLOOR )
maze[x][y] = CRUMB;