Code:
#include <stdio.h>
#define M 50
#define N 50
int rows, columns, exitRow, exitCol, enterRow, enterCol;
void printArray(char a[M][N], int rows, int columns);
int moveLeft(int currCol, int currRow, char a[M][N]);
int moveRight(int currCol, int currRow, char a[M][N]);
int moveUp(int currRow, int currCol, char a[M][N]);
int moveDown(int currRow, int currCol, char a[M][N]);
int Move(int currCol, int currRow, char array[M][N], int first);
void removeX(int currRow, int currCol, char array[M][N]);
int fail=0;
main()
{
char comma;
char foundEnter='n';
char foundExit='n';
int first=1;
//takes in the size of the maze
printf("Please type in the size of the field as rows, columns\n");
scanf("%d", &rows);
scanf("%c", &comma);
scanf("%d", &columns);
/*Asks user for input file and adds the field from the file to an array*/
char array[M][N]={9};
FILE *fptr;
char c;
char file_name[20];
int i,j;
printf("Type in the name of the file containing the Field\n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
c=fgetc(fptr);
while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
array[i][j]=c;
}
fclose(fptr);
//Copies maze into the failed array
char failArray[M][N]={9};
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
failArray[i][j]=array[i][j];
}
//finds the entrance and exit of the maze
for(j=0; j<columns; j++){
if(array[0][j] != '1' && array[0][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=0;
foundEnter='y';
}
else{ exitCol= j;
exitRow=0;
foundExit='y';
}
}
}
for(i=0; i<rows; i++){
if(array[i][columns-1] != '1' && array[i][columns-1]=='0'){
if(foundEnter=='n'){
enterCol= columns-1;
enterRow=i;
foundEnter='y';
}
else { exitCol= columns-1;
exitRow=i;
foundExit='y';
}
}
}
for(j=columns-1; j>=0; j--){
if(array[rows-1][j] != '1' && array[rows-1][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=rows-1;
foundEnter='y';
}
else { exitCol= j;
exitRow=rows-1;
foundExit='y';
}
}
}
for(i=rows-1; i>=0; i--){
if(array[i][0] !='1' && array[i][0]=='0'){
if(foundEnter=='n'){
enterCol= 0;
enterRow=i;
foundEnter='y';
}
else { exitCol= 0;
exitRow=i;
foundExit='y';
}
}
}
//prints the array
printArray(array, rows, columns);
int currRow=enterRow; //sets the current Row to the enter Row
int currCol=enterCol; //sets the current Column to the enter Column
int end;
end=Move(currCol, currRow, array, first);
//if theres no path found it prints the fail array
if(end){
printArray(failArray, rows, columns);
printf("Path not found.\n");
}
else{
//if the path is found it prints the path to the exit
printArray(array, rows, columns);
printf("Path found.\n");
}
return 0;
}
//prints the array
void printArray(char a[M][N], int rows, int columns){
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<columns; j++) {
if (j == 0) printf("\n");
printf("%c ",a[i][j]);
}
printf("\n");
}
//checks if move Left is possible
int moveLeft(int currCol, int currRow, char a[M][N]){
if(currCol-1>=-1)
if(a[currRow][currCol-1]=='0')
return 1;
return 0;
}
//checks if move Right is possible
int moveRight(int currCol, int currRow, char a[M][N]){
if(currCol+1<currCol)
if(a[currRow][currCol+1]=='0')
return 1;
return 0;
}
//checks if move Up is possible
int moveUp(int currRow, int currCol, char a[M][N]){
if(currRow-1>-1)
if(a[currRow-1][currCol]=='0')
return 1;
return 0;
}
//checks if move Down is possible
int moveDown(int currRow, int currCol, char a[M][N]){
if(currRow+1<currRow)
if(a[currRow+1][currCol]=='0')
return 1;
return 0;
}
//moves through the maze
int Move(int currCol, int currRow, char array[M][N], int first){
int fail;
int c;
array[currRow][currCol]='X';
failArray[currRow][currCol]='X';
//checks the exit condition as long as its not the first time through
if(!first){
if(currRow==exitRow && currCol==exitCol)
return 0;
if(currRow==enterRow && currCol==enterCol)
return 1;
}
//this is to track the array (temporary until code fixed)
first=0;
printArray(array, rows, columns);
printf("At Point row %d and col %d \n",currRow, currCol);
printf("fail=%d \n", fail);
getchar();
//goes left until it can't
if(moveLeft(currCol, currRow, array)){
c=--currCol;
printf("Moved left \n");
printf("Moving to col %d \n",c);
fail = !Move(c, currRow, array, first);
}
//if left fails goes up until it can't
if(moveUp(currCol, currRow, array) && fail){
c=--currRow;
printf("Moved up \n");
fail = !Move(currCol, c, array, first);
}
//if up fails goes down until it can't
if(moveDown(currCol, currRow, array) && fail){
c=++currRow;
printf("Moved down \n");
fail = !Move(currCol, c, array, first);
}
//if down fails goes right until it can't
if(moveRight(currCol, currRow, array) && fail){
c=++currCol;
printf("Moved right \n");
fail = !Move(c, currRow, array, first);
}
//removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;
//absolute failure
printf("Failed all \n");
return 0;
}
//removes the X and replaces it with a 0.
void removeX(int currRow, int currCol, char array[M][N]){
array[currRow][currCol]='0';
}