Thread: maze problems

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    maze problems

    i have to do maze
    but have this proplem

    Code:
    #include <stdio.h>
    #include <lab6.h>
    #include <unistd.h>
    
    int main()
    {
    
    	int i;
    	int j;
    
    	for(i=0;i<20;i++)
    	{
    	for(j=0;j<50;j++)
    	{
    	printf("&#37;c",maze1[i][j]);
    	}
    	printf("\n");
    	}
    
    
    	}
    the lab6.h have maze shape
    i know how to draw it
    but the problem i dont know how to make "c" in it
    and know how to move C but first i need to know how to make it in maze without use "define" i need simple solution
    please help
    Last edited by thaer89; 11-20-2007 at 11:18 AM.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    can i make the maze always repeat print it
    when i move c

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    The first problem is a complete lack of any declaration for maze1.

    Remember, lab6.h is something only you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Code:
    #include <stdio.h>
    #include <lab6.h>
    #include <unistd.h>
    
    
    
    
    void dis (int k,int l)
    {
    	int j;
    	int i;
    
    
    	for(i=0;i<20;i++)
    	{
    		for(j=0;j<50;j++)
    		{
    			if(k==i && l==j)
    				{
    					maze1[i][j]='C';
    				}
    			
    			printf("%c",maze1[i][j]);
    
    		}
    	printf("\n");
    	}
    }
    
    
    
    int main()
    {
    	int k=18;
    	int c;
    	int l=0;
    	int w=1;
    	dis(k,l);
    
    	while(w=1)
    	{
    			c=getchar();
    		  	getchar();
    
    		  switch(c)
    		  {
    		  	 case 'u':
    				  k=k-1;
    				  dis(k,l);
    				  break;
    
    			 case 'd':
    				  k=k+1;
    				  dis(k,l);
    				  break;
    			 case 'l':
    				  l=l-1;
    				  dis(k,l);
    				  break;
    	   		 case 'r':
    				  l=l+1;
    				  dis(k,l);
    				  break;
    			}
    	}
    	return 0;
    }
    this last thing i do it
    now i can make "C" not to repeat in every new frame or new maze draw
    and make clear screan

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess you'll have to "remove the C" in the previous location, before you place it in the new location.

    Also, you don't need to repeat the dis(l, k) in every case-block, just stick it after the end of the switch.

    And you need to check the range of l and k so that you don't stomp outside the maze somehow.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    i know this but how do it
    and how to clear screen

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thaer89 View Post
    i know this but how do it
    and how to clear screen
    You don't need to clear the screen, you just need to NOT store every C location inside your maze.

    One alternative is of course to just print the C instead of your maze content.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Last edited by thaer89; 11-20-2007 at 11:18 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's certainly not how I meant. If you must be doign it that way, perhaps creating a function that takes the current and old position as parameters would be a good idea - you are repeating essentially the same code 5 times in your code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    but u i dont know how and the proplem now how to make appear one C not two C
    Last edited by thaer89; 11-20-2007 at 10:41 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thaer89 View Post
    but u i dont know how
    The you have two choices:
    1. Think about how you would do it.
    2. Leave it as it is.

    You really don't have to do anything different from what you are doing right now - except use a function to draw the maze. If you make it a separate function to modify the maze (put a C in a new place) it probably makes it easier, rather than doing that as part of the draw function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    i know why right two c because (j-1) was drawing before my if

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, that's good.

    However, there are two things about all variants of your code like this one:
    Code:
    			  l=l+1;
    				for(i=0;i<20;i++)
    				{
    					for(j=0;j<50;j++)
    				{
    
    
    				if(k==i && l==j && maze1[i][j]!='#')
    					{
    						maze1[i][j]='C';
    						maze1[i][j-1]=' ';
    					}
    
    
    				printf("%c",maze1[i][j]);
    				}
    				printf("\n");
    				if(maze1[k][l]=='#')
    					{
    						l=l-1;
    					}
    1. You check if you hit a wall AFTER you've modified the position, and then back off - which is wrong.
    2. You are repeating a LARGE portion of code in 5 different places, which actually doesn't have to be repeated at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Code:
    #include <stdio.h>
    #include <lab6.h>
    #include <unistd.h>
    
    int main()
    {
    
    	int k=18;
    	int c;
    	int l=0;
    	int w=1;
    	int j;
    	int i;
    
    
    	for(i=0;i<20;i++)
    	{
    		for(j=0;j<50;j++)
    		{
    
    			if(k==i && l==j && maze1[i][j]!='#' )
    				{
    					maze1[i][j]='C';
    				}
    
    			printf("%c",maze1[i][j]);
    		}
    	printf("\n");
    	}
    
    	while(w!=0)
    	{
    			c=getchar();
    		  	getchar();
    
    		  switch(c)
    		  {
    
    	   		 case 'r':
    				  l=l+1;
    
    				for(i=0;i<20;i++)
    				{
    					for(j=0;j<50;j++)
    				{
    
    					if(k==i && j==l-1 && maze1[i][j]!='#')
    						{
    							maze1[i][j]=' ';
    						}
    					if(k==i && l==j && maze1[i][j]!='#')
    
    					{
    						maze1[i][j]='C';
    
    					}
    
    
    				printf("%c",maze1[i][j]);
    				}
    				printf("\n");
    				if(maze1[k][l]=='#')
    					{
    						l=l-1;
    					}
    	  		   }
    
    				  break;
    			 case 'l':
    				  l=l-1;
    			for(i=0;i<20;i++)
    			{
    			for(j=0;j<50;j++)
    			{
    			if(k==i && j==l+1 && maze1[i][j]!='#')
    						{
    							maze1[i][j]=' ';
    						}
    
    			if(k==i && l==j && maze1[i][j]!='#' )
    				{
    
    					maze1[i][j]='C';
    
    				}
    				printf("%c",maze1[i][j]);
    		    }
    			printf("\n");
    			if(maze1[k][l]=='#')
    					{
    						l=l+1;
    					}
      	  		  }
    			 	break;
    			case 'u':
    				  k=k-1;
    
    			for(i=0;i<20;i++)
    			{
    				for(j=0;j<50;j++)
    				{
    
    					if(i==k+1 && l==j && maze1[i][j]!='#')
    						{
    							maze1[i][j]=' ';
    						}
    					if(k==i && l==j && maze1[i][j]!='#' )
    					{
    						maze1[i][j]='C';
    
    					}
    
    				printf("%c",maze1[i][j]);
                 	}
    			printf("\n");
    			if(maze1[k][l]=='#')
    					{
    						k=k+1;
    					}
    		    }
    
    				  break;
    
    			 case 'd':
    				  k=k+1;
    
    			for(i=0;i<20;i++)
    			{
    				for(j=0;j<50;j++)
    				{
    				if(i==k-1 && l==j && maze1[i][j]!='#')
    						{
    							maze1[i][j]=' ';
    						}
    				if(k==i && l==j && maze1[i][j]!='#' )
    				{
    
    					maze1[i][j]='C';
    
    				}
    				printf("%c",maze1[i][j]);
      				}
    			printf("\n");
    			if(maze1[k][l]=='#')
    					{
    						k=k-1;
    					}
    			}
    			  break;
    			 case 'e':
    				return 0;
    			   break;
    			}
    
    			if(k==0 && l==48)
    				{
    
    				printf("Congratulations, you won the Amazing Maze!");
    				printf("Amazing Maze Design by:thaer serhan");
    				break;
    				}
    	}
    	return 0;
    }
    last version no error but i want to take character without enter
    and clear screen i can do it in Linux because not work in windows
    any body know how to make it small with void function with out change any thing because i try but i cant do it because if statement for not move from wall and dont repeat 'c'
    and how make computer do it with self it hard
    sorry for this

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, reading characters from the keyboard without hitting enter is non-portable, and non-trivial.

    I think you should start by concentrating on getting your maze-drawing into a function, and not repeat 5 x 15 lines of code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  2. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  3. Recursive Solution to Any Maze And Stack Overflow Problems
    By PunkyBunny300 in forum C Programming
    Replies: 14
    Last Post: 12-14-2002, 07:00 PM
  4. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM