-
the problem the function i didnt finish because teacher not finish it
and the problem until i dont know how to put if statement for wall and not repeat in a function
then put it in function if you now it please help at least how put function in function
because in first i do what you said but not work with me
-
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 && j==l-1 && maze1[i][j]!='#')
{
maze1[i][j]=' ';
}
if(k==i && j==l+1 && maze1[i][j]!='#')
{
maze1[i][j]=' ';
}
if(i==k+1 && l==j && maze1[i][j]!='#')
{
maze1[i][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");
}
}
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;
}
if(k==0 && l==48)
{
printf("Congratulations, you won the Amazing Maze!");
printf("\t\t Amazing Maze Design by:\n thaer serhan");
w=2;
}
}
return 0;
}
this work but not work with wall
-
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 && j==l-1 && maze1[i][j]!='#')
{
maze1[i][j]=' ';
}
if(k==i && j==l+1 && maze1[i][j]!='#')
{
maze1[i][j]=' ';
}
if(i==k+1 && l==j && maze1[i][j]!='#')
{
maze1[i][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");
}
}
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;
if(maze1[k][l]=='#')
{
k=k+1;
}
dis(k,l);
break;
case 'd':
k=k+1;
if(maze1[k][l]=='#')
{
k=k-1;
}
dis(k,l);
break;
case 'l':
l=l-1;
if(maze1[k][l]=='#')
{
l=l+1;
}
dis(k,l);
break;
case 'r':
l=l+1;
if(maze1[k][l]=='#')
{
l=l-1;
}
dis(k,l);
break;
}
if(k==0 && l==48)
{
printf("Congratulations, you won the Amazing Maze!");
printf("\t\t Amazing Maze Design by:\n thaer serhan");
w=2;
}
}
return 0;
}
i do it with less coding
how you think
-
That's much better. Although I'm not sure about all those "if ... != '#' ..." in the dis() function - you really should be able to remove your C from where you are instead.
--
Mats
-
Maze algo:
Requires: Stack, maze size, total_cells (number of cells in maze), counter for cells
1. Allocate an maze array based on size (maze[maxrow,maxcol] or maze[maxrow*width)maxcol])
2. Create a stack (an array of size total_cells can be used to)
3. Choose any location in the maze to start and push onto stack
4. Create wall or leave empty and increase count -> if (count>total_cells) maze is done
5. Choose any of 4 directions to move
6. If cell in direction is empty, move there and push on stack, goto 4.
7. If cell in direction is not empty, pop location off stack, move there and goto 5
This greatly reduces the lines of code and gets rid of all those if's. This will also produce a completely random (or as random as your random number generator is) maze each time regardless of size. This will create a maze in milliseconds either recursively or iteratively.