Jump to content




Recent Topics

Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Need help with maze program.


  • Please log in to reply
1 reply to this topic

#1 posto2012

posto2012

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 24 November 2009 - 08:44 PM

I am working on a maze assignment. We are supposed to read in the text file that contains the maze, which looks like the one below, and convert it to a graph/ grid. I can get it to read in the file, but cant figure out what to do next. Any help would be appreciated. I'm not necessarily looking for code.
This is what I have so far.
int main(){
	FILE *myfile;
	char c;

	myfile = fopen("maze2.txt", "r+");

	if(!myfile){
	   printf("File not found.");
	}

	do{

		c = fgetc(myfile);
		putchar(c);
	}
	while(c!= EOF);
	fclose(myfile);
	node_t maze;
	return 0;
}


This is one of the mazes.; The * are blank spaces.
ooooooooooo
o*********o
o*ooooooo *o
o+o*******o
ooo* ooooooo
o$o******$o
o*o*ooooooo
o********-o
ooooooooooo

  • 0

#2 speculatius

speculatius

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts
  • Location:Slovakia
  • Programming Language:C, Java, C++, PHP, PL/SQL

Posted 25 November 2009 - 03:18 AM

Convert to grid? I think you want to do something like this:

// I will keep this easy. If you need to support large
// mazes, have a look on function [I]malloc[/I] for C
// or [I]new[/I] for C++
int maze[MAX_MAZE_WIDTH][MAX_MAZE_HEIGHT];

for(int i = 0; i < MAX_MAZE_HEIGHT; ++i) {
  for(int j = 0; j < MAX_MAZE_WIDTH; ++j) {
    int c = fgetc(myfile);
    if(c == '\n' || feof(myfile)) {
      break;
    }
    maze[j][i] = c;
  }
  if(feof(myfile) {
    break;
  }
}

I havent tested it, so you have to debug it :) But the idea should be ok.
  • 0