Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void print_maze(char** mazeblock, int r, int c, int columns, int rows );
// void freeMem(char** mazeblock, int rows, int r,int c);
int main()
{
// all variables declared
int rows , columns , r , c,i,j, rowStart, columnStart, rowEnd, columnEnd, currentX, currentY;
char** mazeblock;
char orientation;
FILE *fPointer;// file pointer
//read file into array
fPointer = fopen("maze03.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 );// read the first line
// 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
// mazeblock[currentX][currentY ] == ' ' (IS A FLOOR)
// mazeblock[currentX][currentY ] == '+' (IS A MARKER)
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] == ' ' || 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 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");
printf(" The Coordiantes are row = %d , column = %d , Starting Row = %d, Starting Column = %d ,Ending Row = %d, Ending Column = %d\n\n", rows, columns, rowStart, columnStart, rowEnd, columnEnd );
print_maze(mazeblock, r, c, columns, rows );
// freeMem(mazeblock, rows, r, c);
// printf("test");
printf("\n");
if((currentX == rowEnd) && (currentY == columnEnd))
{
printf("The maze has been solved");
}
return 0;
}
void print_maze(char** mazeblock, int r, int c, int columns, int rows )
{
for (r = 0; r < rows; r++)
{
for ( c = 0; c < columns; c++)
{
printf("%c", mazeblock[r][c]);
}
}
}
// void freeMem(char** mazeblock, int rows, int r,int c)
//{
//for (r = 0; r < rows; r++){
// free ( mazeblock[r][c]);
//}
//free( mazeblock[r][c]);
//}