Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by TechBiz Xccelerator
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




Powered by binpress