Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Nov 25th, 2004, 8:29 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
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
x = 1 and y = 2 (with zero based indexing) under the cartesian coordinate system would result in 8. However, because C++ arrays are in row-major order, the index of the first dimension is the vertical path (it selects which single array in the array of arrays to use) and the index of the second dimension is the horizontal path (it selects which item in the single array to use). Using the same indices under C++ rules, the result would be 6. You should adjust your logic and assumptions accordingly.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC