View Single Post
Old Nov 24th, 2004, 3:47 PM   #1
bigmitch
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 bigmitch is on a distinguished road
Send a message via MSN to bigmitch
Heres my code:

[ CODE ]


#include <stdio.h>
#include <stdlib.h> /*header for malloc call*/

/*setting filename before main program starts*/
#define FILENAME "maze2.txt"

/*Recusive maze solver function*/
void solve (char **maze, int row, int col) {

if (maze[row][col] == 'g' )
{
printf("Goal Reached!\n");
}
else if (maze[row][col] == 'p')
{
maze[row][col] = 'f';

if (maze[row-1][col] == 'p') //up
{
maze[row-1][col] = 'f';
solve(maze, row-1, col);
maze[row-1][col] = 'f';
}

if (maze[row+1][col] == 'p') //down

{
maze[row+1][col] = 'f';
solve(maze, row+1, col);
maze[row+1][col] = 'f';
}

if (maze[row][col-1] == 'p') //left

{
maze[row][col-1] = 'f';
solve(maze, row, col-1);
maze[row][col-1] = 'f';
}

if (maze[row][col+1] == 'p') //right

{
maze[row][col+1] = 'f';
solve(maze, row, col+1);
maze[row][col+1] = 'f';
}
}
return;
}


/*main*/
int main ( void )
{

/*pointer to file */
FILE *ipf;

/*declare variables*/
int size,x,y;
char **maze;
int p, q;
int ch;

/* Open file for reading*/
ipf = fopen ( FILENAME, "r" );

/* If file not there show error*/
if((ipf=fopen(FILENAME,"r"))==NULL) {
printf("The file is not open");
}

/* Get size of maze from file*/
fscanf ( ipf, "%d", &size );
printf ("%d x %d MAZE\n Text File:\n", size, size);

/* Check for start of maze */
if ( fgetc ( ipf ) != '\n' ) {
printf ("Invalid file format\n" );
fclose (ipf);
}

/*Declaring maze using memory allocation*/
maze = malloc ( size * sizeof *maze );

for (p= 0; p < size; p++ ) {
maze[p] = malloc ( size * sizeof *maze[p] );

}

/*Loop through rows and columns of text file entering
*characters into array*/
for ( p = 0; p < size; p++ ) {
for ( q = 0; q < size; q++ ) {
ch = fgetc ( ipf ); //fgetc gets char's from text file
if ( ch != '\n' )
maze[p][q] = (char)ch;
else
--q;
}
}

/* Displays the whole maze */
printf ( "%d\n", size );
for ( p = 0; p < size; p++ ) {
for ( q = 0;q < size; q++ )
putchar ( maze[p][q] );
putchar ( '\n' ); //new line added at end of each row
}

/*Closes file from reading*/
fclose (ipf);

/*Ask user for start coordinates*/
start: printf("The start coordinates must be a p character\n");
printf("Coordinates start from 0,0\n");
printf("Give start coordinates\nX = ");
scanf("%d", &x);
if ( x >= size )
{
printf("Coordinate out of range\n");
goto start; //if x value too big then restart
}
printf("Y = ");
scanf("%d", &y);
if ( y >= size )
{
printf("Coordinate out of range\n");
goto start;//if y value too big then restart
}

if (maze[x][y] == 'x')
{
printf ( "You have landed on a wall! - Please try again\n");
goto start; //if not starting on x then restart
}

if (maze[x][y] == 'g' )
{
printf( "You got lucky - End of Maze!\n" );
exit(1);
}
printf("Start at [%d , %d]\n", x,y);

solve (maze, x, y);

return 0;
}


[ CODE\ ]


Here's the txt file:

11
xxxxxxxxxxx
xpxxpxxpgxx
xppppxppxxx
xxxppxpppxx
xpppxxxxpxx
xxxppppppxx
xpppxxxxxxx
xxxpxxxppxx
xxxpppppxxx
xxxxxxxxxxx
xxxxxxxxxxx

Question: When I type coordinates at beginning of program, X seams to the Y coordinate and visa versa? eg type X = 1 and Y = 2 shows x - wall and not p - path
bigmitch is offline   Reply With Quote