![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
In cartesian coordinate notation, x is the horizontal path while y is the vertical path. Using the table:
1 2 3 4 5 6 7 8 9 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|