fork download
  1. /*
  2. Lab 4
  3. CNIT 315 - Fall 21
  4. Created by: Ben Paglia
  5. Email: bpaglia@purdue.edu
  6. Lab: 11:30 Friday
  7. This is program creates a maze using a 2 dimensional array. The maze has a designated start and end position. The program then uses recursive functions to find a path through the maze from the start to the end points.
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #define HEIGHT 6
  13. #define WIDTH 6
  14. char maze [HEIGHT] [WIDTH] = { {'S','#','#','#','#','#'},
  15. {'.','.','.','.','.','#'},
  16. {'#','.','#','#','#','#'},
  17. {'#','.','#','#','#','#'},
  18. {'.','.','.','#','.','G'},
  19. {'#','#','.','.','.','#'}};
  20.  
  21. int possible;
  22. int main()
  23. {
  24.  
  25.  
  26. void mazeGo(int, int );
  27.  
  28. mazeGo(0, 0);
  29. return 0;
  30. }
  31.  
  32. void mazeGo(int y, int x )
  33. {
  34. char surrounding [4] = {' ',' ',' ',' '};
  35. int North, South, East, West;
  36. North = y-1;
  37. South = y+1;
  38. East = x+1;
  39. West = x-1;
  40. if (North > 0){
  41. surrounding [0] = maze [North] [x]; //North
  42. }
  43. else {surrounding [0] = '#';}
  44. if (East < WIDTH){
  45. surrounding [1] = maze [y] [East]; //East
  46. }
  47. else {surrounding [1] = '#';}
  48. if (South < HEIGHT){
  49. surrounding [2] = maze [South] [x]; //South
  50. }
  51. else {surrounding [2] = '#';}
  52. if (West > 0){
  53. surrounding [3] = maze [y] [West]; //West
  54. }
  55. else {surrounding [3] = '#';}
  56. possible = 0;
  57. /*
  58. if (maze [y] [x] == '.' || '+')
  59. {
  60. for (int a=0; a<4; a++)
  61. {
  62. if (surrounding [a] == '.')
  63. {
  64. possible = 1;
  65. }
  66. }
  67. }
  68. if (possible == 1)
  69. {
  70. maze [y] [x] = '+';
  71. }
  72. else
  73. {
  74. maze [y] [x] = '@';
  75. }
  76. */
  77.  
  78. if (maze [y] [x] == 'G')
  79. { for (int i=0; i<HEIGHT; i++ )
  80. {
  81. for (int w=0; w<WIDTH; w++)
  82. {
  83. printf("%c ", maze [i] [w]);
  84. }
  85. printf("\n");
  86. }
  87. return;
  88. }
  89.  
  90. else
  91. {
  92. if (surrounding [1] != '#' || '@' && East < WIDTH ){
  93. mazeGo(y, East);//East
  94. }
  95. else if (surrounding [2] != '#' || '@' && South < HEIGHT){
  96. mazeGo(South, x);//South
  97. }
  98. else if (surrounding [3] != '#' || '@' && West < WIDTH){
  99. mazeGo(y, West);//West
  100. }
  101. else if(surrounding [0] != '#' || '@' && North < HEIGHT){
  102. mazeGo(North, x);//North
  103. }
  104. }
  105.  
  106. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
S # # # # # 
. . . . . # 
# . # # # # 
# . # # # # 
. . . # . G 
# # . . . #